Where should I set the variable PATH in R? - r

I constantly need to call Tex Live binaries for compilation in R. However after the upgrade of Tex Live distribution, the path to current binaries needed to updated manually in the PATH(Sys.getenv("PATH")) variable.
As a single user on a Ubuntu system, which file should I update the value in, so that R gets the PATH correctly irrespective of whichever directory R is launched from.
One point I still don't gather is from where does R gets its site-wide (I mean for all users, even if faulty in saying so) PATH variable set, because no such variable name as "PATH" occur inside any files (Renviron, Renviron.site, Rprofile.site) in either of "R_HOME/etc/" and user's home directory? I also haven't set Sys.getenv("R_ENVIRON") and Sys.getenv("R_ENVIRON_USER") values.
I'd appreciate anybody's input here.

#JeffreyGoldberg's solution was close, but not quite right.
Rprofile files are interpreted as R code
Renviron files can only contain name value pairs, and are not interpreted as R code
From the help for Startup:
Note that there are two sorts of files used in startup: environment files which contain lists of environment variables to be set, and profile files which contain R code.
I'm not sure if this question is asking specifically how one can set the site wide value of PATH, rather than PATH for one specific user, but there are three locations you can put these files.
A project directory (i.e., a directory you choose to launch R from)
HOME
R_HOME/etc
These locations are searched in the order numbered above. The first location can contain configurations specific to a project, the second contains those specific to a user, and the third, site wide configuration settings. When a file is found it is used, so local takes precedence over global. Don't think you can create a more specific version that simply updates what you've done in a more general configuration file. R_HOME/etc/Renviron is created on installation and should not be edited. You may create a file called R_HOME/etc/Renviron.site, but do not edit R_HOME/etc/Renviron.
To create a site wide value of PATH, you will want to set it in a file in R_HOME/etc. Here you can use either Renviron.site or Rprofile.site for the file name. For a file in R_HOME/etc, Do not use Renviron, Rprofile, .Renviron, or .Rprofile for the name of a profile or environment file in this location. You can find out what R_HOME is in an R session using R.home(), or Sys.getenv("R_HOME")
To create a PATH value for a single user, set it in a file in HOME, which you can find in your R session using Sys.getenv("HOME") or path.expand("~"). You can also just use "~" to refer to HOME. Here, an Renviron file should be ~/.Renvironand an Rprofile file ~/.Rprofile. Take note of the difference between how profile and environment files are named in your HOME directory vs. R_HOME/etc
To create a PATH for a single project, set it in a file in that project's top level directory. Name the files as you would in your home directory (.Rprofile or .Renviron).
If you are creating an Renviron file, the file should include the following line:
PATH=<your path>
< and > should not be included. An example would be:
PATH=/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
If you are creating an Rprofile file, the file should include the following line:
Sys.setenv("<your path>")
again, don't include "<" or ">". An example would be:
Sys.setenv("/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin")
There are various ways of doing this that get and edit a PATH variable (e.g., tack on a new path at the end, or the beginning). You can also use the strategy of setting an environment variable if it doesn't already exist and/or doesn't contain something you want it to. I've come to prefer just setting up my path simply, and coding it directly.
One final note, if you run R from a command line interface, environment variables may be inherited from your shell. RStudio also has its own startup sequence and may modify the end of your PATH variable. It should start as it is defined in your Rprofile or Renviron files. The R Console app itself has the fewest quirks with system environment variables, and should accept your path exactly as it is set with an Rprofile or Renviron file.

Edit: I should have tested before posting. What I describe below did not work. (Down voting my own answer is a strange thing.)
On my system (macOS, bash), R.app is not picking up my $PATH from my shell environment or .profile. However RStudio is picking it up. I do not understand the different behaviors.
One way to get consistent behavior would be to specify this in an Renviron file.
If you create a file named .Renviron in your come directory with a line like
Sys.setenv(PATH="/opt/local/bin:usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/Library/TeX/texbin")
(but of course with the path elements you need) that should give you consistent behavior.
The downside is that you need to manually maintain this. I suppose you could run a script from one of your other start up scripts that generated the .Renviron file. But either way, I consider this whole thing a work around in place of actually understanding where R picks up its environment from.

Related

R - Set environment variable permanently

I'd like to set the default distribution of Python for my Reticulate package to use. I use,
Sys.setenv(RETICULATE_PYTHON = "/usr/local/bin/python3")
however, I have to re-enter this line of code every time I start R. How can I set this permanently, so I don't need to specify which Python distribution I need every time?
On Windows, use Sys.getenv('R_USER') as #Brian Davis suggested in the comments to know the location of your home folder. On Linux, Sys.getenv('HOME') should be your normal home folder which you should use.
Now open up a terminal (if you're using recent versions of Rstudio there is one next to the console), go to your home folder and add a .Renviron file. You can do this without using the terminal too, but you'll probably have to confirm creation of a file starting with a dot.
cd path_to_my_home_Folder
touch .Renviron
Add RETICULATE_PYTHON = /usr/local/bin/python3 to it, and add also a new line at the end. Your file should look like this (if it's new):
> RETICULATE_PYTHON = /usr/local/bin/python3
Now you should be able to access your environment variable with Sys.getenv('RETICULATE_PYTHON') at each R session, since R looks for any .Renviron file defining environment variables in R home folder at startup (see documentation for startup?Startup).
UPDATE 29/10/2018
As it turnouts the variables defined with .Renviron are available only within Rstudio, which is not so much of a surprise since the .Renviron file is read at Rstudio startup. If you want the environment variable to be available for Rscript (for instance), you can :
Windows Add it to your user environment variables, using the Modify environment variables utility (available in the Start menu search bar)
Mac You can do the exact same procedure as above but do that on your .bash_profile instead of .Rstudio. Open up a terminal and place yourself to your user root folder (default location of the terminal usually). Add the following line (without blanks around the equal sign):
export RETICULATE_PYTHON=/usr/local/bin/python3
Save and close, restart terminal. The terminal reads your .bash_profile at start up, thus defining the environment variables. Your RETICULATE_PYTHON should now be available even in non-interactive R sessions.
The packge usethis has a function that opens the file .Renviron of your home folder.
usethis::edit_r_environ()
Once the file is opened, you just need add your pair key=value, save and close it.
RETICULATE_PYTHON=/usr/local/bin/python3

Sourcing in Rprofile.site: How to find the location of the sourced file

I have a "copy deployed" installation of R with no R-specific environment variables set.
I want to source a file from within Rprofile.site:
source("how/to/find/this/file/my_settings.R")
Where could I place my my_settings.R file so that it can be found from within Rprofile.site no matter in which path Rprofile.site is installed?
BTW: I want to avoid an absolute path to my_settings.R this would work indeed. I'd prefer to use the same folder as Rprofile.site or a path relative to it to support copy deployment of R.
Edit 1: The problem is that getwd is always different depending on the current folder from which you start R
If you can't use absolute paths and that your working directory is not stable, one way is to use .libPaths or .Library.
By default your Rprofile should be in directory paste0(.Library,"/../etc/") or paste0(.libPaths()[2],"/etc/") so you can put your file there and source it with :
source(paste0(.Library,"/../etc/my_settings.R"))
source(paste0(.libPaths()[2],"/etc/my_settings.R"))
As far as I understand the first option is stable (I don't think one can change the value of .Library).
If you use the second option just make sure that if in the future you alter your .libPaths() you do it after sourcing your file.
See ?.libPaths for more info on default folders.

R: Identifying an erroneous path setting - which path and where set?

When I start my R session (under windows 7), I get this error message:
Error: '\U' used without hex digits in character string starting ""C:\U"
I know what the problem is here: Somewhere, there is a directory being set to c:\USERS\something where the \ needs to be a \\ or a /. However, I do not know where it is.
My R_HOME/etc directory has an Rprofile.site file, but no .Rprofile and no .Renviron file.
My HOME directory has neither file.
My HOME/R/win-library/R_Library/base/R directory has an Rprofile file (no period) but no .Rprofile, and no .Renviron
My working directory has neither file.
The R_ARCH directory contains nothing R-related.
I identified all these directories with Sys.getenv().) Of the directories shown there, about 30 display as formed with (single) \'s, while nine are displayed as formed with (single) /'s. There is more than one C: Users directory with the slash going each way.
A computer-wide file search found no instances of either .Rprofile or .Renviron.
So where is this setting? And why does my R installation work at all, with 3/4 of the paths defined in environment variables going one way, and 1/4 of the paths (but still a lot of paths) going the other?
I'll bet it doesn't happen if you start it from cmd.exe with r --vanilla. Find all .Rdta or .Rdata files and rename them to bak version. Same for the .Rhistory files. May need to look inside the .rprofile.site file and see if it has been taken over by some gremlin. In Windows (and Macs), the "dot-files are usually hidden and you use magical incantations to expose them. The number of directories with solitary backslashes displayed in returns from Sys.getenv() should be zero.

How can I set the latex path for sweave in R?

I would like to know how I can set the pdflatex path in R to use sweave. Because I have 2 different MikTeX installations and one is working properly. Please take into account that I am using R (RStudio) in Windows. I found some suggestion however, for Linux or Unix users.
thanks in advance
If you have multiple installs of LaTeX (i.e. MikTeX) and you want to use a specific one of these, then you need to make sure that R finds the one you need first. This means that you have to add the location of your preferred version of pdfLaTeX at the front of your PATH system environment variable.
If you do not have administrator rights in Windows, then you can use R's environment file to change the PATH variable for R only. See ?Startup in R for details on this process. Follow the following steps:
in R, check the output of Sys.getenv("R_ENVIRON"). This will return the full path to an existing environment file, but will be empty in most cases. If a file exists, skip to step 3 below.
if no path is returned in step 1, create a file Renviron.site in the folder R_HOME/etc where R_HOME is the path returned by Sys.getenv("R_HOME").
add a line to the environment file as follows: PATH=C:\\full\\path\\to\\the\\folder\\with\\pdflatex;"${PATH}" (the quotations marks are important)
restart R and check Sys.getenv("PATH") and Sys.which("pdflatex") returns the correct paths.
If you do not have rights to create a Renviron.site file in R_HOME\etc, then you can also create a .Renviron file in HOME (Sys.getenv("HOME")).

How to change .Rprofile location in RStudio

I am working with a "factory fresh" version of RStudio on Windows 7. R is installed under C:/Program Files which means the default libraries are stored here, and the two locations contained in .libPaths() on startup are both within this folder.
I want to work with another R library (igraph). Since the C:\Program Files folder is write-protected, I have set up another area to work in: C:\Users\nick\R and installed the igraph library in C:\Users\nick\R\library. I can manually add this location to the .libPaths() variable and use the library with no problems.
However, my problem is getting RStudio to automatically add this location to the .libPaths() variable on startup. I read that I could add the relevant command to my .Rprofile file - but I couldn't find any such file (presumably they are not automatically created when RStudio is installed). I then created a file called .Rprofile containing only this command. This only seemed to work when the .Rprofile file was saved in C:\Users\nick\Documents (which is the path stored in both the R_USER and HOME environmental variables). What I would like is to have the .Rprofile file stored in C:\Users\nick\R.
I have read all the information in ?Startup and it talks about where to store commands that run on startup. But I just can't make this work. For example there seems to be no way to change the location of the home directory without reading a file stored in the home directory. I don't seem to have any .Renviron files and creating these myself doesn't seem to work either.
I would really appreciate an answer in simple terms that explains how I could go about changing where the .Rprofile file is read from.
In Windows, you set the R_USER profile by opening up a command line and running:
SETX R_PROFILE_USER "C:/.../.Rprofile"
Where (obviously) the path is the path to your desired .Rpofile. In R, you can check that it worked:
Sys.getenv("R_PROFILE_USER")
Should return the path you specified. Note that you likely need to have all R sessions closed before setting the R_USER variable.

Resources