简体   繁体   中英

load(url(....)) vs download.file in R. Why the latter doesn't work when downloading .Rdata files?

I'm reading some of my old R notes and I'm faced with something that make things confusing to me. Here you can find a dummy database with .Rdata extension.

When I use load + url the database is downloaded and read properly:

load(url("http://d396qusza40orc.cloudfront.net/statistics%2Fproject%2Fgss.Rdata"))
str(gss)

But when I try to accomplish same thing using download.file and then readRDS I got an error:

URLtest <- "http://d396qusza40orc.cloudfront.net/statistics%2Fproject%2Fgss.Rdata"
download.file(URLtest,"myfile", method="curl")
readRDS("myfile)
Error in readRDS ("myfile"): unknown input format

The reason I called readRDS can be found here .

So if load is not advised because its side effects (such as silently overwriting files) why does it work in cases like this?

Any reference and comment will be much appreciated.

If the file was created with save() , then you need to use load() to open it. If it was created with saveRDS() , then you can use readRDS() to load it. You cannot open the same file with both load() and readRDS() -- those are two different file formats. It is customary to use an .Rdata extension for files created with save() and .rds for files created with saveRDS() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM