Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading an R Package from a Custom directory

If I download an "package-name".tar.gz file from CRAN website, gunzip and untar it into a custom directory, how do I load that package from within R? I cannot extract the file in the R installation directory.

like image 316
rlh2 Avatar asked Mar 17 '11 13:03

rlh2


People also ask

How do I install a custom package in R?

To install a R package locally, specify the local directory where you want to install by using the “-l” option in the “R CMD INSTALL” command. For example, to install the R package in the local directory “/usr/me/localR/library”, use the “R CMD INSTALL” as follows.


4 Answers

Try using Hadley Wickham's devtools package, which allows loading packages from a given directory:

library(devtools)

# load package w/o installing
load_all('/some/package/diR')

# or invoke 'R CMD INSTALL'
install('/some/package/diR')
like image 172
f3lix Avatar answered Oct 10 '22 06:10

f3lix


Please add some extra information on the operating system. If you're on windows, you need Rtools ( http://www.murdoch-sutherland.com/Rtools/ ) to build from source. See that website for more information on how to install everything you need.

Even when you're on Linux, simply extracting the package-file doesn't work. There might be underlying C-code (which is the case for the MSBVAR package), and even R code has to be processed in order to be built into a package that can be loaded directly with the library() function.

Plus, you have to take into account that the package you want to install might have dependencies. For the MSBVAR package, these are the packages coda and bit. When building from source, you need to make sure all dependencies are installed as well, or you can get errors.

apart from the R CMD INSTALL you could try from within R :

# from CRAN
install.packages("MSBVAR", type="source")
# from a local file 
install.packages("/my/dir/MSBVAR.tar.gz",repos=NULL, type="source")

or why not just do

# from CRAN
install.packages("MSBVAR")

This works perfectly fine.

like image 38
Joris Meys Avatar answered Oct 10 '22 05:10

Joris Meys


You need to install the package to a directory to which you have permission to read and write. First, download the package to an easily accessible directory. If you're on Linux/Mac, try creating a directory called 'rlib' in your home directory.

cd ~; mkdir rlib
R CMD INSTALL MSBVAR.tar.gz --library=rlib

If you would prefer to install the package from R, do this:

## From CRAN
install.packages("MSBVAR", lib="~/rlib")
like image 30
Dan Gerlanc Avatar answered Oct 10 '22 05:10

Dan Gerlanc


You can't call R CMD INSTALL downloadedpackage.gz?

As I understand it, this should install the package in your user-space if it cannot get write permissions to the R installation folder

like image 2
tim_yates Avatar answered Oct 10 '22 05:10

tim_yates