11876: Add ArvadosFile$connection and $flush
authorPeter Amstutz <pamstutz@veritasgenetics.com>
Thu, 11 Jan 2018 22:02:12 +0000 (17:02 -0500)
committerPeter Amstutz <pamstutz@veritasgenetics.com>
Thu, 11 Jan 2018 22:02:12 +0000 (17:02 -0500)
Convenience functions for working with standard R functions like
read.table() and write.table().

Update documentation

Arvados-DCO-1.1-Signed-off-by: Peter Amstutz <pamstutz@veritasgenetics.com>

sdk/R/R/ArvadosFile.R
sdk/R/README

index 8c2e9e2547d2c071a8515501555324a4c82eb780..2da3db3b39792fa5427dccea7e71b665126be7e3 100644 (file)
@@ -88,18 +88,18 @@ ArvadosFile <- R6::R6Class(
 
             if(length > 0)
                 range = paste0(range, offset + length - 1)
-            
+
             fileURL = paste0(private$collection$api$getWebDavHostName(),
                              "c=", private$collection$uuid, "/", self$getRelativePath());
 
             if(offset == 0 && length == 0)
             {
                 headers <- list(Authorization = paste("OAuth2",
-                                                      private$collection$api$getToken())) 
+                                                      private$collection$api$getToken()))
             }
             else
             {
-                headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()), 
+                headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
                                 Range = range)
             }
 
@@ -111,15 +111,31 @@ ArvadosFile <- R6::R6Class(
             parsedServerResponse <- httr::content(serverResponse, contentType)
             parsedServerResponse
         },
-        
+
+       connection = function(rw)
+       {
+         if (rw == "r") {
+           return(textConnection(self$read("text")))
+         } else if (rw == "w") {
+           private$buffer <- textConnection(NULL, "w")
+           return(private$buffer)
+         }
+       },
+
+       flush = function() {
+         v <- textConnectionValue(private$buffer)
+         close(private$buffer)
+         self$write(paste(v, collapse='\n'))
+       },
+
         write = function(content, contentType = "text/html")
         {
             if(is.null(private$collection))
                 stop("ArvadosFile doesn't belong to any collection.")
 
-            fileURL = paste0(private$collection$api$getWebDavHostName(), 
+            fileURL = paste0(private$collection$api$getWebDavHostName(),
                              "c=", private$collection$uuid, "/", self$getRelativePath());
-            headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()), 
+            headers <- list(Authorization = paste("OAuth2", private$collection$api$getToken()),
                             "Content-Type" = contentType)
             body <- content
 
@@ -193,8 +209,9 @@ ArvadosFile <- R6::R6Class(
         parent     = NULL,
         collection = NULL,
         http       = NULL,
-        httpParser = NULL
+        httpParser = NULL,
+        buffer     = NULL
     ),
-    
+
     cloneable = FALSE
 )
index 2e1a4df322188afad38f74e912f99406235f88bd..7f984fe6f9f8a0b9262db3def73cdd0ab80259eb 100644 (file)
@@ -72,9 +72,9 @@ collection <- Collection$new(arv, "uuid")
 
 --------------------------------------------------------------------------------------------------------------------------------
 
-#Get file/folder content as character vector
+#Get list of files
 
-collection$getFileContent()
+collection$getFileListing()
 
 --------------------------------------------------------------------------------------------------------------------------------
 
@@ -88,6 +88,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()