How can I set different directory to the current directory in R? - 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"))

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.

here::here Pulls from Incorrect Directory and Ignores .here File in R

I am having trouble loading my .csv files with here::here.
The here package is setting its default directory as:
'Z:/PSE/Analysis/One'
My working directory is:
Z:/PSE and I have put a .here file in that directory. Here is the message I get after using dr_here():
Here() starts at Z:/PSE/Analysis/One, because it contains a file matching [.]Rproj$ with contents matching ^Version: in the first line
I thought the here function started at the current working directory, which is Z:/PSE, in which it should recognize the .here file and follow the path that I specify in the remainder of the here function?

Need help manipulating directory string in a function

I'm having trouble getting the current the current directory of my R Shiny app. I use the getSrcDirectory function to get the script's current directory and then try to manually modify it into the appropriate format to pass to xlsx::loadWorkbook, in order to load a .xlsx file. Here is the pertinent code:
#get the directory of the script by creating an empty function.
src<-getSrcDirectory(function(x) {x})
wb <- loadWorkbook(file = c(gsub("/", "\\\\", c(src, "/www/NJ2012.xlsx")),
"\\www\\NJ2012.xlsx"))
I'm trying to get the file directory to look like this: C:\\Users\\misha\\Desktop\\Accessible Project\\R_Econ_App\\www\\NJ2012.xlsx.
My script is in a folder called R_Econ_App, so it should be included in the src variable. I concatenated an additional string to the src string to provide some more information about where certain files are in certain sub directories. I use gsub in an attempt to replace "/" with "\" because that's what the file directory passed into loadWorkbook has to look like.
Can you please help me figure out where my mistake is and how to fix it?
Don't bother with gsub. Just use file.path to join your directory and file path
loadWorkbook(file=file.path(src, "www", "NJ2012.xlsx"))

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

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

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.

Resources