R如何使用R从谷歌驱动器中读取文件

sev*_*ven 7 url r dataset

我想在R中读取来自谷歌驱动器的数据集,如 屏幕截图所示.

也不

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)
Run Code Online (Sandbox Code Playgroud)

也不

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

作品.

我一直在研究这个问题好几个小时.任何提示或解决方案都会非常感激.

Wei*_*ong 7

尝试

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>
Run Code Online (Sandbox Code Playgroud)

该URL应与您用于使用浏览器下载文件的URL相对应.

正如@ Mako212所指出的,你也可以使用的googledrive包装,取代drive_downloaddownload.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 = ";")
Run Code Online (Sandbox Code Playgroud)


dra*_*doc 6

  • 谷歌驱动器共享链接不是直接文件链接,因此1. download.file 2. RCurl first method in accepted answer仅下载显示文件的网页,而不下载文件本身。您可以编辑下载的文件并看到它是一个 html 文件。

  • 您可以使用此找到文件的实际直接链接。通过直接链接,所有常规下载方法都可以使用。

  • 有关获取直接链接或下载它的非常详细的讨论,请参阅此问题

  • Google Drive api 要求客户端登录,因此 googledrive 包还会要求您登录 google(如果尚未登录)。