R如何使用R从谷歌驱动器读取文件 [英] R How to read a file from google drive using R

查看:23
本文介绍了R如何使用R从谷歌驱动器读取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 R 中读取来自 google drive 的数据集作为截图.

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

都没有

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)

也没有

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

有效.

我已经为此工作了好几个小时.任何提示或解决方案将不胜感激.

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

推荐答案

试试

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>

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

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

正如@Mako212 指出的那样,您还可以使用 googledrive 包,将 drive_download 替换为 download.file:

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

这篇关于R如何使用R从谷歌驱动器读取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆