Determining current file's location in R to include file from same directory? - r

I want to be able to source() a file which includes a different file in its same directory, but I don't want to have to set the working directory from the R-prompt before running this file:
> getwd()
[1] "/Users/myser"
> source("/Users/myuser/workspace/myproject/myfile.r")
Inside /Users/myuser/workspace/myproject, there would be myfile.r and my-utils.r. myfile.r calls source('my-utils.r') from within it.
Other programming languages can determine the current file's path. Does R have something similar? Example:
cur_dir <- sys.get_current_file_path()
source(file.path(cur_dir, "my-utils.r"))

source("/Users/myuser/workspace/myproject/my-utils.r", chdir=TRUE)
When chdir option is set to true and the source file parameter is a full path, the directory of file will be used as the working directory while sourcing the file.
NOTE: cur_dir <- sys.get_current_file_path() doesn't make much sense because pathnames are not unique.

Related

How to reference a data file not in a working directory

I'm using R and I know how to define a working directory:
setwd("C:/temp/")
I have a data file that is not in this folder. How can I access it? In SAS, it would be this:
libname x1 "C:\temp";
libname x2 "C:\temp\data folder";
There are several ways to solve this problem.
One is to save the current directory in a variable, temporarily change dir, then return to the current directory.
old_dir <- getwd()
setwd("C:/temp/data folder")
#code
setwd(old_dir)
Another is to use function file.path to make a fully qualified file name.
path <- "C:/temp/data folder"
filename <- "datastuff.csv"
fullname <- file.path(path, filename)
fullname
#[1] "C:/temp/data folder/datastuff.csv"
one way is just with another line,
setwd("C:/temp/newfolder")
read_csv(...)
and then again your original to get you back
setwd("C:/temp/")
In my opinion, library(here) is a nice one for easily setting relative paths and working up and down directories.
You can use the absolute file path or the relative file path in the same way that you would use the file name if the file were in your current working directory.
For a file in your current working directory:
source("myFile.R")
For a file in a directory that is a peer to your current working directory:
source("../folder/myFile.R")
In this relative path, the ../ indicates "up one directory". Relative file paths are relative to your current working directory.
For a file anywhere on your system:
source("C:/full/path/to/myFile.R")
Absolute paths start with C: on Windows, or / ("root") for *nix systems.

How can I set different directory to the current directory in R?

I'm trying to open a file that does not exist in my current directory. This file is named testFile.r existing in the data folder in my current directory. I tried file.path("./data") and then wanted to show the file with this command file.show("testFile.r") but it gives this error:
Error: File testFile.r does not exist.
And the command getwd() gives the previous current directory. So any thoughts on this?
You change your current directory using the command setwd(new_directory)
(replacing new_directory with the path to the directory you want).
If you'd rather stay in your current directory, just do
file.show("./data/testFile.r")
To keep track of multiple paths, you can save each as a variable and use the paste function to reference the file:
path1 <- "./data/"
path2 <- "./second_folder_name/"
file.show(paste0(path1, "testFile.R"))
file.show(paste0(path2, "testFile.R"))

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

setwd() in the current working dir

I have a list of folders. In each folder there's an R identical script that have to run on files into the folder. I wrote the script one time and copied the script in each folder. The problem is that I have a list of around 100 folders so it is impossible to me to setwd() in the current working dir by hand. I would like to know if it is possible to set current working dir with for example a "." in this way:
setwd("/User/myname/./")
or in another easy way that tells R the current working dir instead of typing every time the folder name.
how about this?
# set the working directory to the main folder containing all the directories
setwd( "/user/yourdir/" )
# pull all files and folders (including subfolders) into a character vector
# keep ONLY the files that END with ".R" or ".r"
r.scripts <- list.files( pattern=".*\\.[rR]$" , recursive = TRUE )
# look at the contents.. now you've got just the R scripts..
# i think that's what you want?
r.scripts
# and you can loop through and source() each one
for ( i in r.scripts ) source( i )
As far as I understand, you want to trigger a batch of R scripts, where the scripts are distributed across a number of folders.
Personally, I would probably write a shell script (or OS equivilent) to do this, rather than doing it in R.
for dir in /directoriesLocation/*/
do
cat $dir/scriptName.R | R --slave --args $arg1 $arg2
done
where $dir is the the location of all directories containing the R script scriptName.R
In addition to the other great answers the source function has a chdir argument that will temporarily change the working directory to the one that the sourced file is in.
One option would be to create a vector with the filenames (including paths) for each of your script files, using list.files and/or other tools. Then source each of those files and letting source with chdir handle setting the working directory for you.

Can I work with multiple working directories in R?

Can I work with parallel working directories in R, or can I change the working directory in a loop to access the files from different folders?
I find it easier to have a single working directory. You find out what that is using the
getwd()
function. Typically, my working directory is something like:
~/colin/project1/R
You can change your working directory using
setwd()
You can easily access other files using the full path. In particular, I find
##List files in current directory
list.files()
##Give full path
list.files(full.names=TRUE)
##list files in the species1 directory
list.files("species1/", full.names=TRUE)
very handy.
Don't change the working directory in a loop, loop over the directories and use file.path to get to the file you want. Something like:
for(path in c("data1","data2","data3")){
for(file in c("file1.txt","file2.txt")){
fullPath = file.path(path,file)
doSomethingWith(fullPath)
}
}
That will loop over data1/file1.txt, data1/file2.txt and so on. Note it will also handle differences between path separators in different operating systems - don't try and paste file path components together with paste because you'll get it wrong.

Resources