Setting R environmental variable in Tortoise SVN - r

I have a collection of functions in a file called some_functions.R and saved in a SVN directory in C:\blah1\blah2\Rcodes\some_functions.R . I have several Rprojects which uses this code file. Say a R project is available in the directory C:\blah1\blah2\Rprojects\project1. I can use hard coded path to refer the file and it works.
source("C:/blah1/blah2/Rcodes/some_functions.R")'
But I would like to set the path as environmental variable.
Looking at How to unfold user and environment variable in R language? and setting the home directory in windows R I add the following line in the RProfile.site file
Sys.setenv(R_CODE_PATH = "C:/blah1/blah2/Rcodes")
and in the project1.Rnw file
source("R_CODE_PATH/some_functions.R")
But the project file can not read the some_functions.R file. I tried with %R_CODE_PATH% without any luck.
Not sure what I'm missing here. Any help is much appreciated.

You retrieve environment variables using Sys.getenv(). Try:
r_code_path <- Sys.getenv("R_CODE_PATH")
Then, for example:
source(paste(r_code_path, "some_functions.R", sep = "/"))
I would use the .Renviron config file to define environment variables. Put it in whatever directory the R command Sys.getenv("HOME") returns and include lines like this:
R_CODE_PATH=C:/blah1/blah2/Rcodes

Related

Read a file in R without changing the working directory

How can others who run my R program read a file(eg: csv) used in my R code without having to change the working directory in setwd()?
I will suggest you use the here() function in the here package in your code like this:
library(here)
Data1 <- read_csv(here("test_data.csv"))
read.csv has a file argument and if I were to quote from the inbuilt R help about file:
If it does not contain an absolute path, the file name is relative to
the current working directory, getwd().
So, providing the absolute path of the file inside the file argument solves this problem.
In Windows
Suppose your file name is test.csv and it's located in D:\files\test_folder (you can get the location of any file from its properties in Windows)
For reading this file you run:
df<-read.csv('D:\\files\\test_folder\\test.csv')
or
df<-read.csv('D:/files/test_folder/test.csv')
Suggested reading: Why \\ instead of \ and Paths in programming languages
Haven't used R in Linux but maybe Getting a file path in Linux might help
Read from web
Just type in the web address of the dataset in the file attribute. Try:
df<-read.csv('https://raw.githubusercontent.com/AdiPersonalWorks/Random/master/student_scores%20-%20student_scores.csv')
Note: This link contains a list of 25 students with their study hours and marks. I myself used this dataset for one of my earlier tasks and its perfectly safe

Outsource function/script permanently

In every Script I paste the same path to get some functions/r-scripts. Is there a possibility to save this path globally and permanently? It should be independently of the workingspace or other loading data paths and should be available on every new R-Session.
Thanks.
1) Place this in your .Rprofile file. The messsage line is optional but will allow you to confirm that .Rprofile was run when you start R.
message("Hello")
options("mypath" = "...path goes here...")
and then in your script get it using the following which will use that path if the mypath option was defined or the current directory otherwise. Omit the second argument if you don't want to have a default.
getOption("mypath", ".")
2) You could also just put this in your .Rprofile
mypath <- "...your path goes here..."
and then just refer to mypath in the script but that will cause mypath to clutter up your workspace which you may not want.
3) R also will read the environment variables defined in .Renviron so if that file contained:
MYPATH="...path goes here..."
then the script could get it via Sys.getenv("MYPATH") .
See ?Startup for more information on .Rprofile and .Renviron .
4) You could also create an exported shell variable on Linux or environment variable on Windows and then read it in the script using Sys.getenv .

R create .Renviron file

I know that there are already some explanations around about how to set variables in a .Renviron file. However, I couldn't find a solution to my question: How to create this file the first time. I don't want to specify the location for my installed packages, but want to insert a json file for API requests.
So can I simply create a .txt file, name it .Renviron and write the code in the file?
This should be the content of the .Renviron file:
GL_AUTH = "auth/auth.json"
GAR_CLIENT_WEB_JSON = "test.json"
I also found this line of code:
Sys.setenv(GAR_CLIENT_WEB_JSON = "app/test.json")
Do I need this line of code to fill the .Renviron file instead of typing the code directly into the file?
Thank you so much in advance!

R: Is there a way to specify TMPDIR in .Renviron based on $HOME or ~?

I am looking for a simple student-proof way to specify a custom TMPDIR in R sessions.
R seems unable to expand ~ or $USER, so if I simply write TMPDIR=~/tmp or TMPDIR=$HOME/tmp in .Renviron it does not find the folder, and uses /tmp instead, which is mounted noexec, causing compilation errors for some packages. I have to use the full path to the folder instead, which is different for each student.
The workaround I know of is to set TMPDIR in Bash and export it before calling R, I would like to know if there is a simpler way that does not depend on the actual home directory path. Ideally, I would like to give all students the same .Renviron file, and not have them use the shell at all.
I know it has been a while since you posted this question, but I have been using it like this:
TMPDIR = "${HOME}/tmp"

Defining a 'scripts' directory in R?

I am working with R in several directories containing model output I'd like to analyse and plot. I maintain a single 'scripts' directory for this project.
I'd like to be able to 'point' an environment variable at this scripts directory so that I could tab complete source(...) commands. Is this a possibility?
So far, I've managed to create an RPATH environment variable, and have written a function in my .Rprofile which lists the directory's contents without me having to type it out. I can't quite figure how I'd get tab completion though.
Any help/advice would be greatly appreciated.

Resources