Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I load a package's data set without installing the package?

Tags:

r

In package ISLR, there is a data set called Default.

I want to use that data set, but the ISLR package is not installed on my machine.

data(Default)
# Warning message:
# In data(Default) : data set ‘Default’ not found
library(ISLR)
# Error in library(ISLR) : there is no package called ‘ISLR’

Since I'll probably never use it again, I don't want to install the package. I thought about reading it from the web, but it's not in the linked web page from the package description.

In general, is there a way to load a data set from a package without installing the package?

like image 624
Rich Scriven Avatar asked Aug 29 '14 01:08

Rich Scriven


People also ask

Do I need to install R packages every time?

You only need to install packages the first time you use R (or after updating to a new version). **R Tip:** You can just type this into the command line of R to install each package. Once a package is installed, you don't have to install it again while using the version of R!

How do I load data into an R library?

Simply check the checkbox next to the package name to load the package and gain access to the datasets. You can also click on the package name and RStudio will open a help file describing the datasets in this package. and press ENTER.

Why do we need packages in R?

The package is an appropriate way to organize the work and share it with others. Typically, a package will include code (not only R code!), documentation for the package and the functions inside, some tests to check everything works as it should, and data sets.

What is the difference between library and package in R?

In R, a package is a collection of R functions, data and compiled code. The location where the packages are stored is called the library. If there is a particular functionality that you require, you can download the package from the appropriate site and it will be stored in your library.


1 Answers

You can do this from within R:

download.file("http://cran.r-project.org/src/contrib/ISLR_1.0.tar.gz",
                dest="ISLR.tar.gz")
untar("ISLR.tar.gz",files="ISLR/data/Default.rda")
L <- load("ISLR/data/Default.rda")
summary(Default)

If you want to keep a copy of the data file:

file.copy("ISLR/data/Default.rda",".")

Clean up:

unlink(c("ISLR.tar.gz","ISLR"),recursive=TRUE)

I'm not sure you can get around having to download the tarball -- in principle you might be able to run untar() directly on a network connection, but I don't think the underlying machinery can actually extract a file without downloading the whole tarball to somewhere on your machine first.

like image 199
Ben Bolker Avatar answered Oct 09 '22 08:10

Ben Bolker