Rprofile and worksplace - r

I'm using macOS and I have a Rstudio, I always use my personal folder to work with R in Rstudio, I set the default working directory by
Rstudio -> Preference -> General -> R Session
and add
~/R
as the Default working directory.
Then I created a .Rprofile just to increase the history size by creating a .Rprofile file as:
usethis::edit_r_profile()
and the add the next line
## Increase the size of my Rhistory file
Sys.setenv(R_HISTSIZE='100000')
and then save it using the floppy icon, and it create the .Rprofile in:
~/.Rprofile
my question is, do I have to change the .Rprofile to the working directory ?
~/R/.Rprofile
or it has to be in home ?
~/.Rprofile
and if I want to move it to my personal working directory (~/R), how to make that Rstudio load it from my personal folder ???
Thanks

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 to permanently set fix my working dir?

Every time I restart R I issue the following command:
setwd("C:/Users/avtarsingh/Downloads")
How do make this my permanent working dir
Create a file called ".Rprofile" and add that command. R will look for this file every time it starts and change to that directory accordingly. More info in the R documentation here
In order to change your working directory permanently, you can find the following file “Rprofile.site” that is located in your installation path “etc” folder and open it in your favorite text editor (i.e., VSCode). Then you can add your permeant working directory to the first line, like the following line of code:
setwd(“C:\your permeant working directory”)
and save it. Next time you open the R-CRAN, your working directory is set to what you have saved.
Any code which you wish to run every R session can be added to Rprofile.site.
On a Windows machine, this file is located for example in C:\Program Files\R\R-3.1.2\etc. On Windows you will need to run your favourite text editor as an administrator to make changes to this file, since it is in Program Files.
This is also a good place to set your local CRAN repository, etc.

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