I recently got a new computer and moved all my work files over to it. The main issue is that the file structure is slightly different than from my previous computer. Therefore, for my R code to work correctly, I'd need to change the path in setwd()
for each of my files. Is there an efficient way to do this? Or is there a more efficient best practice for setting the directory or reading files into R?
CodePudding user response:
I highly recommend the here package. Much more efficient way to organize, find, read, and collaborate with/across R files compared to setwd()
, which is bound by local use and paths.
CodePudding user response:
1) If all the paths are set in setwd
commands then as a first step define your own setwd
that checks if the argument is an old path and if so replaces it with a new one. The software otherwise does not need to change. so it can be done quickly.
setwd <- function(dir) {
if (dir == "oldpath1") dir <- "newpath1"
else if (dir == "oldpath2") dir <- "newpath2"
# etc
base::setwd(dir)
}
2) To fix this for the future as well, instead of the above, define the paths as options and put them in your .Rprofile file.
setwd <- function(dir) {
if (dir == "oldpath1") dir <- getOption("MYPROJ_PATH1")
else if (dir == "oldpath2") dir <- getOption("MYPROJ_PATH2")
# etc
base::setwd(dir)
}
and in your .Rprofile
options(MYPROJ_PATH1 = "...whatever...")
options(MYPROJ_PATH2 = "...whatever...")
# etc
Then if you move computers again or change paths for any reason then it is just a matter of setting the options in the .Rprofile .
An additional benefit of this is that if you forget where things are such as when returning to a project that you have not worked on for some period of time the key paths are located in your .Rprofile for all your projects.
The .Rprofile is usually in the path shown by this R command
path.expand("~/.Rprofile")
but can be placed in certain other locations as discussed in ?Startup
.
3) Over time you might want to remove the setwd
command defined above and replace each use of setwd
using code like this:
myproj_path1 <- getOption("MYPROJ_PATH1")
setwd(myproj_path1)
Also you might be able to simplify things if everything in the project can be put in a single directory tree in which case you could just change the root of the tree and keep all other directories as fixed relative path offsets which do not change in moving to another computer. Thus there is only one root directory that needs to be changed each time you move.
root <- getOption("MYPROJ_ROOT")
path1 <- file.path(root, "relative_path1")
path2 <- file.path(root, "relative_path2")
Smaller projects can often do that but if there are several projects that share resources that likely won't be possible. For example you may have a database directory that is shared and other directories that are not. At any rate you can try to reduce the number of root paths as much as possible by fixing relative paths to the degree feasible and only changing roots.