There is plenty of information on how to change the default working directory in R (every time R or RStudio is started, the working directory would change back to default, s
What about something like
set_default_wd <- function(wd = getwd()) {
text <- paste0(
'local({ setwd("', wd, '") })')
##
if (Sys.info()["sysname"] == "Windows") {
write(
text,
file = paste0(Sys.getenv("HOME"), "\\.Rprofile"),
append = TRUE)
} else {
write(
text,
file = paste0(Sys.getenv("HOME"), "/.Rprofile"),
append = TRUE)
}
}
##
#R> set_default_wd() #set_default_wd("some/file/path")
This should work on Windows and Unix-like systems, and avoid any permissions issues. Really the only requirement on the user's end is to specify a valid file path, which they should (hopefully) be able to work out.
It may be worthwhile to have the option of overwriting the $HOME/.Rprofile (instead of forcing lines to be appended) in case a malformed file path is given, etc...
set_default_wd <- function(wd = getwd(), overwrite = FALSE) {
text <- paste0(
'local({ setwd("', wd, '") })')
##
if (Sys.info()["sysname"] == "Windows") {
write(
text,
file = paste0(Sys.getenv("HOME"), "\\.Rprofile"),
append = !overwrite)
} else {
write(
text,
file = paste0(Sys.getenv("HOME"), "/.Rprofile"),
append = !overwrite)
}
}