6

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, so one has to run setwd() every time). In RStudio, there is a relevant option in Tools>Global Options>General. The other solutions seem to involve editing the Rprofile.site file. However, all of this requires the user to be capable of finding the Rprofile and editing it, or browsing through the settings, and all the while not messing up.

What I need is a solution for fools students who have no idea how to do this. One might say customizing the environment would be good practise, but this is a very short course, and I'd like it to be as painless as possible to the computer-illiterate souls in the audience.

I have already written a script that downloads all the necessary packages for the course, loads the script in RStudio, downloads and loads a workspace with data and functions. They just have to run it once after installing R+RStudio. For a moment I though this would be a good idea:

cat("setwd(\"the desired working directory\")", file=file.path(Sys.getenv("R_HOME"), "etc", "Rprofile.site"), append=T)

...but this throws Permission Denied, at least under Windows (Program files are protected I guess). The desired solution should be platform independent (most of them have Windows, but some might have Macs or Linux). But most importantly, it should consist of just pasting the script in the console and pressing enter, nothing more complex (hence the fool-proof part of the title).

user3554004
  • 1,044
  • 9
  • 24
  • 1
    `setwd(Sys.getenv("R_HOME"))` goes to the installation directory while `setwd(Sys.getenv("HOME"))` will go to to the user's Documents folder in Windows and the user's home folder in Linux (and I'm guessing on Mac also). – maccruiskeen Jul 15 '15 at 15:16
  • @choff I'm aware; I already tried setting Sys.setenv("HOME"=...) but it gets reset on R restart. – user3554004 Jul 15 '15 at 15:43
  • hope the below helps. It's a simple way to use R in windows and custome your starting folder withoput any `setwd`. Feel free to ask if you have questions on it. – Colonel Beauvel Jul 15 '15 at 15:44
  • @ColonelBeauvel thanks for the input, but just knowing the audience tells me that running one script (copy and paste) will be easier for them than making a shortcut and then properly editing a property of the shortcut (also, I wouldn't know if this worked the same on unix-likes). – user3554004 Jul 15 '15 at 16:10

3 Answers3

5

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)
  }
}
nrussell
  • 18,382
  • 4
  • 47
  • 60
  • 1
    Worked! Indeed, choosing the working directory is the only interactive step I'll have them do during the set-up phase (using choose.dir(), which pops up a folder browsing interface). For some reason wd=choose.dir() won't work, the \\ get changed to single \ and saved in the Rprofile, which will throw an error on R restart. But setwd(choose.dir) and then wd=getwd() works fine. – user3554004 Jul 15 '15 at 16:08
  • 1
    @user3554004 I'm glad this worked out for you. It should be noted that `choose.dir()` is a Windows specific function, if you weren't aware of that. I haven't used it before, it looks like there is a platform independent version of `choose.dir` available in the `rChoiceDialogs` package - see [here](http://www.inside-r.org/packages/cran/rChoiceDialogs/docs/rchoose.dir). Granted, this package would have to be installed, so that may be an extra step / complication in the process. – nrussell Jul 15 '15 at 16:21
  • 1
    No, I actually wasn't aware of that, +1. – user3554004 Jul 15 '15 at 16:44
1

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

Colonel Beauvel
  • 30,423
  • 11
  • 47
  • 87
1

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.

Greg Snow
  • 48,497
  • 6
  • 83
  • 110
  • That is not a bad option (server), but internet access is one thing I won't have on the premises (to say it's in the woods wouldn't be far from the truth). Having learned from the comments that choose.dir() is windows-specific, I might actually take your suggestion to just leave the working directory default for the few non-windows users (downside is that they'll probably have other stuff in there as well). – user3554004 Jul 15 '15 at 16:42