R setting library path via R_LIBS - r

I have read the R FAQS and other posts but I am a bit confused and would be grateful to know whether I did everything correctly.
In Windows, in order to modify the default library folder I created a file Renviron.site and put inside E:/Programs/R-3.3.0/etc.
The file has only one line saying
R_LIBS=E:/Rlibrary
When I open R and run .libPaths() I see E:/Rlibrary as [1] and the default R library E:/Programs/R-3.3.0/library as [2].
This should mean that from now on all packages I will install will go in E:/Rlibrary but at the same time I will be able to load and use both packages in this folder and those in the default location. Am I correct?

When you load a package via library, it will go through each directory in .libPaths() in turn to find the required package. If the package hasn't been found, you will get an error. This means you can have multiple versions of a package (in different directories), but the package that will be used is determined by the order of .libPaths().
Regarding how .libPaths() is constructed, from ?.R_LIBS
The library search path is initialized at startup from the
environment variable 'R_LIBS' (which should be a colon-separated
list of directories at which R library trees are rooted) followed
by those in environment variable 'R_LIBS_USER'. Only directories
which exist at the time will be included.

Related

R alternative to install.packages() function

Is there any documentation on manually installing a package in a user library when the R.home() path is locked down and incomplete (no etc, no bin, just library?) The system does NOT support shelling out to execute R CMD, which I believe standard R does.
I would like to build existing source packages (from CRAN) and install into a user library directory, so that I can use the library() function and get all the usual namespace and *.Rdx and *.Rdb files.
At the moment, I'm plodding through install.packages, tools::.build_package, and tools:::.install.packages source, using a standard MacOS R and the r source. Hopefully this has been documented in a more user-friendly fashion and my google searches have missed it.
Thanks.
You don't need to use a different install.packages method, rather you only need to specify a writable location for storing packages and give it precedence over the system default one. A simple way to accomplish this is to set an R_LIBS environment variable. For instance, in my .bashrc I have
export R_LIBS='/home/username/.local/lib/R-3.3.3'
Then, by default, all packages are installed here. Further, packages installed both here and the system-wide location will give priority to the ones here when loading.
You can verify that the location is being used by checking .libPaths() in your R session.

Remove path from .libPaths() so just a single non standard path is left as library

I want to have a single library in R, which is not the default.
The idea is, to push the needed Rprofiles or environment variables out to all network computers, such that all use the same R-respository.
I added an environment variable to add the new lib, but I can't figure out how to get rid of the standard library. I don't know how to edit the Rprofile.
> Sys.getenv("R_LIBS_USER")
[1] "X:/R Repository Database"
> Sys.getenv("R_LIBS")
[1] "X:/R Respository Database"
> .libPaths()
[1] "X:/R Repository Database" "C:/ProgramFiles/R/R-3.2.5/library"
You can't change the system setting for the package directory ($R_HOME/library), nor should you. That directory contains the packages that come with R, including the base package, and it's likely that R would fail to start correctly if you tried pointing it elsewhere.
But this is really a distraction. The main sources of incompatibilities come from using different versions of user-contributed packages. Those you can control by having a site-wide package directory, which is what you've done. Incompatibilities due to different versions of system packages are really down to using different versions of R; if you want to avoid those, then install only one R version.

RStudio: using different package versions for each .Rproj

I have a few older R projects I'm working with, which are dependent on several currently deprecated (or heavily modified) packages. In order for everything to work smoothly I use older versions of those packages, which I have saved in another folder and load up manually to %userprofile%\documents\R\win-library\3.3 when necessary. However, this is not convenient, especially if I want to run multiple projects simultaneously, some of which requires the new and updated versions of the packages.
My question - is there a way to specify custom directories for each .Rproj from which it would take and load the libraries?
You can solve this much simpler:
Have a top-level directory for each project, call projA, projB, ...
Within each of these, create a directory libs/, say.
And within each of these directories have a file .Rprofile with a single assignment such as .libPaths("./libs")
Now when you start R in the different project directories, each will a separate library directory preceding the path, allowing you to place per-projects overrides there.
In an nutshell, the approach outlines here allows you to keep the local and modified packages around as you please. (You can even assign common directories via .libPaths() if you so choose.)
The nice things is that this will
work with any R invocation, batch or GUI or RStudio or shiny or ...
does not depend on any other tools, and hence
does not rely on RStudio or .Rprof files -- though you are free to use RStudio as well.
As so often, Base R is there for you.
One option is to use the checkpoint package by Revolution Analytics.
You can indicate for each main R file in a project the date for which you which you wish to load a set of packages. You can read a bit more about it here.
To pull snapshotted packages from a given date from the mirror use getValidSnapshots(mranRootUrl = mranUrl()).
To create a checkpoint:
# Create temporary project and set working directory
example_project <- paste0("~/checkpoint_example_project_", Sys.Date())
dir.create(example_project, recursive = TRUE)
oldwd <- setwd(example_project)
# Write dummy code file to project
cat("library(MASS)", "library(foreach)",
sep="\n",
file="checkpoint_example_code.R")
# Create a checkpoint by specifying a snapshot date
library(checkpoint)
checkpoint("2014-09-17")
# Check that CRAN mirror is set to MRAN snapshot
getOption("repos")
# Check that library path is set to ~/.checkpoint
.libPaths()
# Check which packages are installed in checkpoint library
installed.packages()
# cleanup
unlink(example_project, recursive = TRUE)
setwd(oldwd)

How to properly set up the library directory/path in R

My goal is to define a single path which R will use for installing and searching for libraries. I read that this can be done by changing the Rprofile.site file in the R installation path. I tried two commands there:
.libPaths("D:/RLibrary")
.Library.site <- file.path("D:/RLibrary")
of which I do not fully understand the difference even after reading the help files.
However after starting R, libraries are still looked for in two locations.
.libPaths()
[1] "D:/RLibrary" "C:/Program Files/R/R-3.3.1/library"
Why is this, and how do I change the library path to my desired path only?
I would suggest you don't want a single directory for packages, since a number of base packages come with R. Instead you want a single directory where a user will install packages.
Create a .Renviron file and add the environment variable R_LIBS pointing to the directory you want your packages to end up in. On my machine, I have
# Linux
R_LIBS=/data/Rpackages/
Or if you have Windows something like
# Windows
R_LIBS=C:/R/library
Your .libPaths() would now look something like
R> .libPaths()
[1] "/data/Rpackages" "/usr/lib/R/site-library"
This means that when I install a package it goes to /data/ncsg3/Rpackages
If you really want to only have a single directory, you can set the R_LIBS_SITE variable to omit the default directories.

RCpp Temporary Build Location

I work in an environment where linking of dynamic libraries are restricted to certain locations. When I use RStudio and request a new C++ file I get the "Hello World" template. When I try to compile that and link that in by clicking on "Source" in RStudio, I get an error:
LoadLibrary failure: Access is denied.
This error is because the library was located in a space which is not allowed to be able to load DLL files. To maneuver around this limitation, I would like to determine how to tell RCpp to place the temporary dll's (not in a package) in a specific location.
I know that Dirk has suggested that this is not in the scope of RCpp and that all code should live in packages, but that will not be he most user friendly environment for the users here. I suspect that most will use RStudio projects with GIT.
So, that being said, is there an environment variable that I can mangle to get RCpp to place temporary dll files in a specific place. Or is there some other mechanism which I can use to alter this?
Try setting TMPDIR which R respects. This is indeed not an Rcpp issue but a generic R CMD build / R CMD INSTALL issue.
From help(tempfile):
The environment variables TMPDIR, TMP and TEMP are checked in
turn and the first found which points to a writable directory is
used: if none succeeds /tmp is used.
PS Rcpp with lower-case C.

Resources