Installing R packages locally: Difference between revisions

From HPCwiki
Jump to navigation Jump to search
No edit summary
 
(11 intermediate revisions by 5 users not shown)
Line 6: Line 6:


You can use several library trees of add-on packages. The easiest way to tell R to use these via a 'dotfile' by creating the following file '$HOME/.Renviron' (watch the quotes and ~ character):
You can use several library trees of add-on packages. The easiest way to tell R to use these via a 'dotfile' by creating the following file '$HOME/.Renviron' (watch the quotes and ~ character):
<source lang='bash'>
<pre>
   R_LIBS_USER="~/R/library"
   R_LIBS_USER="~/R/library"
</source>
</pre>
This specifies a keyword (<code>R_LIBS_USER</code>) which points to a colon-separated list of directories at which R library trees are rooted. You do not have to specify the default tree for R packages.
This specifies a keyword (<code>R_LIBS_USER</code>) which points to a colon-separated list of directories at which R library trees are rooted. You do not have to specify the default tree for R packages.


If necessary, create a place for your R libraries
If necessary, create a place for your R libraries
<source lang='bash'>
<pre>
   mkdir ~/R ~/R/library        # Only need do this once
   mkdir ~/R ~/R/library        # Only need do this once
</source>
</pre>
Set your R library path
Set your R library path
<source lang='bash'>
<pre>
   echo 'R_LIBS_USER="~/R/library"' >  $HOME/.Renviron
   echo 'R_LIBS_USER="~/R/library"' >  $HOME/.Renviron
</source>
</pre>
== Installing to a local library search location ==
== Installing to a local library search location ==


Installation is dead easy. Start up R and tell R to fetch your package from CRAN, compile whatever needs compiling and set everything else up.
Start up R:


Beware - each package will only work for the platform (i.e. Linux or Solaris) where you installed it. If you want a package on both Linux and Solaris, you'll need to install it in different directories for each system type.
<pre>
 
<source lang='bash'>
   R                    # Invoke R
   R                    # Invoke R
</source>
</pre>
Then, from the R environment, install the packages you require while pointing at the root R-package directory of choice:
Then, from the R environment, install the packages you require while pointing at the root R-package directory of choice. This example will install from CRAN.
<source lang='rsplus'>
<pre>
install.packages("name-of-your-package",lib="~/R/library")
install.packages("name-of-your-package",lib="~/R/library")
</source>
</pre>
 
== Installing and using a local library from a JuPyteR notebook ==
Click on the image.
[[File:R_libpaths.png|thumb]]
== See also ==
== See also ==
* [[Control_R_environment_using_modules | Control R environment on the cluster using modules]]
* [[Parallel_R_code_on_SLURM | Using parallel R workloads under SLURM]]
* [[Bioinformatics_tips_tricks_workflows | Bioinformatics tips, tricks, and workflows]]


== External links ==
== External links ==
[http://csg.sph.umich.edu/docs/R/localpackages.html source for material for this aricle]
[http://csg.sph.umich.edu/docs/R/localpackages.html source for material for this aricle]
[http://cran-mirror.cs.uu.nl CRAN homepage]
[http://en.wikipedia.org/wiki/R_(programming_language) R article from Wikipedia]

Latest revision as of 14:49, 9 April 2024


Specifying a local library search location

Specify a local library search location.

You can use several library trees of add-on packages. The easiest way to tell R to use these via a 'dotfile' by creating the following file '$HOME/.Renviron' (watch the quotes and ~ character):

  R_LIBS_USER="~/R/library"

This specifies a keyword (R_LIBS_USER) which points to a colon-separated list of directories at which R library trees are rooted. You do not have to specify the default tree for R packages.

If necessary, create a place for your R libraries

  mkdir ~/R ~/R/library         # Only need do this once

Set your R library path

  echo 'R_LIBS_USER="~/R/library"' >  $HOME/.Renviron

Installing to a local library search location

Start up R:

  R                     # Invoke R

Then, from the R environment, install the packages you require while pointing at the root R-package directory of choice. This example will install from CRAN.

install.packages("name-of-your-package",lib="~/R/library")

Installing and using a local library from a JuPyteR notebook

Click on the image.

R libpaths.png

See also

External links

source for material for this aricle

CRAN homepage

R article from Wikipedia