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)
You can readRDS (i.e. read RDS file) only from your current working directory. To check your current work directory use command getwd() . Show activity on this post. Set up your working directory to the folder where you have downloaded the Chicago file.
Rds files store a single R object. According to R documentation: These functions provide the means to save a single R object to a connection (typically a file) and to restore the object, quite possibly under a different name.
If you cannot open your RDS file correctly, try to right-click or long-press the file. Then click "Open with" and choose an application. You can also display a RDS file directly in the browser: Just drag the file onto this browser window and drop it.
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" ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With