Search code examples
rgoogle-sheetshttr

R read in a rds file using httr of Google Drive and authenticating


So I would like to read in a rds file from Google Drive.

I know its possible with the googledrive package, however I dont want to download the rds file and import it. I would like to load it directly into the environment.

So with a google sheets this is possible using rangespread() form the googlesheets4 package. The main code which does this is a httr call:

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

For the rds file I changed it to:

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

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

But get this response:

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

Any help would be great!

UPDATE

Using a google service account token I still get a Content-Type: text/html; charset=utf-8 response which is also smaller than the original file

# service account token
drive_auth(path =  "....json")

#google4sheets authentication
gs4_auth(token = drive_token())

token <- gs4_token()
response <- httr::GET(url, config = token)

Solution

  • readRDS can't read the raw bytes, it needs to read from a connection. And you also have to uncompress the data. This should work

    response <- httr::GET(url)
    cn <- rawConnection(httr::content(response, type = "raw"))
    df <- readRDS(gzcon(cn))
    close(cn)