27

I use R under Windows on several machines.

I know you can set the working directory from within an R script, like this

setwd("C:/Documents and Settings/username/My Documents/x/y/z")

... but then this breaks the portability of the script. It's also annoying to have to reverse all the slashes (since Windows gives you backslashes)

Is there a way to start R in a particular working directory so that you don't need to do this at the script level?

Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
Dan Goldstein
  • 24,229
  • 18
  • 37
  • 41

10 Answers10

16

You should copy shortcut to R (R.lnk file) to desire folder. Then in "Properties" (right mouse button -> last option) delete anything in field "Start in..." in second tab ("Shortcut"?). If you start R with this shortcut working directory will be that one where the shortcut is.

I don't have english version of Windows so I'm not sure about field names, but they should be easy to find.

Similar questions were in R-windows-faq:

2.5 How do I run it?

2.10 How can I keep workspaces for different projects in different directories?

2.14 What are HOME and working directories?

In 2.14 is mentioned that

The working directory is the directory from which Rgui or Rterm was launched, unless a shortcut was used when it is given by the `Start in' field of the shortcut's properties.

Marek
  • 49,472
  • 15
  • 99
  • 121
6

You could use an environmental variable. This can work with Sys.getenv() and Sys.setenv(). For instance:

> Sys.setenv(R_TEST="testit")
> Sys.getenv("R_TEST")
  R_TEST 
"testit" 

If you sent the variable in your script, you should be able to access it from within, and then call setwd() on that output.

eykanal
  • 26,437
  • 19
  • 82
  • 113
Shane
  • 98,550
  • 35
  • 224
  • 217
  • Do the environmental variables set this way persist across R sessions? How about full computer restarts? – eykanal Jan 13 '12 at 18:50
  • No, but it will if you add this into your .Rprofile file. Alternatively, you can get system variables from the OS (eg. in Windows, you can store you values in environment variables). – Shane Jan 16 '12 at 20:08
5

Save your workspace to the desired directory and thereafter you just open the workspace from Windows explorer.

Rob Hyndman
  • 30,301
  • 7
  • 73
  • 85
4

I put the following line in front of my scripts and it allows me to work across my computers.

setwd(path.expand("~/path/to/working/directory/") )

where ~ is = to your home directory.

Sys.setenv(HOME = "path") or Sys.setenv(R_USER = "path") can both set the home directory.

In my case, I work on several windows boxes, each have fairly different directory structures, but by setting the home directory properly I can sync code between computers and have them run properly on each one since where I run my R projects have similar directory structures.

MERose
  • 4,048
  • 7
  • 53
  • 79
Robert
  • 838
  • 6
  • 8
3

If you're using Emacs/ESS, this isn't a problem. I navigate to the directory where my R script is located, open it, then start an R ESS process. An R console pops up with the current directory as R's working directory.

If you haven't converted to Emacs/ESS, I recommend it. (Though to prevent a flame war, I also note there are similar options for Vi users.)

Hope that helps.

Christopher DuBois
  • 42,350
  • 23
  • 71
  • 93
3

Just a detail: instead of reversing the slashes as you say, just add another backslash. Two of these \\ works the same way as one of these /. That makes it at least a little easier.

Ehva
  • 201
  • 2
  • 5
  • +1 Ehva : This is the easiest way when you're working plain old R and want to set your working directory only once. – PavoDive Nov 10 '14 at 16:33
2

For Ubuntu:
Insert the following command into your .Rprofile file (usually in your home directory):

setwd(Sys.getenv("PWD"))

Now your default working directory will be whatever directory you launched R from. Keep in mind you can also set up default workspaces in different directories by saving your workspace image as .RData wherever you plan to launch R (startup sources .Rprofile before searching for .Rdata in the cwd).

john_science
  • 6,325
  • 6
  • 43
  • 60
Brian Mack
  • 21
  • 1
  • **THIS** is the answer I have been seeking for OSX. (I used the command in my `Rscript` file to find the directory where invoked, not in my `.Rprofile`...) – beroe Oct 09 '18 at 19:54
1

To set the R work directory like the current directory of the R script that I'm working, I always use a combination of the commands getwd() and setwd(), like this:

path <- getwd() setwd(path)

or

setwd(getwd())

If you want learn more about it, see this article.

Cheers,

[]'s

0

To set working directory in R Studio: Refer detailed slide deck with screen-shots here.

  1. Using setwd(): windows users would need to replace backward slashes '' with forward slashes '/' or double backward slashes '\' You can do the former using find & replace (Short-cut: Ctrl+F)
  2. Another option: Go to Session --> set working directory --> choose working directory & browse the folder which you want to set as the working directory, click on open
  3. Quickest method (my favorite) use the shortcut 'Ctr+Shift+H' (on windows system), browse the folder which you want to set as the working directory, click on open

To set a permanent working directory (when not in a project) in R Studio: Refer my quick video on the same: https://youtu.be/hMjzO4bAi70

Go to Tools --> Global Options --> R General [Basic] --> Default Working Directory (when not in a project) browse the folder which you want to set as the working directory, click on 'Apply' and 'OK'

enter image description here

However, the efficient & better way to organize your work is to create projects & use version control.

Dr Nisha Arora
  • 632
  • 1
  • 10
  • 23
-1

Put a shortcut for the R gui into your desired directory. Right-click and look at the shortcut properties. Delete the entry for "Start In" and click OK. When you launch the R gui from this shortcut the default directory will be the folder from which you have launched. Copy/paste this shortcut wherever you desire.