- 1
-
Function to look at the current working directory, which is where my
.qmdfile lives
[1] "/Users/wakim/Library/CloudStorage/OneDrive-OregonHealth&ScienceUniversity/Teaching/Classes/PUBH_523_26Su/PUBH_523_26Su_site/lessons/10_Rproj_wds"
here package?Answer: Reproducibility!
Research data and code can reach the same results regardless of who is running the code
We want to set up our work so the entire folder can be moved around and work in its new location
Working directory
The working directory is the folder R is currently “looking at.” When you want to locate a file, it is the starting point for all file paths in your session.
.qmd file lives
[1] "/Users/wakim/Library/CloudStorage/OneDrive-OregonHealth&ScienceUniversity/Teaching/Classes/PUBH_523_26Su/PUBH_523_26Su_site/lessons/10_Rproj_wds"
.. to go up one level, and then I can access the data_lessons folder and the hrs_data.rds file.
/Users/wakim/Library/CloudStorage/OneDrive-OregonHealth&ScienceUniversity/Teaching/Classes/PUBH_523_26Su/PUBH_523_26Su_site/lessons/ data_lessons/hrs_data.rdsYou can set the working directory by hand, but you should NOT!
When you set a wd manually, it will break if you move the folder or if you are on a different computer
qmds, running code in the console and rendering result in two different working directories
R projects and the here package can help fix both of these issues!
R Project
An R Project is a file (.Rproj) that sits in a folder and tells RStudio: “This is where I want my working directory to be set automatically.”
I can add an R Project to my PUBH_523_26Su folder. The R project will show up as PUBH_523_26Su.Rproj.
PUBH_523_26Su/
├── PUBH_523_26Su.Rproj
├── admin/
├── data_lessons/
├── data_practice/
├── data_project/
│ ├── data_raw/
│ └── data_processed/
├── lessons/
├── practice/
├── project/
└── README.txt
When you open the .Rproj file, RStudio sets the working directory to that folder — every time, on any computer.
Let’s see an example!
.Rproj file in your folder
Opening a script/qmd directly (by double-clicking it) does not activate the project or set the working directory
We haven’t fixed this issue yet:
- When you use
qmds, running code in the console and rendering result in two different working directories
- Let’s walk through an example!
We need the here package to fix this issue!
here packageFor scripts in subfolders, use here() to build paths from the project root
The here() function makes your code start at the R project folder whether we are working in the console or rendering a qmd document
here() returns same thing in console and on renderhere package to use the here() function
here() function returns the path to the project root folder, regardless of where the script lives
[1] "/Users/wakim/Library/CloudStorage/OneDrive-OregonHealth&ScienceUniversity/Teaching/Classes/PUBH_523_26Su/PUBH_523_26Su_site"
here() to navigate to files in our project folder[1] "/Users/wakim/Library/CloudStorage/OneDrive-OregonHealth&ScienceUniversity/Teaching/Classes/PUBH_523_26Su/PUBH_523_26Su_site/lessons/01_Welcome"
[1] "/Users/wakim/Library/CloudStorage/OneDrive-OregonHealth&ScienceUniversity/Teaching/Classes/PUBH_523_26Su/PUBH_523_26Su_site/images/PUBH_R.png"
[1] "/Users/wakim/Library/CloudStorage/OneDrive-OregonHealth&ScienceUniversity/Teaching/Classes/PUBH_523_26Su/PUBH_523_26Su_site/images/PUBH_R.png"
.Rproj file or opening in R Studio
setwd(), ever
here() to make relative file pathsR Projects \(+\) here() \(+\) good folder structure
\[=\]
analysis that runs reliably anywhere, for anyone
R projects and working directories