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
How to Save Summary(Lm) to a File
Change Color of Only One Bar in Ggplot
Center-Align Legend Title and Legend Keys in Ggplot2 for Long Legend Titles
Writing to a Dataframe from a For-Loop in R
Creating a Facet_Wrap Plot with Ggplot2 with Different Annotations in Each Plot
Dynamically Converting a List of Excel Files to CSV Files in R
Is There an R Markdown Equivalent to \Sexpr{} in Sweave
R 3.4.1 "Single Candle" Personal Library Path Error: Unable to Create 'Na'
Reversed Order After Coord_Flip in R
How to Refer to a Variable Name with Spaces
R Ggplot Barplot; Fill Based on Two Separate Variables
Ggplot2: Line Connecting the Means of Grouped Data
R - Return Position of Element in Matrix
Read.CSV Is Extremely Slow in Reading CSV Files with Large Numbers of Columns
Fast Linear Regression by Group
R Shiny Mouseover Text for Table Columns
Calculating All Distances Between One Point and a Group of Points Efficiently in R