How to create .Renviron file? - r

I need to use my user defined folder where I stored my packages. I have referred the link given below.
R libraries installation
I have applied the same but it didn't work for me. I have created a file called .Renviron with data R_LIBS=c:/Rpackages ,where my packages installed. But, still it is showing there is no such package 'RODBC' exists. Also, I put this file in my Documents folder in C drive and tried. That also gave the same error. Still the file type is showing as text document. How to change this file type?

On mac (or linux)
Open the terminal and type
touch $HOME/.Renviron
To open the file you just created, navigate through finder to /Users/<your-user-name>/.Renviron, or simply open the terminal and type
open $HOME/.Renviron
On windows
Click on start and open powershell. Copy this code into powershell
Add-Content c:\Users\$env:USERNAME\Documents\.Renviron "TEST_VARIABLE_1=my_username"
Add-Content c:\Users\$env:USERNAME\Documents\.Renviron "TEST_VARIABLE_2=123"
You'll now have a file called .Renviron located in the Documents folder. Close and reopen RStudio. Then run Sys.getenv('TEST_VARIABLE_1') to access the variable in R (obviously that works for any other environment variables you set as well).

It may be worth adding that package usethis now offers a series of function facilitating easy editing and opening of R startup files. Family of edit_* functions can be used to conveniently edit desired startup and configuration files. To edit .Renviron, which is appropriate place to store the relevant configuration use:
# Edit Renviron
usethis::edit_r_environ()
# You can also consider creating project-specific settings:
usethis::edit_r_environ(scope = "project")
Usethis provides other functions useful for managing R configuration. For instance in order to edit Makevars you can do:
# Edit .R/Makevars
usethis::edit_r_makevars()

Look at help(Startup) which has an example at the end:
## Example ~/.Renviron on Unix
R_LIBS=~/R/library
PAGER=/usr/local/bin/less
## Example .Renviron on Windows
R_LIBS=C:/R/library
MY_TCLTK="c:/Program Files/Tcl/bin"
## Example of setting R_DEFAULT_PACKAGES (from R CMD check)
R_DEFAULT_PACKAGES='utils,grDevices,graphics,stats'
# this loads the packages in the order given, so they appear on
# the search path in reverse order.
But note the spelling: Renviron with lower case e. Just use a text editor and edit the file. Also note that R has a variant in one of its system folder etc/ below its RHOME:
edd#bud:~$ R RHOME
/usr/lib/R
edd#bud:~$ cat $(R RHOME)/etc/Renviron.site
## Emacs please make this -*- R -*-
## empty Renviron.site for R on Debian
##
## Copyright (C) 2008 Dirk Eddelbuettel and GPL'ed
##
## see help(Startup) for documentation on ~/.Renviron and Renviron.site
# ## Example ~/.Renviron on Unix
# R_LIBS=~/R/library
# PAGER=/usr/local/bin/less
# ## Example .Renviron on Windows
# R_LIBS=C:/R/library
# MY_TCLTK="c:/Program Files/Tcl/bin"
# ## Example of setting R_DEFAULT_PACKAGES (from R CMD check)
# R_DEFAULT_PACKAGES='utils,grDevices,graphics,stats'
# # this loads the packages in the order given, so they appear on
# # the search path in reverse order.
edd#bud:~$
Hm. Looks like I wrote that for the Debian package and it does always exist. You can still copy it.

On Mac
The Renviron file is located in
/Library/Frameworks/R.framework/Versions/Current/Resources/etc/
Creating a .Renviron file in the User's home directory will not help.

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

Changing R options persistently

I want to change the prompt in R from > to R> and I know I should use the options command options(prompt="...") and it works, but then when I restart R the prompt is back to >.
Is there anyway to save the change so that it sticks?
Use .Rprofile file:
You can customize the R environment through a site initialization file or a directory initialization file. R will always source the Rprofile.site file first. On Windows, the file is in the C:\Program Files\R\R-n.n.n\etc directory. You can also place a .Rprofile file in any directory that you are going to run R from or in the user home directory.
At startup, R will source the Rprofile.site file. It will then look for a .Rprofile file to source in the current working directory. If it doesn't find it, it will look for one in the user's home directory. There are two special functions you can place in these files. .First( ) will be run at the start of the R session and .Last( ) will be run at the end of the session.
More details are here

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.

Making R installation self-contained/user-independent

I'm trying to get R to ignore c:\users\name\documents and be completely self-contained/portable
Here's my directory structure:
.../R/R-2.1.2.2/...
.../R/r_user/
.../R/libs_site/
I updated my Rprofile.site as follows:
# Set the working directory
setwd( file.path( R.home() , ".." , "r_user" ) )
# set the home directory
Sys.setenv(HOME=file.path( R.home() , ".." , "r_user" ) )
# Set the site library folder
.Library.site = file.path( R.home() , ".." , "libs_site" )
when R launches, I run .libPaths() but I still see c:/users/...
So perhaps R isn't updating per the Sys.setenv above? Solutions?
In my experience, creating a file named Renviron.site did not work on all my Windows machines, whereas naming the same file .Renviron instead did work everywhere. Not sure why. So if you're having difficulty with the above suggestions, then try .Renviron for a name.
Please do not hesitate to critically comment about my suggestion, because while I am describing what has worked for me, it may have difficulties I am not aware of.
In my experience, the following has worked:
Both Windows and Linux Platform:
Setting R paths
Find out the default paths:
.libPaths()
Set the path temporarily (in a R script)
.libPaths( "F:/Rlib" )
where F could be the letter associated with, say, a USB drive.
Query paths (both Windows and Linux):
Sys.getenv('R_LIBS_USER')
Sys.getenv('R_LIBS')
Sys.getenv('R_USER')
Sys.getenv('R_DOC_DIR')
Sys.getenv('HOME')
Try also:
normalizePath("~")
Try also:
getwd()
setwd(dir)
getwd returns an absolute filepath representing the current working directory of the R process
setwd(dir) is used to set the working directory to dir.
Windows (Tested on: 7x64)
Create an environment file named .Renviron
place it in the working directory or home directory:
"C:/Users/username/Documents"
Some users have reported that the .Renviron file needed to be in "c:/users/username/" instead. If you're not sure where to place it, save the desktop history and see where the .Rhistory file is located. Then place your .Renviron file in the same location. To save history savehistory()
# Windows .Renviron file:
R_LIBS_USER="C:/R/library"
R_USER="C:/R"
R_DOC_DIR="C:/R"
HOME="C:"
Set global PATH
My Computer / Properties / Advanced System Settings / Environment Variables --> user variables --> Path --> Edit
c:\R;c:\R\library;
Linux (Tested on kUbuntu 12.10)
Create an environment file named Renviron.site
place it in:
/etc/R/
Query the paths to check that your system is reading the Renviron.site file.
# Linux Renviron.site file:
R_LIBS_USER="~/R/library"
R_USER="~/R"
R_DOC_DIR="~/R"
#HOME="/home" # may not be needed
Remark: afaik the file is read from bottom to top, so HOME is defined at the bottom. In my setup ~ is correctly assigned to /home/ so I omit that last line anyway.
If you use RStudio, you may also want to add an rsession.conf file in the RStudio program directory. The following has worked for me:
# Windows 7:
r-libs-user="C:/R/library"
# Kubuntu 12:
# r-libs-user=~/R/%p-library/%v
You could create file Renviron.site in [your R installation path]\etc with lines
HOME="${R_HOME}\..\r_user"
R_LIBS_SITE="${R_HOME}\..\libs_site"
which set second and third of your settings. First could be replaced by setwd(Sys.getenv("HOME")).
I used the Rprofile.site file in [your R installation path]\etc and added the following lines to make C:/R/library my default library location each time R is launched:
# set a site library
.Library.site <- file.path("C:/R/library")
.libPaths(.Library.site)
I tried the other answers here but none of them worked with R 2.13.1 on Windows 7 64.
adding this does the trick:
.Library.site = file.path( R.home() , ".." , "site-library" )
.libPaths(.Library.site)

Resources