Merge branch 'master' of git.curoverse.com:arvados into 11876-r-sdk
[arvados.git] / sdk / R / README
index 0c4347352ac043a6fe23826ab630488e05d1c96d..8a0c31dce6131ec30dc8f1d66fbc00b7b4531aa7 100644 (file)
@@ -1,72 +1,73 @@
 R SDK for Arvados
 
-Installation from source
+This SDK focuses on providing support for accessing Arvados projects, collections, and the files within collections.
 
-1. Dependencies
+The API is not final and feedback is solicited from users on ways in which it could be improved.
 
-libxml2-dev, libssl-dev, curl
+INSTALLATION
 
-2. Build the ArvadosR package tarball
+1. Install the dependencies
 
-cd arvados/sdk
-R CMD build R
+    > install.packages(c('R6', 'httr', 'stringr', 'jsonlite', 'curl', 'XML'))
 
-That will create a tarball of the Arvados package in the current directory.
+If needed, you may have to install the supporting packages first. On Linux, these are:
 
-3. Install the R package and its dependencies
+    libxml2-dev, libssl-dev, libcurl4-gnutls-dev or libcurl4-openssl-dev
 
-Then start R. Assuming the generated tarball is named `ArvadosR_0.0.1.tar.gz`,
-install it like this:
+2. Install the ArvardosR package
 
-> install.packages(c('R6', 'httr', 'stringr', 'jsonlite', 'curl', 'XML'))
-> install.packages('./ArvadosR_0.0.1.tar.gz', repos = NULL, type="source", dependencies = TRUE)
-> library('ArvadosR')
+    > install.packages('/path/to/ArvadosR_0.0.2.tar.gz', repos = NULL, type="source", dependencies = TRUE)
 
-4. Examples of usage:
 
---------------------------------------------------------------------------------------------------------------------------------
+EXAMPLES OF USAGE
 
-#Initialize API:
 
-arv <- Arvados$new("insert_token", "insert_host_name")
+#Load Library and Initialize API:
+
+library('ArvadosR')
+arv <- Arvados$new() # uses environment variables ARVADOS_API_TOKEN and ARVADOS_API_HOST
+arv <- Arvados$new("your Arvados token", "example.arvadosapi.com")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Get collection:
+#Get collection:
 
 arv$getCollection("uuid")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
 #List collections:
-collectionList <- arv$listCollections(list(list("uuid", "=", "aaaaa-4zz18-ccccccccccccccc")), limit = 10, offset = 2)
+collectionList <- arv$listCollections(list(list("name", "like", "Test%"))) # offset of 0 and default limit of 100
+collectionList <- arv$listCollections(list(list("name", "like", "Test%")), limit = 10, offset = 2)
 
---------------------------------------------------------------------------------------------------------------------------------
+collectionList$items_available # count of total number of items (may be more than returned due to paging)
+collectionList$items # items which match the filter criteria
 
-#Delete collection:
+#Next example will list all collections even when the number of items is greater than maximum API limit
 
-deletedCollection <- arv$deleteCollection("uuid")
+collectionList <- arv$listAllCollections(list(list("name", "like", "Test%")))
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Update collection:
+#Delete a collection:
 
-updatedCollection <- arv$updateCollection("uuid", list(name = "new_name", description = "new_desciption"))
+deletedCollection <- arv$deleteCollection("uuid")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Create collection:
+#Update a collection's metadata:
 
-createdCollection <- arv$createCollection(list(name = "new_name", description = "new_desciption"))
+updatedCollection <- arv$updateCollection("uuid", list(name = "New name", description = "New description"))
 
 --------------------------------------------------------------------------------------------------------------------------------
 
---------------------------------------------------------------------------------------------------------------------------------
+#Create collection:
 
-#Collection content manipulation:
+createdCollection <- arv$createCollection(list(name = "Example", description = "This is a test collection"))
 
---------------------------------------------------------------------------------------------------------------------------------
 
+--------------------------------------------------------------------------------------------------------------------------------
+COLLECTION CONTENT MANIPULATION
 --------------------------------------------------------------------------------------------------------------------------------
 
 #Create collection object:
@@ -75,9 +76,9 @@ collection <- Collection$new(arv, "uuid")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Get file/folder content as character vector
+#Get list of files
 
-collection$getFileContent()
+collection$getFileListing()
 
 --------------------------------------------------------------------------------------------------------------------------------
 
@@ -91,6 +92,21 @@ arvadosSubcollection <- collection$get("location/to/my/directory/")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
+#Read a table
+
+arvadosFile   <- collection$get("myinput.txt")
+arvConnection <- arvadosFile$connection("r")
+mytable       <- read.table(arvConnection)
+
+#Write a table
+
+arvadosFile   <- collection$create("myoutput.txt")
+arvConnection <- arvadosFile$connection("w")
+write.table(mytable, arvConnection)
+arvadosFile$flush()
+
+--------------------------------------------------------------------------------------------------------------------------------
+
 #Read whole file or just a portion of it.
 
 fileContent <- arvadosFile$read()
@@ -99,7 +115,7 @@ fileContent <- arvadosFile$read("raw", offset = 1024, length = 512)
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Get ArvadosFile or Subcollection size 
+#Get ArvadosFile or Subcollection size
 
 size <- arvadosFile$getSizeInBytes()
 size <- arvadosSubcollection$getSizeInBytes()
@@ -108,29 +124,26 @@ size <- arvadosSubcollection$getSizeInBytes()
 
 #Create new file in a collection
 
-#Call structure
-
-collection$add(arvadosFileOrSubcollectionOrFileName, optionalRelativePath)
+collection$create(fileNames, optionalRelativePath)
 
 #Example
 
-collection$add("main.cpp", "cpp/src/")
+mainFile <- collection$create("main.cpp", "cpp/src/")
+fileList <- collection$create(c("main.cpp", lib.dll), "cpp/src/")
 
-#or
+--------------------------------------------------------------------------------------------------------------------------------
+
+#Add existing ArvadosFile or Subcollection to a collection
 
 folder <- Subcollection$new("src")
-file <- ArvadosFile$new("main.cpp")
+file   <- ArvadosFile$new("main.cpp")
 folder$add(file)
 
 collection$add(folder, "cpp")
 
-#Both examples will add file "main.cpp" in "./cpp/src/" folder if folder exists.
+#This examples will add file "main.cpp" in "./cpp/src/" folder if folder exists.
 #If subcollection contains more files or folders they will be added recursively.
 
-#You can also add multiple files
-
-collection$add(c("path/to/my/file.cpp", "path/to/other/file.cpp"))
-
 --------------------------------------------------------------------------------------------------------------------------------
 
 #Write to existing file (Override current content of the file)
@@ -143,57 +156,64 @@ arvadosFile$write("This is new file content")
 
 #Delete file from a collection
 
-file <- collection$get("location/to/my/file.cpp")
-
-file$removeFromCollection()
-
-#Or
+collection$remove("location/to/my/file.cpp")
 
-collection$remove(file)
-
-#Both examples will remove file "file.cpp" from a collection
+#You can remove both Subcollection and ArvadosFile
 #If subcollection contains more files or folders they will be removed recursively.
 
 #You can also remove multiple files
 
 collection$remove(c("path/to/my/file.cpp", "path/to/other/file.cpp"))
 
+#Delete file or folder from a Subcollection
+
+subcollection <- collection$get("mySubcollection/")
+subcollection$remove("fileInsideSubcollection.exe")
+subcollection$remove("folderInsideSubcollection/")
+
 --------------------------------------------------------------------------------------------------------------------------------
 
 #Move file or folder inside collection
 
-file <- collection$get("location/to/my/file.cpp")
+#Directley from collection
 
-file$move("destination/file.cpp")
+collection$move("folder/file.cpp", "file.cpp")
 
-#Or subcollections
+#Or from file
 
-subcollection <- collection$get("location/to/folder")
+file <- collection$get("location/to/my/file.cpp")
+file$move("newDestination/file.cpp")
 
-subcollection$move("destination/folder")
+#Or from subcollection
 
-#Make sure to include folder name in destination
-#For example
-#file$move("destination/") will not work
+subcollection <- collection$get("location/to/folder")
+subcollection$move("newDestination/folder")
 
---------------------------------------------------------------------------------------------------------------------------------
+#Make sure to include new file name in destination
+#In second example file$move("newDestination/") will not work
 
+--------------------------------------------------------------------------------------------------------------------------------
+WORKING WITH ARVADOS PROJECTS
 --------------------------------------------------------------------------------------------------------------------------------
 
-# Working with projects:
-
-#Get project:
+#Get a project:
 
 arv$getProject("uuid")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
 #List projects:
-projects <- arv$listProjects(list(list("uuid", "=", "aaaaa-j7d0g-ccccccccccccccc")), limit = 10, offset = 2)
+
+projects <- arv$listProjects(list(list("owner_uuid", "=", "aaaaa-j7d0g-ccccccccccccccc"))) # list subprojects of a project
+arv$listProjects(list(list("name","like","Example%"))) # list projects which have names beginning with Example
+
+#Next example will list all projects even when the number of items is greater than maximum API limit
+
+collectionList <- arv$listAllProjects(list(list("name","like","Example%")))
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Delete project:
+#Delete project:
 
 deletedProject <- arv$deleteProject("uuid")
 
@@ -201,12 +221,21 @@ deletedProject <- arv$deleteProject("uuid")
 
 #Update project:
 
-updatedProject <- arv$updateProject("uuid", list(name = "new_name", description = "new_desciption"))
+updatedProject <- arv$updateProject("uuid", list(name = "new_name", description = "new description"))
 
 --------------------------------------------------------------------------------------------------------------------------------
 
 #Create project:
 
-createdProject <- arv$createProject(list(name = "project_name", description = "project_desciption"))
+createdProject <- arv$createProject(list(name = "project_name", description = "project description"))
+
 
 --------------------------------------------------------------------------------------------------------------------------------
+BUILDING THE ARVADOS SDK TARBALL
+--------------------------------------------------------------------------------------------------------------------------------
+
+
+cd arvados/sdk
+R CMD build R
+
+This will create a tarball of the Arvados package in the current directory.