bookdown question regarding libraries, setup chunks, and K-M vs M-K rendering

I would like to have several R code chunks in several chapters of a bookdown book.

However, I am struggling with where to put setup chunks and call libraries.

It sounds like if I do the default (K-M), then I can put one setup chunk in index.Rmd with all libraries for all future chapters. Is this correct?

If I want to use M-K rendering, as some have advised, do I then need a setup chunk in each chapter with the libraries used in each chapter?

There is not a lot of detail in section 1.4 of the bookdown book to guide me.

I am getting a recurring error in the Build Site step on Netlify

Build site
[error]Process completed with exit code 1.

Run Rscript -e 'bookdown::render_book("index.Rmd", quiet = TRUE)'

Quitting from lines 19-22 (rmrwr.Rmd)

[error]Error in library(tidyverse) : there is no package called 'tidyverse'

when I have called this library in the setup chunk.

Clearly I am doing something wrong.

Ideas?

Actually the default strategy is M-K, which means you can configure one set up chunk in index.Rmd and all other following chapters will share that setting. The bookdown book says:

The most significant difference is that M-K runs all code chunks in all chapters in the same R session, whereas K-M uses separate R sessions for individual chapters. For M-K, the state of the R session from previous chapters is carried over to later chapters (e.g., objects created in previous chapters are available to later chapters, unless you deliberately deleted them);

To switch to the K-M approach, create a file named _bookdown.yml under your root directory and add the field new_session: yes. If you do this, then each chapter will need its individual setup chunk in their Rmd file respectively. However, another option is use the before_chapter_script field in _bookdown.yml. For example before_chapter_script: foo.R will let bookdown execute code inside foo.R each time before it starts to knit individual Rmd files, so that you can still put your setup chunk only in one place. If you want to load tidyverse without having to call it in each chapter, just use library(tidyverse) in foo.R.

That said, it seems tidyverse isn't installed according the error message. Could you run install.packages("tidyverse") and try again? Or did you look up how to publish book through netlify?

2 Likes

Thanks, enixam. It appears that it was mostly a problem with GitHub Actions, which I was using to push to Netlify. It turns out that this approach is much slower (4-7 min) and buggier (it could not find tidyverse) than the drag and drop strategy for netlify espoused by Alison Hill here: Sharing on Short Notice: How to Get Your Materials Online With R Markdown - RStudio

Now working fine with same Rmd files, using drag and drop strategy to Netify.

Thanks for your explication of M-K vs K-M. I think I may end up going with K-M, rather than trying to keep track of all libraries across multiple chapters and updating them in index.Rmd.

The takeaway here may be to always follow Alison Hill's advice on Rmd.

2 Likes

If your question's been answered (even by you!), would you mind choosing a solution? It helps other people see which questions still need help, or find solutions if they have similar problems. Here’s how to do it:

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