[ACCEPTED]-How to get R to recognize your working directory as its working directory?-working-directory
You should copy shortcut to R (R.lnk file) to 14 desire folder. Then in "Properties" (right 13 mouse button -> last option) delete anything 12 in field "Start in..." in second 11 tab ("Shortcut"?). If you start 10 R with this shortcut working directory will 9 be that one where the shortcut is.
I don't 8 have english version of Windows so I'm not 7 sure about field names, but they should 6 be easy to find.
Similar questions were in 5 R-windows-faq:
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 4
The working directory is the directory from 3 which Rgui or Rterm was launched, unless 2 a shortcut was used when it is given by 1 the `Start in' field of the shortcut's properties.
You could use an environmental variable. This 4 can work with
Sys.setenv(). For instance:
> Sys.setenv(R_TEST="testit") > Sys.getenv("R_TEST") R_TEST "testit"
If you 3 sent the variable in your script, you should 2 be able to access it from within, and then 1 call
setwd() on that output.
Save your workspace to the desired directory 2 and thereafter you just open the workspace 1 from Windows explorer.
I put the following line in front of my 10 scripts and it allows me to work across 9 my computers.
where ~ is = to your home 8 directory.
Sys.setenv(HOME = "path") or
Sys.setenv(R_USER = "path") can both set the home directory.
In 7 my case, I work on several windows boxes, each 6 have fairly different directory structures, but 5 by setting the home directory properly I 4 can sync code between computers and have 3 them run properly on each one since where 2 I run my R projects have similar directory 1 structures.
If you're using Emacs/ESS, this isn't a 8 problem. I navigate to the directory where 7 my R script is located, open it, then start 6 an R ESS process. An R console pops up 5 with the current directory as R's working 4 directory.
If you haven't converted to Emacs/ESS, I 3 recommend it. (Though to prevent a flame 2 war, I also note there are similar options 1 for Vi users.)
Hope that helps.
Just a detail: instead of reversing the 4 slashes as you say, just add another backslash. Two 3 of these \\ works the same way as one of 2 these /. That makes it at least a little 1 easier.
Insert the following command 8 into your
.Rprofile file (usually in your home directory):
Now 7 your default working directory will be whatever 6 directory you launched R from. Keep in 5 mind you can also set up default workspaces 4 in different directories by saving your 3 workspace image as
.RData wherever you plan to 2 launch R (startup sources
.Rprofile before searching 1 for
.Rdata in the
To set the R work directory like the current 4 directory of the R script that I'm working, I 3 always use a combination of the commands 2
setwd(), like this:
path <- getwd()
If you want learn more 1 about it, see this article.
To set working directory in R Studio: Refer detailed slide deck with screen-shots 8 here.
- 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)
- 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
- 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 7 Tools --> Global Options --> R General 6 [Basic] --> Default Working Directory 5 (when not in a project) browse the folder 4 which you want to set as the working directory, click 3 on 'Apply' and 'OK'
However, the efficient 2 & better way to organize your work is 1 to create projects & use version control.
More Related questions