R How to Read a File from Google Drive Using R

R How to read a file from google drive using R

Try

temp <- tempfile(fileext = ".zip")
download.file("https://drive.google.com/uc?authuser=0&id=1AiZda_1-2nwrxI8fLD0Y6e5rTg7aocv0&export=download",
temp)
out <- unzip(temp, exdir = tempdir())
bank <- read.csv(out[14], sep = ";")
str(bank)
# 'data.frame': 4119 obs. of 21 variables:
# $ age : int 30 39 25 38 47 32 32 41 31 35 ...
# $ job : Factor w/ 12 levels "admin.","blue-collar",..: 2 8 8 8 1 8 1 3 8 2 ...
# $ marital : Factor w/ 4 levels "divorced","married",..: 2 3 2 2 2 3 3 2 1 2 ...
# <snip>

The URL should correspond to the URL that you use to download the file using your browser.

As @Mako212 points out, you can also make use of the googledrive package, substituting drive_download for download.file:

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 = ";")

Use R package googledrive to load in R a file from my googledrive

You should be able to do this by:

library(googledrive)
drive_download("~/Work/Data/Project1/filename.xlsx")

The type parameter is only for Google native spreadsheets, and does not apply to raw files.

read file from google drive

I would try to publish the sheet as a CSV file (doc), and then read it from there.

It seems like your file is already published as a CSV. So, this should work. (Note that the URL ends with /pub?output=csv)

read.csv("https://docs.google.com/spreadsheets/d/170235QwbmgQvr0GWmT-8yBsC7Vk6p_dmvYxrZNfsKqk/pub?output=csv")

Reading data into R from google drive

The easiest method would be to use the desktop app to sync the files to your hard drive and import it from there. This saves the trouble of authentication etc.

Importing only the latest file based on the name depends on the naming convention used. If the date was formatted as yyyy/mm/dd you could use something like:

library(dplyr)
x <- list.files('drive_path') %>% sort(decreasing = T)
df <- read_csv(x[1])


Related Topics



Leave a reply



Submit