Reputation: 52198
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:
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")
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)
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
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