RStudio keeps adding `/usr/local/bin` to the front of the PATH variable - r

When I run system commands from RStudio using system("..."), I would like RStudio to use a certain version of Python.
When I open RStudio (just by clicking on the icon on a Mac) and set the PATH variables with Sys.setenv(PATH="my_python_path"), this works successfully. To make this the default variable, I tried updating both my ~/.Renviron (Using PATH=...) and ~/.Rprofile (using Sys.setenv("...")) pre-pending the path to the Python I would like to use.
Updating the PATH variables in .Renviron and .Rprofile takes effect, but RStudio nevertheless keeps pre-pending /usr/local/bin to the front of the PATH variable, which directs R to the default system Python in that directory. The Python path I specified comes right after that and does not get used.
Is there a way to make RStudio respect the PATH order that I specified in my .Renviron or .Rprofile?

From the comments, it's clear that it's not RStudio messing up the path, it's Finder. RStudio does modify the path, but it puts its entries after the existing ones. When I put this code in my ~/.Rprofile file:
print(Sys.getenv("PATH"))
Sys.setenv(PATH=paste("foobar", Sys.getenv("PATH"), sep=":"))
I see this printed:
[1] "/usr/bin:/bin:/usr/sbin:/sbin"
That's the PATH Finder is setting. Then in the R session, when I run Sys.getenv("PATH") I see
[1] "foobar:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin:
/usr/local/MacGPG2/bin:/opt/X11/bin:/Library/Apple/usr/bin:/usr/local
/git/bin:/Applications/RStudio.app/Contents/MacOS/postback"
showing the additions RStudio (or some code in R run after .Rprofile) has made to the end of the path.

Related

Migrate all R packages to another directory permanently [duplicate]

I am running R on Windows, not as an administrator. When I install a package, the following command doesn't work:
> install.packages("zoo")
Installing package(s) into ‘C:/Program Files/R/R-2.15.2/library’
(as ‘lib’ is unspecified)
Warning in install.packages :
'lib = "C:/Program Files/R/R-2.15.2/library"' is not writable
To install a package, I have to specify a library location:
install.packages("zoo", lib="C:/software/Rpackages")
To load a package, I also have to specify the library location:
library("zoo", lib.loc="C:/software/Rpackages")
All of this is OK, but I wanted to see if I could add C:/software/Rpackages to the library path somehow and thus not have to type it each time.
As I searched online, I found that one way to do this is to edit the Rprofile.site file and to add the line
.libPaths("C:/software/Rpackages")
However, after doing this, and starting RStudio, this is the output that I get
> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library"
The .libPaths command that I added to the Rprofile.site doesn't seem to have had any effect! Why is this the case? Or more importantly, how can I fix the problem so that I can install and load packages without typing in the library location?
Note: if I start RStudio the .libPaths() command seems to work as it is supposed to
.libPaths("C:/software/Rpackages")
> .libPaths()
[1] "C:/software/Rpackages" "C:/Program Files/R/R-2.15.2/library"
Isn't that strange?
The proper solution is to set environment variable R_LIBS_USER to the value of the file path to your desired library folder as opposed to getting RStudio to recognize a Rprofile.site file.
To set environment variable R_LIBS_USER in Windows, go to the Control Panel (System Properties -> Advanced system properties -> Environment Variables -> User Variables) to a desired value (the path to your library folder), e.g.
Variable name: R_LIBS_USER
Variable value: C:/software/Rpackages
If for some reason you do not have access to the control panel, you can try running rundll32 sysdm.cpl,EditEnvironmentVariables from the command line on Windows and add the environment variable from there.
Setting R_LIBS_USER will ensure that the library shows up first in .libPaths() regardless of starting RStudio directly or by right-clicking an file and "Open With" to start RStudio.
The Rprofile solution can work if RStudio is always started by clicking the RStudio shortcut. In this case, setting the default working directory to the directory that houses your Rprofile will be sufficient. The Rprofile solution does not work when clicking on a file to start RStudio because that changes the working directory away from the default working directory.
I generally try to keep all of my packages in one library, but if you want to add a library why not append the new library (which must already exist in your filesystem) to the existing library path?
.libPaths( c( .libPaths(), "~/userLibrary") )
# obviously this would need to be a valid file directory in your OS
# min just happened to be on a Mac that day
Or (and this will make the userLibrary the first place to put new packages):
.libPaths( c( "~/userLibrary" , .libPaths() ) )
Then I get (at least back when I wrote this originally):
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
[2] "/Users/user_name/userLibrary"
The .libPaths function is a bit different than most other nongraphics functions. It works via side-effect. The functions Sys.getenv and Sys.setenv that report and alter the R environment variables have been split apart but .libPaths can either report or alter its target.
The information about the R startup process can be read at ?Startup help page and there is RStudio material at: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio
In your case it appears that RStudio is not respecting the Rprofile.site settings or perhaps is overriding them by reading an .Rprofile setting from one of the RStudio defaults. It should also be mentioned that the result from this operation also appends the contents of calls to .Library and .Library.site, which is further reason why an RStudio- (or any other IDE or network installed-) hosted R might exhibit different behavior.
Since Sys.getenv() returns the current system environment for the R process, you can see the library and other paths with:
Sys.getenv()[ grep("LIB|PATH", names(Sys.getenv())) ]
The two that matter for storing and accessing packages are (now different on a Linux box):
R_LIBS_SITE /usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library
R_LIBS_USER /home/david/R/x86_64-pc-linux-gnu-library/3.5.1/
I managed to solve the problem by placing the code in the .Rprofile file in the default working directory.
First, I found the location of the default working directory
> getwd()
[1] "C:/Users/me/Documents"
Then I used a text editor to write a simple .Rprofile file with the following line in it
.libPaths("C:/software/Rpackages")
Finally, when I start R and run .libPaths() I get the desired output:
> .libPaths()
[1] "C:/software/Rpackages" "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"
https://superuser.com/questions/749283/change-rstudio-library-path-at-home-directory
Edit ~/.Renviron
R_LIBS_USER=/some/path
I found what I think is a solution here (thank you Carl Schwarz at SFU) for adding a personal library that is permanently (you don't have to define it each session) recognized whether using R or Rstudio, and Rstudio treats it as the default on my Mac machine. I hadn't seen it laid out this explicitly on SO, so I summarized the steps they provided, for Windows and then for Mac.
For a Windows 7 OS:
Create a directory on the drive where you want to have your personal library, e.g. C:\User\Rlibs (or another that you have permissions to)
Search for/go to "Edit environment variable for your account" in the Windows search bar to edit control panel settings
Click "New..." in the middle of the "Environmental Variables" window
In the "New User Variable" window, type R_LIBS for the "Variable name", and the path to the personal library directory you created, e.g. C:\User\Rlibs
Click OK and you should see the Variable/Value pair in the User variables window
Click OK again
Now when you start R (or Rstudio) and type the command .libPaths() you should see the personal library you created as well as the R system library.
For Mac:
In your "Home" or "username" directory create a folder called Rlibs
Launch the Terminal application
Type: echo "R_LIBS=~/Rlibs" > .Renviron Make sure the spelling and case matches.
Type ls -a to see the full list of files in the directory, which should now include .Renvrion
Verify that the .Renviron file has been set properly: more .Renviron
Launch R/Rstudio and type .libPaths() and you should see the new path to your personal library.
If you do not have admin-rights, it can also be helpful to open the Rprofile.site-file located in \R-3.1.0\etc and add:
.First <- function(){
.libPaths("your path here")
}
This evaluates the .libPath() command directly at start
just change the default folder for your R libraries in a directory with no Administrator rights, e.g.
.libPaths("C:/R/libs")
On Ubuntu, the recommended way of changing the default library path for a user, is to set the R_LIBS_USER variable in the ~/.Renviron file.
touch ~/.Renviron
echo "R_LIBS_USER=/custom/path/in/absolute/form" >> ~/.Renviron
I've had real trouble understanding this. gorkypl gave the correct solution above when I last re-installed my OS & Rstudio but this time round, setting my environment variable didn't resolve.
Uninstallled both R and Rstudio, creating directories C:\R and C:\Rstudio then reinstalled both.
Define R_LIBS_USER user variable to your prefered directory (as per gorkypl's answer) and restart your machine for User variable to be loaded. Open Rstudio, errors should be gone.
You can also use Sys.setenv() to modify R_LIBS_USER to the path of your alternative library which is easier and does not need to restart your computer.
To see what R_LIBS_USER is set to:
?Sys.getenv()
Reading help(Startup) is useful.
If your default package library has been changed after installing a new version of R or by any other means, you can append both the libraries to use all the packages with the help of the commands below.
Get the existing library path :
.libPaths()
Now,set the existing and the old path :
.libPaths(c(.libPaths(), "~/yourOldPath"))
Hope it helps.
I read the readme. In that they mentioned use .libPaths() in command line to check which paths are there. I had 2 library paths earlier. When I used the command .libpath("C:/Program Files/R/R-3.2.4revised/library") where I wanted, it changed the library path. When I typed in .libPaths() at the command line again it showed me the correct path. Hope this helps
getwd()
# [1] "C:/Users/..../software/My R studio"
copy the above link with double inverted comma
.libPaths(new="C:/Users/..../software/My R studio")
Your default path will change for installing pakages
If you want to change your library path permanently (without calling .libPath() every time when entering in R, this works for me:
create .Rprofile under your home directory. (~/.Rprofile)
type
.libPaths(c( .libPaths(), "your new path" ))
in .Rprofile file, save.
open R (any directory) and check, just type .libPaths(), you can find your libaray path is updated!
Since most of the answers here are related to Windows & Mac OS, (and considering that I also struggled with this) I decided to post the process that helped me solve this problem on my Arch Linux setup.
Step 1:
Do a global search of your system (e.g. ANGRYSearch) for the term Renviron (which is the configuration file where the settings for the user libraries are set).
It should return only two results at the following directory paths:
/etc/R/
/usr/lib/R/etc/
NOTE: The Renviron config files stored at 1 & 2 (above) are hot-linked to each other (which means changes made to one file will automatically be applied [ in the same form / structure ] to the other file when the file being edited is saved - [ you also need sudo rights for saving the file post-edit ] ).
Step 2:
Navigate into the 1st directory path ( /etc/R/ ) and open the Renviron file with your favourite text editor.
Once inside the Renviron file search for the R_LIBS_USER tag and update the text in the curly braces section to your desired directory path.
EXAMPLE:
... Change From ( original entry ):
R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/4.0'}
... Change To ( your desired entry ):
R_LIBS_USER=${R_LIBS_USER-'~/Apps/R/rUserLibs'}
Step 3:
Save the Renviron file you've just edited ... DONE !!
I had the same problem and I run into this. If you want to create another location c("C:/Users/mynewlocation") should be working as well. As mentioned in here "You should be able to right-click on the Rstudio.exe icon, click Properties, and select an option to always run Rstudio as administrator. Be sure you use that same icon whenever you want to open Rstudio."
myPaths <- .libPaths() # get the paths
myPaths <- c(myPaths[2], myPaths[1]) # switch them
.libPaths(myPaths) # reassign them
I was looking into this because R was having issues installing into the default location and was instead just putting the packages into the temp folder. It turned out to be the latest update for Mcaffee Endpoint Security which apparently has issues with R. You can disable the threat protection while you install the packages and it will work properly.

Find pandoc executable from R

rmarkdown::find_pandoc helps us to find the pandoc executable w/o the need of specifiying any environmental variable when running form within RStudio:
## in Rstudio
!is.null(rmarkdown::find_pandoc()$dir)
# [1] TRUE
However, when running the same command from a plain R console I get:
### R console
!is.null(rmarkdown::find_pandoc()$dir)
# [1] FALSE
Reading the documentation pf ?rmarkdown::find_pandoc(), explains why I am getting these results:
dir: A character vector of potential directory paths under which
‘pandoc’ may be found. If not provided, this function
searches for ‘pandoc’ from the environment variable
RSTUDIO_PANDOC (the RStudio IDE will set this variable to the
directory of Pandoc bundled with the IDE), the environment
variable PATH, and the directory ‘~/opt/pandoc/’.
I want now to write a script which can be run from the command line (specifically not from within RStudio), which needs pandoc to be found. As per the help I could set my PATH to assure that pandoc is also found from the command line, but as soon as I want a colleague of mine to use the script, I have to make sure that his/her PATH is set accordingly and I want to avoid that.
However, I do know that everybody has Rstudio installed (not at the same location though), so if I knew the location of RStudio I could derive pandoc's location too.
Is there any reliable way to get the path of Rstudio from the console (i.e. also when Rstudio is not running)?
To make a long story short: how can I find Rstudio even if it is not running`?
This could work, it is based on linux, but I guess if you are in other system, it could be adapted:
pandoc_path <- system2("find", args="/usr/lib/rstudio -name pandoc -type f", stdout = T)
path_sep <- ":"
Sys.setenv(PATH = paste0(Sys.getenv("PATH"),path_sep, sub(".pandoc$","",pandoc_path )))

Running newer version of R from terminal when older version is invoked by default

I'm trying to run R from iTerm on an OSX computer (OSX 10.11.6). When I enter R, it opens up an older version of R, from the path /Users/***/miniconda2/bin/R. I would like it to run, by default, an R version found at /usr/local/bin/R, without having to enter the full path every time. How would one go about changing the location of the default R?
Thanks for your help
This is likely due to the PATH variable preferring ~/miniconda2/bin before /usr/local/bin. I'm giving you a few options here to help understand why it is happening.
Let's assume your PATH looks like this:
/Users/me/bin:/Users/me/miniconda2/bin:/usr/local/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Modify PATH
You could modify PATH to move /Users/me/miniconda2/bin after /usr/local/bin or remove it from PATH completely. The downside is that if you rely on other binaries in ~/miniconda2/bin they will no longer be found when executing them by name.
Move R out of the way
Another option would be to move ~/miniconda/bin/R out of the way, for example using
mv ~/miniconda/bin/R ~/miniconda/bin/R-miniconda
Afterwards R will be run from the next location in $PATH, but if you update miniconda2 it may return.
Link to R further up in the PATH (easiest/best)
Finally, you could make sure that there is an R executable in something that is further up the $PATH. This is probably the easiest and most effective option.
First, make sure you have a bin folder in your home directory. If this is not the case, create it using mkdir ~/bin and then restart the terminal. The restart should cause the code in ~/.profile to add that folder to your $PATH. You can verify by doing echo $PATH. If this is not the case, add the following line to your ~/.profile or ~/.bash_profile:
export PATH=$HOME/bin:$PATH
In the example at the top, the PATH already contains /Users/me/bin at the beginning of the line (highest priority).
Next, create a soft link to the R binary in the newly created folder:
ln -s /usr/local/bin/R ~/bin/R
You should now be able to execute R, which will prefer the softlink created, which will execute the one you like. If it does not work right away execute hash -r or restart the terminal.
Just in case you happen to be using RStudio Server (open source) or someone is looking for how to change the RStudio Server default version of R, here is what I found when trying to answer the same question:
Starting in RStudio Server 1.3 (the newest version is 1.4.1106, released February 22, 2021), a user’s preferred version of R can be specified in the rstudio-prefs.json file in the global-level /etc/rstudio folder or in the user-level ~/.config/rstudio folder.
See https://blog.rstudio.com/2020/02/18/rstudio-1-3-preview-configuration/ and https://docs.rstudio.com/ide/server-pro/session-user-settings.html for user setting options in newer versions of RStudio Server.
See https://support.rstudio.com/hc/en-us/articles/200716783-RStudio-Release-History for RStudio release history and https://www.rstudio.com/products/rstudio/download-server/redhat-centos/ for Red Hat downloads of the newest version of RStudio Server.

How do I change the default library path for R packages

I have attempted to install R and R studio on the local drive on my work computer as opposed to the organization network folder because anything that runs through the network is really slow. When installing, the destination path shows that it's my local C:drive. However, when I install a new package, the default path shown is my network drive and there is no option to change:
.libPaths()
[1] "\\\\The library/path/I/don't/want"
[2] "C:/Program Files/R/R-3.2.1/library"
I'm running windows 7 professional. How can I remove library path [1] and make path [2] my primary for all base packages and all new packages that I install?
Windows 7/10: If your C:\Program Files (or wherever R is installed) is blocked for writing, as mine is, then you'll get frustrated editing RProfile.site (as I did). As specified in the accepted answer, I updated R_LIBS_USER and it worked. However, even after reading the fine manual several times and extensive searching, it took me several hours to do this. In the spirit of saving someone else time...
Let's assume you want your packages to reside in C:\R\Library:
Create the folder C:\R\Library. Next I need to add this folder to the R_LIBS_USER path:
Click Start --> Control Panel --> User Accounts --> Change my environmental variables
The Environmental Variables window pops up. If you see R_LIBS_USER, highlight it and click Edit. Otherwise click New. Both actions open a window with fields for Variable and Value.
In my case, R_LIBS_USER was already there, and Value was a path to my desktop. I added to the path the folder that I created, separated by semicolon. C:\R\Library;C:\Users\Eric.Krantz\Desktop\R stuff\Packages.
(NOTE: In the last step, I could have removed the path to the Desktop location and simply left C:\R\Library).
See help(Startup) and help(.libPaths) as you have several possibilities where this may have gotten set. Among them are
setting R_LIBS_USER
assigning .libPaths() in .Rprofile or Rprofile.site
and more.
In this particular case you need to go backwards and unset whereever \\\\The library/path/I/don't/want is set.
To otherwise ignore it you need to override it use explicitly i.e. via
library("somePackage", lib.loc=.libPaths()[-1])
when loading a package.
Facing the very same problem (avoiding the default path in a network) I came up to this solution with the hints given in other answers.
The solution is editing the Rprofile file to overwrite the variable R_LIBS_USER which by default points to the home directory.
Here the steps:
Create the target destination folder for the libraries, e.g.,
~\target.
Find the Rprofile file. In my case it was at C:\Program Files\R\R-3.3.3\library\base\R\Rprofile.
Edit the file and change the definition the variable R_LIBS_USER. In my case, I replaced the this line file.path(Sys.getenv("R_USER"), "R", with file.path("~\target", "R",.
The documentation that support this solution is here
Original file with:
if(!nzchar(Sys.getenv("R_LIBS_USER")))
Sys.setenv(R_LIBS_USER=
file.path(Sys.getenv("R_USER"), "R",
"win-library",
paste(R.version$major,
sub("\\..*$", "", R.version$minor),
sep=".")
))
Modified file:
if(!nzchar(Sys.getenv("R_LIBS_USER")))
Sys.setenv(R_LIBS_USER=
file.path("~\target", "R",
"win-library",
paste(R.version$major,
sub("\\..*$", "", R.version$minor),
sep=".")
))
Windows 10 on a Network
Having your packages stored on the network drive can slow down the performance of R / R Studio considerably, and you spend a lot of time waiting for the libraries to load/install, due to the bottlenecks of having to retrieve and push data over the server back to your local host. See the following for instructions on how to create an .RProfile on your local machine:
Create a directory called C:\Users\xxxxxx\Documents\R\3.4 (or whatever R version you are using, and where you will store your local R packages- your directory location may be different than mine)
On R Console, type Sys.getenv("HOME") to get your home directory (this is where your .RProfile will be stored and R will always check there for packages- and this is on the network if packages are stored there)
Create a file called .Rprofile and place it in :\YOUR\HOME\DIRECTORY\ON_NETWORK (the directory you get after typing Sys.getenv("HOME") in R Console)
File contents of .Rprofile should be like this:
#search 2 places for packages- install new packages to first directory- load built-in packages from the second (this is from your base R package- will be different for some)
.libPaths(c("C:\Users\xxxxxx\Documents\R\3.4", "C:/Program Files/Microsoft/R Client/R_SERVER/library"))
message("*** Setting libPath to local hard drive ***")
#insert a sleep command at line 12 of the unpackPkgZip function. So, just after the package is unzipped.
trace(utils:::unpackPkgZip, quote(Sys.sleep(2)), at=12L, print=TRUE)
message("*** Add 2 second delay when installing packages, to accommodate virus scanner for R 3.4 (fixed in R 3.5+)***")
# fix problem with tcltk for sqldf package: https://github.com/ggrothendieck/sqldf#problem-involvling-tcltk
options(gsubfn.engine = "R")
message("*** Successfully loaded .Rprofile ***")
Restart R Studio and verify that you see that the messages above are displayed.
Now you can enjoy faster performance of your application on local host, vs. storing the packages on the network and slowing everything down.
I was struggling for a while with this as my work computer (with Windows 10) created the default user library on a network drive, which would slow down R and RStudio to an unusable state.
In case this helps someone, this is the easiest way I found, without requiring admin rights:
make sure the directory you want to install your packages into exists. If you want to respect the convention, use: C:\Users\username\R\win-library\rversion (for example, something like: C:\Users\janebloggs\R\win-library\3.6)
create a .Renviron file in your home directory (which might be on the network drive?), and in it, write one single line that defines the R_LIBS_USER variable to be your custom path:
R_LIBS_USER=C:\Users\janebloggs\R\win-library\3.6
(feel free to add comments too, with lines starting with #)
If a .Renviron file exists, R will read it at startup and use the variables as they are defined in there, before running the code in the .Rprofile. You can read about it in help(Startup).
Now it should be persistent between sessions!
After a couple of hours of trying to solve the issue in several ways, some of which are described here, for me (on Win 10) the option of creating a Renviron file worked, but a little different from what was written here above.
The task is to change the value of the variable R_LIBS_USER. To do this two steps needed:
Create the file named Renviron (without dot) in the folder \Program\etc\ (Program is the directory where R is installed--for example, for me it was C:\Program Files\R\R-4.0.0\etc)
Insert a line in Renviron with new path: R_LIBS_USER = "C:/R/Library"
After that, reboot R and use .libPaths() to confirm the default directory changed.
I think I tried all of the above and it didn't work for me. This worked, though:
In home directory, make a file called ".Renviron"
In that file, write:
.libPaths(new = "/my/path/to/libs")
Save and restart R if you had it open

Change R default library path using .libPaths in Rprofile.site fails to work

I am running R on Windows, not as an administrator. When I install a package, the following command doesn't work:
> install.packages("zoo")
Installing package(s) into ‘C:/Program Files/R/R-2.15.2/library’
(as ‘lib’ is unspecified)
Warning in install.packages :
'lib = "C:/Program Files/R/R-2.15.2/library"' is not writable
To install a package, I have to specify a library location:
install.packages("zoo", lib="C:/software/Rpackages")
To load a package, I also have to specify the library location:
library("zoo", lib.loc="C:/software/Rpackages")
All of this is OK, but I wanted to see if I could add C:/software/Rpackages to the library path somehow and thus not have to type it each time.
As I searched online, I found that one way to do this is to edit the Rprofile.site file and to add the line
.libPaths("C:/software/Rpackages")
However, after doing this, and starting RStudio, this is the output that I get
> .libPaths()
[1] "C:/Program Files/R/R-2.15.2/library" "C:/Program Files/RStudio/R/library"
The .libPaths command that I added to the Rprofile.site doesn't seem to have had any effect! Why is this the case? Or more importantly, how can I fix the problem so that I can install and load packages without typing in the library location?
Note: if I start RStudio the .libPaths() command seems to work as it is supposed to
.libPaths("C:/software/Rpackages")
> .libPaths()
[1] "C:/software/Rpackages" "C:/Program Files/R/R-2.15.2/library"
Isn't that strange?
The proper solution is to set environment variable R_LIBS_USER to the value of the file path to your desired library folder as opposed to getting RStudio to recognize a Rprofile.site file.
To set environment variable R_LIBS_USER in Windows, go to the Control Panel (System Properties -> Advanced system properties -> Environment Variables -> User Variables) to a desired value (the path to your library folder), e.g.
Variable name: R_LIBS_USER
Variable value: C:/software/Rpackages
If for some reason you do not have access to the control panel, you can try running rundll32 sysdm.cpl,EditEnvironmentVariables from the command line on Windows and add the environment variable from there.
Setting R_LIBS_USER will ensure that the library shows up first in .libPaths() regardless of starting RStudio directly or by right-clicking an file and "Open With" to start RStudio.
The Rprofile solution can work if RStudio is always started by clicking the RStudio shortcut. In this case, setting the default working directory to the directory that houses your Rprofile will be sufficient. The Rprofile solution does not work when clicking on a file to start RStudio because that changes the working directory away from the default working directory.
I generally try to keep all of my packages in one library, but if you want to add a library why not append the new library (which must already exist in your filesystem) to the existing library path?
.libPaths( c( .libPaths(), "~/userLibrary") )
# obviously this would need to be a valid file directory in your OS
# min just happened to be on a Mac that day
Or (and this will make the userLibrary the first place to put new packages):
.libPaths( c( "~/userLibrary" , .libPaths() ) )
Then I get (at least back when I wrote this originally):
> .libPaths()
[1] "/Library/Frameworks/R.framework/Versions/2.15/Resources/library"
[2] "/Users/user_name/userLibrary"
The .libPaths function is a bit different than most other nongraphics functions. It works via side-effect. The functions Sys.getenv and Sys.setenv that report and alter the R environment variables have been split apart but .libPaths can either report or alter its target.
The information about the R startup process can be read at ?Startup help page and there is RStudio material at: https://support.rstudio.com/hc/en-us/articles/200549016-Customizing-RStudio
In your case it appears that RStudio is not respecting the Rprofile.site settings or perhaps is overriding them by reading an .Rprofile setting from one of the RStudio defaults. It should also be mentioned that the result from this operation also appends the contents of calls to .Library and .Library.site, which is further reason why an RStudio- (or any other IDE or network installed-) hosted R might exhibit different behavior.
Since Sys.getenv() returns the current system environment for the R process, you can see the library and other paths with:
Sys.getenv()[ grep("LIB|PATH", names(Sys.getenv())) ]
The two that matter for storing and accessing packages are (now different on a Linux box):
R_LIBS_SITE /usr/local/lib/R/site-library:/usr/lib/R/site-library:/usr/lib/R/library
R_LIBS_USER /home/david/R/x86_64-pc-linux-gnu-library/3.5.1/
I managed to solve the problem by placing the code in the .Rprofile file in the default working directory.
First, I found the location of the default working directory
> getwd()
[1] "C:/Users/me/Documents"
Then I used a text editor to write a simple .Rprofile file with the following line in it
.libPaths("C:/software/Rpackages")
Finally, when I start R and run .libPaths() I get the desired output:
> .libPaths()
[1] "C:/software/Rpackages" "C:/Program Files/R/R-2.15.2/library"
[3] "C:/Program Files/RStudio/R/library"
https://superuser.com/questions/749283/change-rstudio-library-path-at-home-directory
Edit ~/.Renviron
R_LIBS_USER=/some/path
I found what I think is a solution here (thank you Carl Schwarz at SFU) for adding a personal library that is permanently (you don't have to define it each session) recognized whether using R or Rstudio, and Rstudio treats it as the default on my Mac machine. I hadn't seen it laid out this explicitly on SO, so I summarized the steps they provided, for Windows and then for Mac.
For a Windows 7 OS:
Create a directory on the drive where you want to have your personal library, e.g. C:\User\Rlibs (or another that you have permissions to)
Search for/go to "Edit environment variable for your account" in the Windows search bar to edit control panel settings
Click "New..." in the middle of the "Environmental Variables" window
In the "New User Variable" window, type R_LIBS for the "Variable name", and the path to the personal library directory you created, e.g. C:\User\Rlibs
Click OK and you should see the Variable/Value pair in the User variables window
Click OK again
Now when you start R (or Rstudio) and type the command .libPaths() you should see the personal library you created as well as the R system library.
For Mac:
In your "Home" or "username" directory create a folder called Rlibs
Launch the Terminal application
Type: echo "R_LIBS=~/Rlibs" > .Renviron Make sure the spelling and case matches.
Type ls -a to see the full list of files in the directory, which should now include .Renvrion
Verify that the .Renviron file has been set properly: more .Renviron
Launch R/Rstudio and type .libPaths() and you should see the new path to your personal library.
If you do not have admin-rights, it can also be helpful to open the Rprofile.site-file located in \R-3.1.0\etc and add:
.First <- function(){
.libPaths("your path here")
}
This evaluates the .libPath() command directly at start
just change the default folder for your R libraries in a directory with no Administrator rights, e.g.
.libPaths("C:/R/libs")
On Ubuntu, the recommended way of changing the default library path for a user, is to set the R_LIBS_USER variable in the ~/.Renviron file.
touch ~/.Renviron
echo "R_LIBS_USER=/custom/path/in/absolute/form" >> ~/.Renviron
I've had real trouble understanding this. gorkypl gave the correct solution above when I last re-installed my OS & Rstudio but this time round, setting my environment variable didn't resolve.
Uninstallled both R and Rstudio, creating directories C:\R and C:\Rstudio then reinstalled both.
Define R_LIBS_USER user variable to your prefered directory (as per gorkypl's answer) and restart your machine for User variable to be loaded. Open Rstudio, errors should be gone.
You can also use Sys.setenv() to modify R_LIBS_USER to the path of your alternative library which is easier and does not need to restart your computer.
To see what R_LIBS_USER is set to:
?Sys.getenv()
Reading help(Startup) is useful.
If your default package library has been changed after installing a new version of R or by any other means, you can append both the libraries to use all the packages with the help of the commands below.
Get the existing library path :
.libPaths()
Now,set the existing and the old path :
.libPaths(c(.libPaths(), "~/yourOldPath"))
Hope it helps.
I read the readme. In that they mentioned use .libPaths() in command line to check which paths are there. I had 2 library paths earlier. When I used the command .libpath("C:/Program Files/R/R-3.2.4revised/library") where I wanted, it changed the library path. When I typed in .libPaths() at the command line again it showed me the correct path. Hope this helps
getwd()
# [1] "C:/Users/..../software/My R studio"
copy the above link with double inverted comma
.libPaths(new="C:/Users/..../software/My R studio")
Your default path will change for installing pakages
If you want to change your library path permanently (without calling .libPath() every time when entering in R, this works for me:
create .Rprofile under your home directory. (~/.Rprofile)
type
.libPaths(c( .libPaths(), "your new path" ))
in .Rprofile file, save.
open R (any directory) and check, just type .libPaths(), you can find your libaray path is updated!
Since most of the answers here are related to Windows & Mac OS, (and considering that I also struggled with this) I decided to post the process that helped me solve this problem on my Arch Linux setup.
Step 1:
Do a global search of your system (e.g. ANGRYSearch) for the term Renviron (which is the configuration file where the settings for the user libraries are set).
It should return only two results at the following directory paths:
/etc/R/
/usr/lib/R/etc/
NOTE: The Renviron config files stored at 1 & 2 (above) are hot-linked to each other (which means changes made to one file will automatically be applied [ in the same form / structure ] to the other file when the file being edited is saved - [ you also need sudo rights for saving the file post-edit ] ).
Step 2:
Navigate into the 1st directory path ( /etc/R/ ) and open the Renviron file with your favourite text editor.
Once inside the Renviron file search for the R_LIBS_USER tag and update the text in the curly braces section to your desired directory path.
EXAMPLE:
... Change From ( original entry ):
R_LIBS_USER=${R_LIBS_USER-'~/R/x86_64-pc-linux-gnu-library/4.0'}
... Change To ( your desired entry ):
R_LIBS_USER=${R_LIBS_USER-'~/Apps/R/rUserLibs'}
Step 3:
Save the Renviron file you've just edited ... DONE !!
I had the same problem and I run into this. If you want to create another location c("C:/Users/mynewlocation") should be working as well. As mentioned in here "You should be able to right-click on the Rstudio.exe icon, click Properties, and select an option to always run Rstudio as administrator. Be sure you use that same icon whenever you want to open Rstudio."
myPaths <- .libPaths() # get the paths
myPaths <- c(myPaths[2], myPaths[1]) # switch them
.libPaths(myPaths) # reassign them
I was looking into this because R was having issues installing into the default location and was instead just putting the packages into the temp folder. It turned out to be the latest update for Mcaffee Endpoint Security which apparently has issues with R. You can disable the threat protection while you install the packages and it will work properly.

Resources