42

I consulted http://www.rstudio.com/ide/docs/using/workspaces and tried the setwd code and clicking on More --> Set as working directory options. However, the next time I closed and opened RStudio, it did NOT change my directory to the one I wanted.

How do I set the directory so I don't have change it each time I open RStudio?

Thanks.

user3380666
  • 439
  • 1
  • 4
  • 4

4 Answers4

60

Not sure if this is what you're looking for, but under Tools | Global Options... (alt-TG) there is a way to set the default directory.

jlhoward
  • 58,004
  • 7
  • 97
  • 140
  • 8
    FYI, selected Apply, but still had to restart to get changes to take effect. – Les Feb 12 '15 at 19:26
  • I had the same problem. Please see the link below: https://stackoverflow.com/questions/47916268/how-to-set-working-directory-pemanently-in-spyder-rstudio/47927925#47927925 @jhoward solution worked for me. But, yes need to restart the application to get changes effective. – WpfBee Dec 23 '17 at 01:13
8

On R (3.1.2) for Windows (Win7) I did the following and it worked:

1- Navigate to the file "Rprofile.site" in the R-folder in my case: C:\Program Files\R\etc\Rprofile.site where "etc" is indeed the given name of the sub-folder.

2- Open the file Rprofile.site in a text-editor with administrator privileges (I did in Notepad) and insert right at the top of the file in the first line the following command which tells R to set the working directory at start-up to the PATH you are specifying within the brackets (be sure you use double backslashes "\\" for Windows!), in my case:

setwd("C:\\Users\\FWA\\Documents\\PROGR&MOOCS\\R_coursera")

3- Save the file which then looks like this:

setwd("C:\\Users\\FWA\\Documents\\PROGR&MOOCS\\R_coursera")

# Things you might want to change

# options(papersize="a4")
# options(editor="notepad")
# options(pager="internal")

# set the default help type
# options(help_type="text")
  options(help_type="html")

# set a site library
# .Library.site <- file.path(chartr("\\", "/", R.home()), "site-library")


# set a CRAN mirror
# local({r <- getOption("repos")
#       r["CRAN"] <- "http://my.local.cran"
#       options(repos=r)})

# Give a fortune cookie, but only to interactive sessions
# (This would need the fortunes package to be installed.)
#  if (interactive()) 
#    fortunes::fortune()

4- Fire up the R-Studio and at the command prompt type the 'get working directory' command like this:

getwd()

5- Your R-Studio should now return the working directory path you previously have specified. In my case R returns this:

[1] "C:/Users/FWA/Documents/PROGR&MOOCS/R_coursera"

6- You're done.

Note: this changes the working directory for your R environment, not only for R-Studio.

Giuseppe Romagnuolo
  • 3,362
  • 2
  • 30
  • 38
soirbleu
  • 87
  • 1
  • 2
  • He is asking about RStudio, you are talking about Rgui.exe. Moreover even with Rgui.exe in Win7 you need to change the "Start in" folder into the properties of the Rgui.exe shortcut. The Rprofile.site does not work. – Marco Demaio Apr 08 '15 at 10:59
  • In addition to Marco's answer, you may have to remove `--cd-to-userdocs` from the end of the shortcut's target property. – Darrel Holt Jun 13 '18 at 04:45
7

Session -> Set Working Directory -> Choose Directory ... or shortcut (Ctrl+Shift+H)

Or you can set your working directory on console like that = setwd("C:/Users/burak/Desktop/R")

Burak URAY
  • 109
  • 1
  • 5
4

I faced the same problem. There are different way to changes you working directory in RStudio, some of them are, for example:

setwd("~/R/R Programming") #Note that ~ represents in a short way your document folder (or whatever folder you are using for) and what its before it.

Set our working directory without using squigless:

setwd("C:\Users\Usuario\Documents\R\R Programming")

Does the same thing using forward slashes:

setwd("C:/Users/Usuario/Documents/R/R Programming")

I got the information from the following link (though there are another options you can find in the video, recommended): https://www.youtube.com/watch?v=x_QnNhQ3jfY

Community
  • 1
  • 1