Unique place to render both R markdown files and book

Hi community,

I am facing the current situation:
I built a book thanks to bookdown package, where each .Rmd file is a chapter of the book.
I would like to keep the ability to render each .Rmd file in the Viewer Pane as it can be done with a classical .Rmd file with its yaml part at the beginning. The bookdown structure imposes to get rid of this yaml and hence, I did not find a simple way to render my .Rmd file.

Currently I am living with 2 versions of my project:

  • one that contains the .Rmd files without their yaml
  • one that contains the .Rmd files with their yaml

Of course, I know this is bad practice and I would like to have one source of truth.

Any idea?


Is previewing a chapter what you are looking for ?

Or possibly different : Having a book with the chapter AND some chapters as independant file ? Is that so ?

Maybe structuring your book using child document could work:

You would store each chapter on its own document, and then in your book only insert their content using child document.

You can pass almost every information using rmarkdown::render(), also using a common output_yaml.
but using child document would allow you probably to keep only those version with the YAML, and when inserted as child document, their YAML would be ignore (if I am right).

Would that help ?

Hello @cderv and thanks for answering.

I can live with bookdown::preview_chapter() or rmarkdown::render().

However it is not exactly what I am expecting. I may not have been clear enough so let me rephrase:
I have a classical book file tree, composed of _bookdown.yml, _output.yml, index.Rmd, and a few chapters.Rmd.
What I would like to do is to mimic the knit button behaviour, I mean rendering the chapters.Rmd in the Viewer pane (not in web browser) with a dedicated .yml different from the one used in index.Rmd.

Please note that I tried rmarkdown::render(input="Chapter1.Rmd", output_yaml="header.yml" ), where header.ymlis similar to a classical frontmatter yaml. It returned an error mentioning that arguments passed as title was not found. So I guess the header.yml is not understood as a yaml file, but I don't know why.

My motivation to do this is that I would like to be able to display the rendered content of my Chapters.Rmd in the Viewer pane and being able to run the chunk of codes without having to continuoulsy switch from the html file to the RStudio console and vice versa.

Thanks for your help!

output_yaml is for providing the content that you put usually under the output key in a YAML front matter. For other parameter (Pandoc variables), there is no specific mechanism in R Markdown for that. You would need to use a YAML passed to Pandoc command line using --metadata-file pandoc args (in pandoc_args from within R)

Did you look at this ? Using child document could help maybe.

I am not sure I am following. If you knit a single chapter (Chapter.Rmd here) using the Knit button, doesn't it open in the Viewer pane ?
It will use the bookdown layout though using the bookdown format because you are in a bookdown project.

Chapters don't have title or author for example. so you don't have per file specific metadata to set usually. If you want that, it is using Rmd in two special project (single file and book). Child document could be working for that.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.