user3504322
user3504322

Reputation: 555

How do I download files using R from a specific folder in Google drive?

This is NOT a duplicate. I am trying to retrieve files in a specific folder.

I want this statement f <- drive_find(n_max=30) to return only the files in a specific directory. How do I point it to a specific directory?

For more detail on what I am doing, see below.

I am using R and can download files with the following code:

 install.packages("googledrive")
 #load googledrive
 library("googledrive")
drive_download(file = as_id(drive_find(pattern = "abc.xlsx", n_max = 30)$id), 
                      path = "/Users/me/Desktop/abc.xlsx")

But I want to download only specific files in a specific directory and I don't know how to specify that directory specifically and exclusively.

I have tried drive_get and drive_download but am unable to specify a specific directory.

f <- drive_find(n_max = 30) # this gives me a list of files

for (i in 1:nrow(f)){

  d_path <- f$name[i]

  drive_download(file = as_id(drive_find(pattern = f$name[i], n_max = 30)$id), 
                 path = paste("/Users/me/Desktop/Gdrive/", d_path, sep =""))

}

The problem is that the statement f <- drive_find(n_max = 30) gives me a list that includes folders and files I do not want. So I need to specify the exact directory to look in. How do I do that?

Upvotes: 2

Views: 1631

Answers (1)

Christina_DJV
Christina_DJV

Reputation: 21

On a previous thread the folder ID was used to specify the exact folder location. Unless you have them in the "drive" folder, this could help. R How to read a file from google drive using R

The code provided there

library(googledrive)
temp <- tempfile(fileext = ".zip")
dl <- drive_download(as_id("1AiZda_1-2nwrxI8fLD0Y6e5rTg7aocv0"), 
path = temp, overwrite = TRUE)
out <- unzip(temp, exdir = tempdir())
bank <- read.csv(out[14], sep = ";")

You can find the file ID when you get a shareable link in google drive.It will look something like this: https://drive.google.com/file/d/"file ID you need"/view?usp=sharing

Upvotes: 2

Related Questions