'How do I change the directory displayed in the RStudio file pane / navigator using code?

I am writing an R package for a lab notebook. One function makes a new directoy with some Rmd files, opens the files with file.edit, and changes the current working directory to the new directory. It would be great if I could also change the "Files" pane of RStudio to display that new folder from the function's code. Is this possible? I have looked through the rstudioapi package. It does similar things (e.g. the viewer function), but I cant find a way to change the path in the file navigator pane.



Solution 1:[1]

You need to change one of your [Rprofile] files in your system. The Rprofile are loading at start of R in the following order:

  • Rprofile.site (global)
  • Rprofile (current directory)
  • Rprofile (users home directory)

Add the following in your Rprofile (change to your path,though):

setwd('/your/path')

Logout/login of Rstudio and you should get the files pane of Rstudio to reflect what you stated in your Rprofile file.

Solution 2:[2]

You can set the working directory using setwd(). Inside of setwd() you need to provide the path to the folder you want to be your working directory, such as:

setwd("C:/Desktop")

I typically set the working directory every time I open R studio.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Toolbox
Solution 2 Oli