I'm following the instructions in this post to download 2 csv files from my google drive to my computer. Here is the code provided -
library(googledrive)
library(purrr)
## store the URL you have
folder_url <- "https://drive.google.com/drive/folders/0B7tJg2i5HAo2c0VzVFVhLUdQcnM"
## identify this folder on Drive
## let googledrive know this is a file ID or URL, as opposed to file name
folder <- drive_get(as_id(folder_url))
## identify the csv files in that folder
csv_files <- drive_ls(folder, type = "csv")
## download them
walk(csv_files$id, ~ drive_download(as_id(.x)))
The instructions above download the csv files to my "documents" folder. I am trying to download the files to a particular folder on my laptop using a slight modification to the last bit of code
walk(csv_files$id, ~ drive_download(as_id(.x),
path = "../Desktop/data_folder/,
overwrite = TRUE))
Unfortunately, this is saving a single .xlsx file which does not contain any data and cannot be opened. How do I correct the code to save both files to a particular folder?
The problem is that path
parameter wants a full filename, not just a path to a directory. So by default it figures out the filename using goodledrive methods and uses it to create a file on your computer in your current working directory. But if you don't want this default behavior you should also provide the file name. So essentially there are two options:
Set your working directory to the folder you want your files to be downloaded to before running the code you provided. (and then most likely it'll be a good idea to change it back)
Rewrite the script so that path
parameter is built using the googledrive filename. Something like this:
path <- "~/Documents/tmp/data_folder/"
for (i in seq_along(csv_files$name)) {
drive_download(
as_id(csv_files$id[[i]]),
path = file.path(path, csv_files$name[[i]]),
overwrite = TRUE
)
}
Or if you prefer to use walk
:
csv_files %>%
split(csv_files$id) %>%
walk(~drive_download(.$id, path = file.path(path, .$name), overwrite = TRUE))