rrprofile

Installing packages from RProfile.site file


I asked this question earlier, but am still unable to get it working. I am trying to install custom packages upon starting R. A lot of the code that is written by us right now is available for editing to the users. To try and protect the code, I am packaging the production level code and having the users install it on their machine during start up.

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

Here is the code I have 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 starting R (without changing the site file), it installs the package perfectly fine and moves on. However, when I try to do it through the RProfile file, that's when it creates the problems.

Last time I tried resolving this issue (https://stackoverflow.com/questions/10610067/installing-packages-upon-starting-r-session), I thought Justin's suggestion of using the if statement check for packages would fix the problem. But this only seems to solve the problem for packages I install from CRAN and not custom packages.

Any help on this matter would be greatly appreciated!


Solution

  • I don't understand why you'd want to do this. Just have them point their .libPaths to the same place. i.e. instead of install.packages(...), just add a line in Rprofile.site that says

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

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

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

    Among other things, the --vanilla flag causes R to be started without using the Rprofile.site file (Your problem is that the Rprofile.site file is being read when R starts, but the Rprofile.site file tells R to install a package which requires starting R, which in turns reads your Rprofile.site file... etc.). Presumably, R --no-site-file INSTALL customPackage would also work.

    Edit

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

    system("Rcmd --vanilla INSTALL customPackage")