Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R How to read a file from google drive using R

Tags:

url

r

dataset

I would like to read in R a dataset from google drive as the screenshot indicated.

Neither

url <- "https://drive.google.com/file/d/1AiZda_1-2nwrxI8fLD0Y6e5rTg7aocv0"
temp <- tempfile()
download.file(url, temp)
bank <- read.table(unz(temp, "bank-additional.csv"))
unlink(temp)

nor

library(RCurl)
bank_url <- dowload.file(url, "bank-additional.csv", method = 'curl')

works.

I have been working on this for many hours. Any hints or solutions would be really appreciate.

like image 900
seven Avatar asked Dec 17 '17 02:12

seven


People also ask

How do I read Google Drive files in R?

You can do all this with the googledrive package. It's a two-step process where you first find the folder in order to get it's ID, and then query for all files with that folder as the parents. You can find more details on parameters for drive_find at the documentation.

How do I change the working directory in Google Drive R?

Specify your working directory to R: On Windows: File –> Change directory. On MAC OSX: Tools –> Change the working directory.

What can I do with Google Drive API?

The Google Drive API allows you to create apps that leverage Google Drive cloud storage. You can develop applications that integrate with Drive, and create robust functionality in your application using the Drive API.

How to programmatically download files from Google Drive in R?

The googledrive package allows you to programmatically download files from your google drive account. After you load the package, run drive_find () to list files which will give you an opportunity to give R access and, if you wish, to cache your logon credentials for R to use in future sessions.

How to read Google Sheets data in R?

You can read google sheets data in R using the package ‘googlesheets4’. This package will allow you to get into sheets using R. First you need to install the ‘googlesheets4’ package in R and then you have to load the library to proceed further. #Install the required package install.packages('googlesheets4') That’s good.

How to read a CSV file in R?

Use full url to read a csv file from internet. If you are a beginner in R to read CSV/Excel file and do dataframe operations like select, filter, visualize data I will suggest you to see this R read csv & analysis for beginners. CSV stands for Comma Seperated Values. A CSV file is used to store data. It is a plain text file with .csv extension.

How to find the parent of a file using Google Drive?

Google drive api require client to sign in, so googledrive package also ask you to sign in google if not already signed in. You can do all this with the googledrive package. It's a two-step process where you first find the folder in order to get it's ID, and then query for all files with that folder as the parents.


2 Answers

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 = ";")
like image 140
Weihuang Wong Avatar answered Oct 13 '22 06:10

Weihuang Wong


  • The google drive share link is not the direct file link, so 1. download.file 2. RCurl first method in accepted answer only download the web page showing the file, not file itself. You can edit the downloaded file and see it's a html file.

  • You can find out the actual direct link to file with this. With the direct link all the regular download methods will work.

  • For very detailed discussions about getting the direct link or downloading it, see this question.

  • Google drive api require client to sign in, so googledrive package also ask you to sign in google if not already signed in.

like image 37
dracodoc Avatar answered Oct 13 '22 06:10

dracodoc