Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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)

like image 376
stevec Avatar asked Sep 08 '19 07:09

stevec


People also ask

How do I open a .RDS file in Linux?

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.

What is an RDS file in R?

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.

How do you open an RDS file?

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.


Video Answer


1 Answers

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" ...
like image 199
Marco Sandri Avatar answered Nov 15 '22 02:11

Marco Sandri