Why does the R have two libraries by default? - r

This is the first one: C:\Program Files\R\R-3.4.1\library
There is another one: C:\Users\Asus\Documents\R\win-library\3.4
I installed R all by default. Some packages are in the first library and some are in the second one.

By default, R use a system library where base packages are installed and a user library where packages are installed.
It is all defined in .libPaths() which is used by default by lib arg in install.packages (see help file) The first element of .libPaths() is used as default path for installing package. All paths are used to look for a package when you load them with library() with first path priority.
On my system, which is windows too, I have the same as you :
system library in the folder where R is installed
a user library in the Documents folder of my user.
you can see libPath help page here (or type ?.libPaths() in console) for how to tweek the default behaviour with environment variable.

Related

library installation how to have one path to install packages instead of two

i have two library path how can I override that via an argument to both install.packages() (from inside R) or R CMD INSTALL (outside R).
or override by setting the R_LIBS_USER variable.
The usual reason to end up with two libraries is that the system library is not writable when you install new packages.
So if you only want one path in .libPaths(), always run with elevated permissions ("Run as administrator" on Windows, use sudo on others) when you install packages.
It's probably a better idea not to install packages in the system library other than the base packages that come with R, because it's very easy to forget to change privileges, and then you can end up with two libraries again, and have one version of a package in the system library and a different version in the user library.

Always reinstalling packages

I just need to solve a problem with my RStudio. I have the lastest version on both R & RStudio, but every time that I reboot the PC ad open the program it tells me that some packages are not installed. After several times I have noticed that the problematic packages are: stringi, MASS, survival or Rcpp.
Also if I have to install another package which depends on them a warning appears saying that the packages mentioned are not updated, and it gives me the option to update all.
I tried to reinstall them and enter as an admin in RStudio but nothing works so, does anyone know what is happening here? Thanks for your help.
R packages are installed into libraries. The location of libraries searched by R is determined by the value of .libPaths(). Likely you have the problematic packages installed in more than one library; check for the same package installed in both dir(.libPaths()[1]) and dir(.libPaths()[2]), for instance.
Use remove.packages() with the lib= argument to remove one installation.
My practice is to install R and base packages only in the default library (possibly as administrator), and to install all other packages in a library that I as a regular user have access to. The personal library is the first entry in .libPaths(), which is the default location for package installation. See ?.libPaths for how to set up libraries; all library paths have to exist, else R silently drops them from .libPaths(). I use a setting in ~/.Renviron. Thus
> .libPaths()
[1] "/home/mtmorgan/R/x86_64-pc-linux-gnu-library/3.5-Bioc-3.8" # personal
[2] "/home/mtmorgan/bin/R-3-5-branch/library" # base
> sapply(.libPaths(), function(path) length(dir(path)))
/home/mtmorgan/R/x86_64-pc-linux-gnu-library/3.5-Bioc-3.8
236
/home/mtmorgan/bin/R-3-5-branch/library
30

How do i keep source files when using R's devtools library function 'install'

I am trying to build an R package (DESeq2) from source so that I can debug it. I've installed all the dependencies required and I'm following Hillary Parker's instructions for creating R packages. I'm running this on CentOS 6.6 using R-3.4.2.
I run :
library("devtools")
install("DESeq2", keep_source=TRUE)
It installs it in the directory with all my other R libraries. When I look at the installed DESeq2 library it is missing all the DESeq2/R/*.R and DESeq2/src/*.cpp files.
QUESTION : Where are these files and why didn't they get installed? This does not seem like the expected behavior.
R uses binary database format for installed packages to pack the objects into a database-alike file format for efficiency reasons (lazy loading). These database files (*.rdb and *.rdx) are stored in the R sub folder of the package installation path (see ?lazyLoad).
Even if
you are looking at the right place to find the installed package (use .libPaths() in R to find the installation folder)
and you have installed the package with the source code (like you did or
via install.packages("a_CRAN_package", INSTALL_opts = "--with-keep.source"))
you will not find R files in R folder there.
You can verify that the source code is available by picking one function name from the package and print it on the console. If you can see the source code (with comments) the package sources (R files) are available:
print(DeSeq2::any_function)
To make the source code available for debugging and stack traces you can set the option keep.source.pkgs = TRUE (see ?options) in your .Rprofile file or via an environment variable:
keep.source.pkgs:
As for keep.source, used only when packages are
installed. Defaults to FALSE unless the environment variable
R_KEEP_PKG_SOURCE is set to yes.
Note: The source code is available then only for newly installed and updated packages (not for already installed packages!).
For more details see: https://yetanothermathprogrammingconsultant.blogspot.de/2016/02/r-lazy-load-db-files.html

installation of R creates two library paths

I have observed that R installation in Windows creates two library paths automatically.
.libPaths()
# [1] "C:/Users/User/Documents/R/win-library/3.4"
# [2] "C:/Program Files/R/R-3.4.0/library"
What is the use of these while installing new packages and which library is used? I have frequently observed the installed packages being missed and need to install again. How do you maintain these two paths and manage the libraries while using R or RStudio in Windows?
Installing into C:/Program Files/R/... makes a package available to all users of the computer.
It is the R default, but installing a package there from within R (using install.packages() requires that R is started with administrator privileges.
Installing into C:/Users/Username/... makes the package available to the present user only, but does not require administrative rights.
R tracks these paths automatically, and looks in both directories when it is asked to load a package with require() or library(). No user input should be required.
When you update R, the version number will of course change, meaning that R will no longer look in the folders whose paths contained the previous version number. Some R updaters (e.g. installR) offer to copy packages from the "old" paths to the "new" paths, though an manually re-installing packages means that you can be sure that you are using the latest version of each package, and that you don't waste disk space and update time on packages that you are no longer using.

How to set the library location of installed R packages

My issue is that the package path I installed in R terminal is different from where I installed in R-studio. So I'd like to change the install location. How can I do that ? Thanks
R terminal:
You can pass the lib argument to install.packages which specifies the library location.
See ?install.packages:
lib: character vector giving the library directories where to
install the packages. Recycled as needed. If missing,
defaults to the first element of ‘.libPaths()’.
Rstudio:
If you go to tools -> Install Packages, you can specify where to install your package in the dialog.

Resources