Install packages from RProfile.site

I asked this question before, but still can't get it to work. I try to install custom packages when I start R. Most of the code we wrote right now is editable for users. To try to protect the code, I pack the production level code and install it on my machine during startup.

However, when I try to install packages in the RProfile.site file, the program goes into a loop, and R constantly runs over and over again. I noticed that the lock file for the package is created along with the package in the library folder inside R.

Here is the code that I added to the site file:

if(length(grep("customPackage", installed.packages()[,1]))==0) { install.packages("customPackage", repos=NULL, type="source") } 

When I try to run this code after running R (without changing the site file), it installs the package perfectly and moves on. However, when I try to do this through an RProfile, this creates problems.

The last time I tried to solve this problem ( https://stackoverflow.com/a/360560/60 ), I thought the Justin suggestion about using if statement validation for packages would fix the problem. But this only seems to solve the problem for packages that I install from CRAN, and not from custom packages.

Any help on this would be greatly appreciated!

+2
source share
3 answers

I donโ€™t understand why you need it. Just ask them to point their .libPaths to the same place. i.e. instead of install.packages(...) just add a line to Rprofile.site that says

 .libPaths('/path/to/common/libraries') require("commonPackage") 

Another thing you could do is make a system call. I don't know much about installing packages under Windows, but on Unix-like instead of using install.packages you can do something like this:

 system("R --vanilla CMD INSTALL customPackage") 

In addition, the --vanilla flag causes R to start without using the Rprofile.site file (the problem is that the Rprofile.site file is read when R starts, but the Rprofile.site file tells R to install a package that requires R to start, which in turn reads your Rprofile.site ... file, etc.). Presumably, R --no-site-file INSTALL customPackage will also work.

Edit

After consulting this SO answer , it looks like you could do something similar on Windows (assuming you installed Rtools ), although I haven't tested it.

 system("Rcmd --vanilla INSTALL customPackage") 
+4
source

You can use the function below to install packages (s) without rebooting .Rprofile :

 surround <- function(x, with) { paste0(with, x, with) } peq <- function(x, y) paste(x, y, sep = " = ") install.packages.vanilla <- function(pkgs, ...){ arguments <- as.list(match.call())[-1] if(!"repos" %in% names(arguments)){ arguments$repos <- getOption("repos") } names_args <- names(arguments) installArgs <- purrr::map_chr(seq_along(arguments), function(i){ value <- arguments[[i]] peq(names_args[i], ifelse(length(value)<2, deparse(value), as.character(list(value)))) }) installArgs <- stringr::str_replace_all(installArgs, "\"", "'") installCmd <- paste0("utils::install.packages(", paste(installArgs, collapse = ", "), ")") fullCmd <- paste( surround(file.path(R.home("bin"), "R"), with = "\""), "--vanilla", "--slave", "-e", surround(installCmd, with = "\"") ) system(fullCmd) return(invisible()) } 
0
source

As GSee said, the problem is that install.packages starts R CMD INSTALL , which starts a new R process that reads the Rprofile.site file, which leads to a loop. Two ways to break the loop of evil:

  1. Verify that R is running interactively:

     if (interactive() && length(grep("customPackage", installed.packages()[,1]))==0) { install.packages("customPackage", repos=NULL, type="source") } 

    interactive() is FALSE when R CMD running, so this breaks the loop.

  2. Set the R_PROFILE environment R_PROFILE empty or invalid so that you do not have the R CMD session in the R CMD . Initialization at the beginning of the R session says:

    If you want '~ / .Renviron or' ~ / .Rprofile to be ignored by R child processes (for example, performed by checking R CMD and assembling R CMD), set the corresponding environment variable R_ENVIRON_USER or R_PROFILE_USER to (if possible, which is not in Windows) "" or to the name of a nonexistent file.

    Adapting this to Rprofile.site, you can install R_PROFILE in an empty or nonexistent file before calling install.packages . For instance:

     if (length(grep("customPackage", installed.packages()[,1]))==0) { Sys.setenv(R_PROFILE = "/dev/null") install.packages("customPackage", repos=NULL, type="source") Sys.unsetenv("R_PROFILE") } 

    Then, when R restarts to install packages, it reads the empty Rprofile.site file, so you break the loop again.

    If the call to install.packages is in .Rprofile, you can set R_PROFILE_USER in the same way.

The first method is simpler and does not require concern about the possible overwriting of the existing R_PROFILE value.

0
source

All Articles