Defining a 'scripts' directory in R? - 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.

Related

Setting Rmd directory

I'm new to R. I usually work my projects in a setting characterized by a directory in which I assign datasets, code and graphics specific folders. This is:
Main directory ~Project
Code ~Project\Code
Within my setup chunk I declare:
knitr::opts_knit$set(root.dir = "~Project")
knitr::opts_knit$get("root.dir")
However, in any following chunk where, for example I wish to load a dataset stored in 'Data' folder as:
read.csv("Data/series.csv",header=TRUE,check.names=FALSE)
I get a warning message on failure to find such directory.
Any advice? Thank you very much!
You need a ".." before that to tell R Studio first to go up a level (to the main directory):
read.csv("../Data/series.csv",header=TRUE,check.names=FALSE)
If you want to check what directory you're in, try running code like print(getwd()) in your chunk.
It's also not clear whether your error comes when you try to knit the document, or just when working on the Project; that does make a difference.

I have assigned a variable from my r shiny file to the global environment but my script file cannot access it

My aim is to upload a dataset within my application(R shiny app) and perform computations on this dataset using my external R script. Given that both my application and script are in the same directory, I have tried to change the scope of the variable containing the script path and put into the global environment so that it is accessible by both files. But when I run my script file, at first it has to read the file but however, it says that the variable has not been found, even if it is found inside the global environment. Any help would be greatly appreciated. I would like to add that I am new to the R language and if ever there are better ways of doing it, please consider commenting. Thank you in advance.
My code segment is here:
#Inside the Application's code
observeEvent(req(input$datasetRNACount),{
tryCatch({
infileDatasetRNACount <-input$datasetRNACount
if(is.null(infileDatasetRNACount)){return()}
else{
file.copy(infileDatasetRNACount$datapath,file.path(getwd(), infileDatasetRNACount$name),overwrite = TRUE)
# put the variable in the global environment
rawCountFile<<- input$datasetRNACount$datapath
}
},
# The script code
dataseq <- read.delim(rawCountFile)
The object "rawCountFile" was not found as shown
While the variable is present inside the global environment as shown

Setting R environmental variable in Tortoise SVN

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

How to converge multiple R files into one single file

Situation
I wrote an R program which I split up into multiple R-files for the sake of keeping a good code structure.
There is a Main.R file which references all the other R-files with the 'source()' command, like this:
source(paste(getwd(), dirname1, 'otherfile1.R', sep="/"))
source(paste(getwd(), dirname3, 'otherfile2.R', sep="/"))
...
As you can see, the working directory needs to be set correctly in advance, otherwise, this could go wrong.
Now, if I want to share this R program with someone else, I have to pass all the R files and folders in relative order of each other for things to work. Hence my next question.
Question
Is there a way to replace all the 'source' commands with the actual R script code which it refers to? That way, I have a SINGLE R script file, which I can simply pass along without having to worry about setting the working directory.
I'm not looking for a solution which is an 'R package' (which by the way is one single directory, so I would lose my own directory structure). I simply wondering if there is an easy way to combine these self-referencing R files into one single file.
Thanks,
Ok I think you could use something like scaning all the files and then writting them again in the same new one. This can be done using readLines and sink:
sink("mynewRfile.R")
for(i in Nfiles){
current_file = readLines(filedir[i])
cat("\n\n#### Current file:",filedir[i],"\n\n")
cat(current_file, sep ="\n")
}
sink()
Here I have supposed all your file directories are in a vector filedir with length Nfiles, I guess you can adapt that

multiple working directories in R

I wrote a list of different functions and script and I put them in some subfolders of the working directory so I can divide all my functions in arguments (descriptive statistic, geostatistic, regression....)
When I type source("function_in_subfolder") R tells me that there is no function.
I understood that it happens because functions have to stay in the working directory.
Is there a way to set also subfolders of the working directory as source for the functions (let's say in a hierarchical way)?
The source function has a chdir argument which, if set to TRUE, will set the working directory to that where the script resides. The new work directory is valid for the duration of the execution of the script, after that it is changed back. Assumung the following structure
main.R
one/
script.R
two/
subscript.R
you can call source("one/script.R", chdir=T) from main.R and, in script.R, call source("two/subscript.R", chdir=T).
However, by default, R will start its search from the current directory. There is no such thing as a "list of search paths" like, e.g., the PATH environment variable, although apparently someone attempted to create such a thing. I would strongly advise against attempting to find a script file "anywhere". Instead, indicate precisely which script is to be run at which point. Otherwise, name clashes resulting from simply adding a file to your scripts can lead to unpredictable behavior which is also difficult to debug.
One solution is to use list.files to get the full path of your function. for example:
myfunction.path <- list.files(getwd(),
recursive=TRUE,full.names=TRUE,
pattern='^myfunction.R$')
Then you can call it :
source(myfunction.path)
The recursive call of list.files can be expensive, so maybe you should call it once at the beginning of your analyze for example and store all functions paths in a named list. And BE CAREFUL the result can not be unique if you create 2 sources files withe the same name in 2 differents sub-directories.

Resources