Search code examples
rbookdown

Bookdown: create file according to source Rmd filename


For each Rmd file I want to create file (directory) that is named according to Rmd file.

Example _bookdown.yml:

rmd_files: [
  "index.Rmd",
  "intro.Rmd",
  "analysis.Rmd"]

For each Rmd (probably using before_chapter_script) I want to create output directory:

./index/
./intro/
./analysis/

Question: How to extract Rmd filename using bookdown?


Solution

  • Not sure if I interpret your qn correctly, here is how you can create those output directories by including the following code in index.Rmd or _main.Rmd file:

    ---
    title: "Untitled"
    author: "Anon"
    date: "`r Sys.Date()`"
    site: bookdown::bookdown_site
    output: bookdown::gitbook
    ---
    
    ```
    cfg <- yaml::yaml.load_file("_bookdown.yml")
    invisible(lapply(gsub(".Rmd", "", cfg$rmd_files), dir.create))
    ```