'How does the working directory setting affect the created R project in RStudio?

How does the working directory setting affect the created R project in RStudio? I get it but can't explain it exactly.



Solution 1:[1]

All file paths referenced by a script are relative to the session's current working directory. Suppose we need to load a file located at ~/my_project/file.csv. Without an explicit working directory, you might need to specify the full file path:

read_csv('~/my_project/file.csv')

But with the working directory set, you could just do:

setwd('~/my_project/')
read_csv('file.csv')

RStudio is also aware of the current working directory. Press Tab inside a pair of empty quotes, and the autocomplete will display files in the current working directory. Creating or loading an RStudio project file also sets the working directory to the current location of the project file. It's also worth keeping in mind that that working directory is set per session, not per script (this sometimes causes confusion when users have multiple scripts open at once and mistakenly expect each script to "remember" a different working directory).

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