stevec
stevec

Reputation: 52198

Load an RDS file from the web (i.e. a url) directly into R?

read.csv() has the excellent ability to read directly form a url. readRDS() does not.

I want to move an RDS file from the internet to my R environment. I see a couple of ways:

Method 1

This method clutters the working directory with the downloaded file

myurl <- "https://collidr-api.s3-ap-southeast-2.amazonaws.com/pfd.RDS"

download.file(myurl, "file.RDS")
# read into R
readRDS("file.RDS")

# Or similarly
curl::curl_download(myurl, "file.RDS")
# read into R
readRDS("file.RDS")

Method 2

So we could instead save to a tempfile

RDS_from_web <- function(url) {
  
  tempFile_location<- tempfile()
  download.file(myurl, tempFile_location)
  b <- readRDS(tempFile_location)
  file.remove(tempFile_location)
  b
}

b <- RDS_from_web(myurl)

Question

Is there a simpler/better way to read in an RDS file directly from the web? (preferably a function that already exists in base R)

Upvotes: 8

Views: 3945

Answers (1)

Marco Sandri
Marco Sandri

Reputation: 24252

You can use:

b <- readRDS(url("https://collidr-api.s3-ap-southeast-2.amazonaws.com/pfd.RDS","rb"))

str(b)
# 'data.frame':   649346 obs. of  2 variables:
#  $ package_names : chr  "A3" "A3" "A3" "A3" ...
#  $ function_names: chr  "a" "a3" "A3-package" "a3.base" ...

Upvotes: 21

Related Questions