Thereβs a famous blog post about workflows in R1 about a talk Jenny Bryan gave that included this slide:
If the first line of your R script is
I will come into your office and SET YOUR COMPUTER ON FIRE π₯.
If the first line of your R script is
I will come into your office and SET YOUR COMPUTER ON FIRE π₯.
setwd()
setwd()
changes the working directory, leading to potential issues in collaboration and reproducibility
my-project/
ββ my-project.Rproj
ββ README
ββ data/
β βββ raw/
β βββ processed/
ββ R/
ββ results/
β βββ tables/
β βββ figures/
β βββ output/
ββ docs/
An .Rproj
file is mostly just a placeholder. It remembers various options, and makes it easy to open a new RStudio session that starts up in the correct working directory. You never need to edit it directly.
A README file can just be a text file that includes notes for yourself or future users.
I like to have a folder for raw data β which I never touch β and a folder(s) for datasets that I create along the way.
{here}
Package{here}
package simplifies file path management within R projects.{here}
Packagesetwd()
and paste()
functions{here}
in your project{here}
package: install.packages("here")
.{here}
package in your R script: library(here)
.here()
function to reference files within your project directory.I actually like to use here::here()
rather than loading the package with library(here)
every time.
{here}
in your projectConstruct file paths with reference to the top directory holding your .Rproj
file.
here::here("data", "raw", "data.csv")
for me, here, becomes "/Users/l.smith/Documents/Presentations/reproducible-epi-SER/data/raw/data.csv"
But if I send you my code to run, it will become whatever file path you need it to be, as long as youβre running it within the R Project.
rm(list = ls())
removes objects from your environment, but it doesnβt unload the packages
You can easily create errors of the type Error in... could not find function...
because your code runs just fine until you restart R and realize you never included the right library()
calls
Restart your R session early and often as you write code!
{here}
package helps with reliable and portable file path management{here}
package documentation: