The googledrive
package has the drive_upload
function to upload a file to Google drive, but how does one go about uploading an entire directory tree?
Following @DalmTo 's suggestion, I wrote a method to recursively upload a directory to Google drive:
#' Recursively upload a directory to google drive.
#'
#' @param dir Path to the local directory to upload.
#' @param path Specifies the target destination for the new directory on Google
#' Drive. Defaults to "My Drive" root folder.
#' @param name New directory name. Defaults to `basename(dir)`.
drive_upload_dir <-
function(
dir,
path = NULL,
name = NULL) {
if (is.null(name))
name <- basename(dir)
assertthat::assert_that(!is.na(file.info(dir)$isdir), file.info(dir)$isdir, msg = "dir must be a directory")
sub_path <- googledrive::drive_mkdir(name, path)
for (p in list.files(dir, full.names = TRUE)) {
if (file.info(p)$isdir) {
drive_upload_dir(p, path = sub_path)
} else {
googledrive::drive_upload(p, path = sub_path)
}
}
return(sub_path)
}
Note that for directory trees with a large number of files it can be very slow. For my uses, it was more expedient to make a tar archive of the directory and drive_upload
the whole archive.