Confused about the working directory...

Hello,

I have a very novice question.

I understand that the current working directory is the directory listed under the Files pane in RStudio. It is the directory we are currently in, correct?

What is the difference and use between the default working directory (set in Tools, Global Options, etc.) vs the current working directory?
When we save a R script, does it get automatically saved in the current working directory, correct? Does the default working directory always become the current working directory every time relaunch RStudio?

I have create a folder called MyRscripts at "C:\MyRscripts" and made it to be the default working directory...

Thanks!

Thank you!

This is not correct.
The working directory is the assumed path from which relative paths will be constructed. The file pane is independent, any folder can be browsed and the working directory wont change from mere browsing.

1 Like

To see what current working dictionary is do

getwd()

If you wish to change it is a session do

setwd("myfilepath") 
2 Likes

Thank you.

So the working directory is simply this: we start a new session in RStudio, create notebooks, markdown docs, quarto docs and when we click "save", those documents get automatically saved in the working directory.

If we want to save them elsewhere, we need to either change the working directory (to make things automatically every time we restart Rstudio) or just click save and specify the destination folder like we do in Windows or Mac...

Thank you

Not exactly: there are two independent working directories: the one for the RStudio File Pane, and the one for the R process.

What you describe is correct for the file pane: when you open a new project, the File Pane will be set by default to the directory where the .Rproj file is located. If you create a script and save it, it will save there. You can change it by navigating within the File Pane.

But when you open your RStudio Project, the Console Pane starts a R session. This one has also a working directory. By default, it's also the directory where the .RProj file is saved. So at the beginning of your session, both of these working directories are the same. This one can be seen and changed with the commands jkrideau gave.

But they can change independently. If, in, the File Pane, you create a new directory and click on it, it will open in the File Pane. But if you type getwd() in the Console Pane, you will see the one from the R session has not changed. Now, in the File Pane, if you click on "More>Go To Working Directory", the File Pane will navigate to the current R session working directory, they match again. Conversely, if you navigate somewhere with the File Pane then click "More>Set As Working Directory", it will automatically write a setwd() command in the R console, changing the R session's working directory.

The R session working directory is important: if you use a command like read_csv(), you have to give the path relative to the R session working directory.

Things get even more complicated if you compile an Rmarkdown document: a new R session is created, and the compilation happens in there. But this session can have yet another working directory, typically where the Rmarkdown document is located. That sometimes gives weird errors when you try to read a file within an Rmarkdown document that is saved in a subdirectory.