Working in R and RStudio
work on more than 1 thing at a time
collaborate, communicate, distribute
start and stop
dedicated directory
RStudio Project
Git repo, probably syncing to a remote
All necessary files contained in the project and referenced relatively
All necessary outputs are created by code in the project
All code can be run in fresh sessions and produce the same output
Does not force other users to alter their own work setup
Jenny will come into your your office and SET YOUR COMPUTER ON FIRE đ„.
Project-oriented workflow designs this away. đ
rm(list = ls())
?Option | Persists? |
---|---|
A. library(dplyr) | |
B. summary <- head | |
C. options(stringsAsFactors = FALSE) | |
D. Sys.setenv(LANGUAGE = âfrâ) | |
E. x <- 1:5 | |
F. attach(iris) |
03:00
rm(list = ls())
?Option | Persists? |
---|---|
A. library(dplyr) | |
B. summary <- head | |
C. options(stringsAsFactors = FALSE) | |
D. Sys.setenv(LANGUAGE = âfrâ) | |
E. x <- 1:5 | |
F. attach(iris) |
RStudio leaves notes to itself in foo.Rproj
Open Project = dedicated instance of RStudio
dedicated R process
file browser pointed at Project directory
working directory set to Project directory
OR
Tools -> Global Options
Session -> Restart R
Windows
Mac
Cmd + Shift + 0
Cmd + Shift + F10
usethis::create_project("~/i_am_new")
File -> New Project -> New Directory -> New Project
usethis::create_project("~/i_exist")
File -> New Project -> Existing Directory
Try option 2 now for wtf-explore-libraries.
05:00
A large-scale study on research code quality and execution.
Trisovic, A., Lau, M.K., Pasquier, T. et al.
Sci Data 9, 60 (2022).
relative to a stable base
use file system functions
not paste()
, strsplit()
, etc.
fs
= file path handling
Project directory
Automatically complete paths with Tab
.
I have nothing against absolute paths.
Some of my best friends are absolute paths!
But donât hard-wire them into your scripts.
Instead, form at runtime relative to a stable base
Use the here
package to build paths inside a project.
Leave working directory at top-level at all times, during development.
Absolute paths are formed at runtime.
Artwork by @allison_horst.
Works on my machine, works on yours!
Works even if working directory is in a sub-folder.
Works for RStudio Projects, Git repos, R packages, etc.
Works with knitr / rmarkdown.
The here
package is designed to work inside a project, where that could mean:
RStudio Project
Git repo
R package
Folder with a file named .here
here::here()
does not create directories; thatâs your job.
Absolute path.
Relative path to working directory, established by the RStudio Project.
Relative path within the RStudio Project directory.
Practice calling here::here()
in a project
to get a feel for it.
Read the README.md
to get started.
15:00
Are you sure it canât?
Review the Good Enough Practices paper for tips.
Create a symbolic link to access the data. (fs::link_create(), fs::link_path())
Put the data in an R package.
Use pins.
Explore other data warehousing options.
RStudio Community threads: