knitr::opts_chunk$set( collapse = TRUE, comment = "#>", error = !identical(Sys.getenv("IN_PKGDOWN"), "true") ) project_path <- system.file("demo-project", package = "here")
The here package enables easy file referencing by using the top-level directory of a file project to easily build file paths.
This article demonstrates the case where the working directory is set to a subdirectory of the project root, for instance when rendering an R Markdown document that lives in a subdirectory.
vignette("here") for a more general introduction.
For demonstration, this article uses a data analysis project that lives in
`r project_path` on my machine.
This is the project root.
The path will most likely be different on your machine, the here package helps deal with this situation.
The project has the following structure:
report.Rmd is rendered, the working directory is internally set to
<project root>/analysis by rmarkdown:
knitr::opts_knit$set(root.dir = file.path(project_path, "analysis"))
penguins.csv still lives in the
The report requires the
penguins.csv file to work.
report.Rmd, you would have to ensure the path to
penguins.csv is relative to the
analysis/ directory - i.e.,
../data/penguins.csv. The chunks would knit properly, but could not be run in the console since the working directory in the console isn't
The here package circumvents this issue by always referring to the project root:
All files accessed by
report.Rmd should be referred to using
library(here) here("data", "penguins.csv") here("data/penguins.csv")
This ensures that
penguins.csv can be read both when the report is knit and when the code is run interactively in the console.
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.