Last updated Apr 14, 2021 2:55 PM

Maintaining a personal R library

You can install packages locally within your home directory using R. If you need a package quickly, on a one-time basis, or if the package is particularly specialized, you can install it locally using a personal library. 

Installing random

The main function to install packages in R is “install.packages()”

It takes a vector of names of the packages you wish to install, and optionally, a destination library. It then downloads the packages from the repositories and installs them.

*If you do not specify a library to use, it will default to the first directory located in .libPaths() and a message if there is more than one.

For installs from a repository, R will attempt to install the packages in an order that respects their dependencies. This assumes that all the entries in lib are on the default library path for installs.

*Before installing packages, run ‘update.packages’ to ensure that any dependencies that are already installed are up to date to their latest versions.

To begin, type "R" into your terminal, after connecting to fiji-viz, to get into the R interpreter/shell.

        [bit-local@fiji-viz ~]$ R
    

After doing so, you should see a message like this one:

        R version 3.4.1 (2017-06-30) -- "Single Candle"
Copyright (C) 2017 The R Foundation for Statistical Computing
Platform: x86_64-redhat-linux-gnu (64-bit)

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
    

To install random, you want to use the same function mentioned above, install.packages()

        > install.packages("random")
    

You will then be asked if you would like to use a personal library instead, enter 'y' for yes. After, you will be asked if you'd like to create a personal library, followed by the name of the library. Enter 'y' for yes. 

        Installing package into ‘/usr/lib64/R/library’
(as ‘lib’ is unspecified)
Warning in install.packages("random") :
 'lib = "/usr/lib64/R/library"' is not writable
Would you like to use a personal library instead? (y/n) y
Would you like to create a personal library
~/R/x86_64-redhat-linux-gnu-library/3.4
to install packages into? (y/n) y
    

Then, it will ask for you to select a CRAN mirror for the session. You could use any of the ones listed, I chose to use 51: USA (CA 1) [https]. After you select a CRAN mirror, the installation will begin and complete. The returned message will tell you where the packages are stored in.

        trying URL 'https://cran.cnr.berkeley.edu/src/contrib/random_0.2.6.tar.gz'
Content type 'application/x-gzip' length 504266 bytes (492 KB)
==================================================
downloaded 492 KB

* installing *source* package ‘random’ ...
** package ‘random’ successfully unpacked and MD5 sums checked
** R
** inst
** preparing package for lazy loading
** help
*** installing help indices
 converting help for package ‘random’
 finding HTML links ... done
 random html 
** building package indices
** installing vignettes
** testing if installed package can be loaded
* DONE (random)

The downloaded source packages are in
 ‘/tmp/Rtmpk4DoPo/downloaded_packages’
    

To ensure that the package was installed properly, use the library() function with the name of the package as the argument. If it was installed properly, nothing will be returned. 

        > library("random")
    

Fiji
Posted in Category: Cluster Computing