Load both local and global .Rprofile in an RStudio project - r

I am working with several projects in Rstudio that require different .Rprofile files. These files usually consist of two parts: global settings I'd like to have every time I run R and local project-specific settings that are loaded when I open the project.
It is natural to exclude the global part from local .Rprofiles to keep it flexible. However, the relevant topic in the documentation states the following (backed up by this question):
When starting RStudio in an alternate working directory the .Rprofile file located within that directory is sourced. If (and only if) there is not an .Rprofile file in the alternate directory then the global default profile (e.g. ~/.Rprofile) is sourced instead.
How do I force to load the global .Rprofile at all times?
Small example. I currently have 2 .Rprofiles:
cat("global\n"); cat("local_1\n) for project 1;
cat("global\n"); cat("local_2\n) for project 2;
The global .Rprofile does not exist.
I'd like to have 3 of them:
cat("local_1\n) for project 1;
cat("local_2\n) for project 2;
cat("global\n") at the home dir.
How should I tinker with these files and/or Rstudio options to get the same output on startup of both projects?

This is just how R works (ie, the behaviour is not specific to RStudio) -- it sources only one of the .Rprofiles available. AFAIK this is not configurable unfortunately -- see ?Startup for full details on what R does on startup.
If you want to load both, I think you'll have to explicitly source the global .Rprofile from any local .Rprofiles.

Related

RStudio: Running .Rprofile in source file location

I have set up a workflow governed by a Makefile.
Under code/ I have multiple *.r scripts each typically responsible for creating one output file (typically an RData file but could also be csv exports or png images, any file in principle)
code/.Rprofile contains some helper functions to bootstrap the whole project directory system and sources some helper functions etc.
The scripts in code/ need this functionality to work properly.
RStudio has the convenient menu entry to set working directory to source file location.
But could I also make it run .Rprofile in that directory if found? Or really just start R a fresh from the directory of the source file?

Add script defining paths to .Rprofile

I am working on a project where I was hoping we'd be able to set file paths using the .Rprofile. I wrote a script that defines paths for specific users and since we're using renv and that puts an activate script into the profile anyways, I just added my paths.R script after that in the .Rprofile.
When I asked a colleague to open the .Rproj file to open the project, I thought it would run renv/activate.R and my paths.R script, but they're not seeing their defined paths, only mine. Am I missing something?
# contents of my .Rprofile
source("renv/activate.R")
source("paths.R")
# contents of path.R
if (Sys.info()["user"] == "francisco"){
data_dir <-
'path/to/data'
}
# etc
Any advice on workflow is welcome. Thanks!

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

Add existing scripts to an Rstudio project

I'm working in Rstudio and have multiple scripts open that have different working directories; however, each working directory exists within a larger folder on my computer (see below). Is it possible to add these scripts to an Rstudio Project without reorganizing all my files and changing each script's working directory?
File structure on computer:
Folder A
~~Folder 1
~~Folder 2
~~Folder 3
Say I have 3 scripts open, each with a working directory of either Folder 1, 2, or 3. Can I create a project that incorporates all three scripts. Say, set working directory to "Folder A"
Thanks much.
Technically, you can change working directory programmatically within a project, but this is considered a very poor practice and is strongly recommended against. However, you can set working directory at a project's top level (full path to Folder A, in your example) and then refer to scripts and objects, located in Folders 1-3 via corresponding relative paths. For example: "./Folder1/MyScript.R" or "./Folder2/MyData.csv".
It should be possible to create a project in the larger folder. You could even construct a simple master script in Folder A to manage this workflow:
setwd("./Folder 1")
source("scriptx")
setwd("..")
setwd("./Folder 2")
source("scripty")
setwd("..")
setwd("./Folder 3")
source("scriptz")
setwd("..")
Compared to source("Folder 1/scriptx") which runs each script within Folder A the master script would be running each script within it's own folder. Just make sure to use setwd("..") after running code in each folder and you can even run code in between to save output to the main Folder A.
If your workflow always creates folders in this manner I don't see how this would not be reproducible if you used relative paths. Albeit platform dependent, this modified version would create folders on the fly and run scripts kept in Folder A.
system("mkdir Folder_1")
setwd("./Folder_1")
source("../Folder A/scriptx")
setwd("..")
Notice here that when running terminal commands in R, it is recommended avoid spaces in directory or file names.

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