36

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.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
rlh2
  • 1,039
  • 2
  • 9
  • 16
  • Please add a bit more info on operating system and why exactly you want to install a package from source. If you're not familiar with R, please read the recommended manuals. – Joris Meys Mar 17 '11 at 14:21
  • Possible duplicate of [How do I load a package without installing it in R?](http://stackoverflow.com/questions/5484903/how-do-i-load-a-package-without-installing-it-in-r) – f3lix Jan 19 '12 at 10:21

6 Answers6

55

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')
f3lix
  • 29,500
  • 10
  • 66
  • 86
  • 2
    `ERROR: cannot install to srcdir for package ‘RPostgreSQL’ * removing ‘/usr/local/lib/R/site-library/RPostgreSQL’ Error: Command failed (1)` :-( – vagabond Jul 18 '16 at 01:53
9

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.

Joris Meys
  • 106,551
  • 31
  • 221
  • 263
7

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")
Dan Gerlanc
  • 417
  • 2
  • 8
2

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

tim_yates
  • 167,322
  • 27
  • 342
  • 338
  • I tried that, but I got an "ERROR: compilation failed for package 'MSBVAR'". – rlh2 Mar 17 '11 at 13:52
  • 1
    Maybe if you paste the error into your question you would get more useful answers? I'm guessing it has some C compilation that needs doing. You're either going to need to download a binary package for your specific platform, or get a compiler set up correctly. – tim_yates Mar 17 '11 at 13:55
  • Does the binary package from here not work? http://cran.r-project.org/web/packages/MSBVAR/index.html – tim_yates Mar 17 '11 at 13:56
  • If compilation failed then there should be an earlier error saying why. Maybe you don't have a C or Fortran compiler installed. What OS are you using? – Spacedman Mar 17 '11 at 14:16
  • my OS is: sparc-sun-solaris2.10. Before the error message I also get an "sh: make: not found" message. I tried loading all of the dependencies, and then using install.packages on the tar.gz file. I guess at this point, I will just have to wait for IT to install it... – rlh2 Mar 21 '11 at 12:39
1

I run into a similar problem and to load an R Package from a custom directory you can also use the lib.loc option of the library() function.

In my case, I had to install the package to a custom directory due to admin restrictions. Below, I tried to install it in the default library directory, but I was forced to change that to another directory:

install.packages('data.table')
Warning in install.packages("data.table") :
  'lib = "/software/all/R/4.1.2-foss-2021b/lib64/R/library"' is not writable
Would you like to use a personal library instead? (yes/No/cancel) yes
Would you like to create a personal library
‘~/R/x86_64-pc-linux-gnu-library/4.1’
to install packages into? (yes/No/cancel) yes

So I ended up with installing the package in ~/R/x86_64-pc-linux-gnu-library/4.1 This will depend from case to case, so pay attention to the messages given by install.packages('data.table') so that you get the right path.

Then to load that package, I had to do just this:

library(data.table, lib.loc = '~/R/x86_64-pc-linux-gnu-library/4.1')

This option does not require any other dependencies.

Valentin_Ștefan
  • 6,130
  • 2
  • 45
  • 68
0

you dont need to unzip or untar
just give this command in command prompt and it will unzip into appropriate place

R CMD INSTALL [options] [l-lib] pkgs.tar.gz

as explained here

then you can use it in R by library(the_pkg)

svural
  • 961
  • 1
  • 9
  • 17