'RStudioServer - load/upgrade preferred set of packages to default sys path (/opt/R/4.2.0/lib/R/library) from R shell

Though an 'old guy' in unix world, I've little experience with RStudioServer and R, acting here as an 'admin' for a shared server for statisticians on a cancer research study. And my unix 'admin' experience is ... rusty !

I have a list of about 100 packages used by our senior Statistician on the study, and would prefer to load (AND UPDATE PERIODICALLY) them from an R shell directly (vs inside RStudioServer). I think using the default system location to add them ( /opt/R/4.2.0/lib/R/library) is the way to go. The RStudioServer setup, i think, still allows user to load their own (which is fine by me), but I thought having a default group loaded per our Statistician's ask... is wise.

The commands I was given which load these packages have the syntax below (only including a few lines of the 90 line .sh script) - they seem to install if the package doesnt already exist.

SO... I wonder if I set an environment (or ?) variable properly, then run R (as root ?), issue all these commands... would the libraries be placed appropriately in the library folder(for 4.2) that I set in the env variable ? (this example, it'd be /opt/R/4.2.0/lib/R/library ) ??

Since this is Linux, we would want sources when approprite, which i THINK is default loading for this os.

If there is a better/easier way to do this, I'm all ears !

Thanks in advance for any thoughts you have...


r

if (!requireNamespace("markdown", quietly = TRUE)) install.packages("markdown")
if (!requireNamespace("devtools", quietly = TRUE)) install.packages("devtools")
# graphical/table output
if (!requireNamespace("igraph", quietly = TRUE)) install.packages("igraph")
if (!requireNamespace("ggplot2", quietly = TRUE)) install.packages("ggplot2")
# advanced regression
if (!requireNamespace("glmnet", quietly = TRUE)) install.packages("glmnet")
if (!requireNamespace("sm", quietly = TRUE)) install.packages("sm")

RStudioServer packageStatus()

environment: CentOS 8 RStudio Server 1.4.1717-3 R 4.2 (4.1.3, 3.6.3)



Solution 1:[1]

These are a lot of questions. Here's an attempt at some answers

I have a list of about 100 packages used by our senior Statistician on the study, and would prefer to load (AND UPDATE PERIODICALLY) them from an R shell directly (vs inside RStudioServer). I think using the default system location to add them ( /opt/R/4.2.0/lib/R/library) is the way to go.

We customize it to a shared network mount, but you certainly don't have to. If you ever need to, be aware of:

  • The R_PROFILE_USER env var, which can contain a path to a script running very early in every R startup sequence
  • The R_LIBS_USER env var, which can contain a new default package folder
  • The .libPaths R function, that can customize the used package folders from within an R script - either the one in R_PROFILE_USER or the per-user .Rprofile.

The RStudioServer setup, i think, still allows user to load their own (which is fine by me), but I thought having a default group loaded per our Statistician's ask... is wise.

I agree.

The commands I was given which load these packages have the syntax below (only including a few lines of the 90 line .sh script) - they seem to install if the package doesnt already exist.

Not sure I understand. The commands you pasted are R, not sh. Either place them in each of your users' .Rprofile, or in a centrally-accessible script and make R_PROFILE_USER include its path.

Also, about periodically updating - here's a simplified version of an R script I use:

instpk <- as.data.frame(installed.packages())
oldpk <- as.data.frame(old.packages(checkBuilt=TRUE))

# base/recommended packages are best installed alongside R:
# (https://stackoverflow.com/a/9705725/89706)
base.rec.packages <- instpk[!is.na(instpk$Priority),]$Package
updatePk <- setdiff(oldpk$Package, base.rec.packages)

update.packages(ask=FALSE, oldPkgs=updatePk)

(I customize some folders and env vars too, but maybe you won't need to).

HTH.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ofek Shilon