What is a fool-proof way of permanently setting R working directory?

后端 未结 3 799
渐次进展
渐次进展 2020-12-20 19:28

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

相关标签:
3条回答
  • 2020-12-20 20:04

    The most foolproof option may be to install an Rstudio server, configure it with all the packages you want, then give each student an account on the server. That way each student starts with an identical setup and their own directory/folder. Students only need internet access to use it. You could then provide instructions for those students who are adventurous enough to install R on their own computer (and are more likely to be able to follow instructions to set it up properly).

    You could also try your cat option, but put the instructions into .Rprofile in 'HOME' instead of .Rprofile.site in 'R_HOME'.

    Or you could put the the code in a .First function and have them save their workspace in the default location, then when they run R from the default location, the working directory would be changed by .First.

    Or you could just leave them working at the default directory.

    0 讨论(0)
  • 2020-12-20 20:05

    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)
      }
    }
    
    0 讨论(0)
  • 2020-12-20 20:19

    You can create a shortcut of RGui.exe on the toolbar.

    Then right-click the Icon, right-click on R, Properties, and in the tab Shortcut, you can set Start in: the folder you want.

    For example C:/Users/myStudentID/Documents/dev

    enter image description here

    0 讨论(0)
提交回复
热议问题