Reading data into R from google drive

I am trying to read some data from google drive using R. I have seen this library called googledrive. With the drive_get I know I can pass the url or using drive_find I can search for a pattern . The issue I have is I will get a new csv file with slight name change(appended date in the file name). So how can I read the latest file only from that particualar drive path. Thank you.

I've done it from Dropbox. You just read in the complete list of files and then use stringr or something to figure out which one you want, then you read it. I can't recall if I did it from googledrive, only googlesheets.

I used this method:
https://shiny.rstudio.com/articles/persistent-data-storage.html#dropbox

# https://github.com/karthik/rdrop2#accessing-dropbox-on-shiny-and-remote-servers
library(rdrop2)
library(stringr)

# folder on the authenticated account
drop_folder <- "bull_data_files" 

# regex pattern to look for in filenames
pattern <- "File_[0-9]{8}_[0-9]{4}EV" 

# check/create dropbox authentication file
if (file.exists("token.rds")) {
  token <- readRDS("token.rds")
} else {
  token <- drop_auth() # launches a login page
  saveRDS(token, "token.rds") # need to do this locally then deploy this file to shiny
}

# check dropbox files
dropfiles <- drop_dir(drop_folder, dtoken = token)$name
bv_file_csv_all <- str_extract(dropfiles, pattern = regex(str_c("^", pattern, ".csv$"), ignore_case = TRUE))

# download and then read latest version
bv_file_csv <- max(bv_file_csv_all, na.rm = TRUE) 
drop_download(str_c(drop_folder, "/", bv_file_csv), progress = FALSE, verbose = FALSE, overwrite = TRUE, dtoken = token)
bv <- suppressMessages(readr::read_csv(bv_file_csv, guess_max = 25000, na = c("", "NA", "NULL")))
1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.