R 使用 Google Drive 的 httr 读取 rds 文件

问题描述 投票:0回答:1

所以我想从 Google Drive 读取 rds 文件。

我知道可以使用

googledrive
包,但是我不想下载 rds 文件并导入它。我想将它直接加载到环境中。

因此,对于谷歌表格,可以使用

rangespread()
形式的
googlesheets4
包来实现。执行此操作的主要代码是 httr 调用:

library(googledrive)
library(googlesheets4)
library(httr)
library(readr)

url <- "https://docs.google.com/spreadsheets/d/1nIi-N7_Past8YlirLdOI9ktV3BBvPraGHZZprElQREc/export?format=csv"

response <- GET(url)
df <- read_csv(content(response, type = "raw"))

对于 rds 文件,我将其更改为:

url <- "https://drive.google.com/u/2/uc?id=1ZvCT4Ook2yUkPQj4-w7qAfGr7k4ZWt1p&export=download"

response <- GET(url)
df <- readRDS(content(response, type = "raw"))

但得到这样的回应:

Error in readRDS(content(response)) : bad 'file' argument

任何帮助都会很棒!

r google-sheets httr
1个回答
0
投票

readRDS
无法读取原始字节,它需要从连接读取。而且您还必须解压缩数据。这应该有效

response <- httr::GET(url)
cn <- rawConnection(httr::content(response, type = "raw"))
df <- readRDS(gzcon(cn))
close(cn)
© www.soinside.com 2019 - 2024. All rights reserved.