Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommend a particular folder structure, rather than putting everything in the project root #29

Open
fkohrt opened this issue Dec 11, 2024 · 0 comments
Assignees
Labels
ready Ready to be worked on

Comments

@fkohrt
Copy link
Member

fkohrt commented Dec 11, 2024

This way, we can also demonstrate how to deal with relative vs. absolute paths and recommend a project-based workflow.

Of course, this is complicated by the fact that notebooks generally assume the working directory to be in their storage location, rather than the root folder of the project.

Solutions

There are three levels on which this can be solved: For an individual code line, for all code chunks of a rmarkdown/Quarto document, or for all rmarkdown/Quarto documents in a project:

Individual code line

source

# rmarkdown
xfun::from_root("data/raw.csv", root = rprojroot::find_rstudio_root_file())

# Quarto
xfun::from_root("data/raw.csv", root = Sys.getenv("QUARTO_PROJECT_DIR"))

All code chunks

source

```{r}
#| cache: false
#| include: false

knitr::opts_knit$set(root.dir = rprojroot::find_rstudio_root_file())
```

All documents

For rmarkdown, set knit_root_dir when rendering:

rmarkdown::render(..., knit_root_dir = rprojroot::find_rstudio_root_file())

For Quarto, set execute-dir within _quarto.yml (source):

project:
  execute-dir: project

Notes

rprojroot vs. here

The package rprojroot is prefered for non-interactive use over here (source).

As rprojroot, here returns absolute paths which are user- and OS-specific, which does not play nice if caches or objects that contain these paths are shared with others.

RStudio settings

In RStudio, one can also set Tools > Global Options > R Markdown > Evaluate chunks in directory: Project

Of course, all code then relies on this setting, making it rather non-portable.

Providing opts_knit in Quarto header

In Quarto, one can also set the following in the header:

knitr:
  opts_knit:
    root.dir: "/path/to/working/directory"

This is, however, of little use, as it is not possible to calculate root.dir dynamically in this case.

@fkohrt fkohrt added the ready Ready to be worked on label Feb 11, 2025
@fkohrt fkohrt self-assigned this Feb 11, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready Ready to be worked on
Projects
None yet
Development

No branches or pull requests

1 participant