5

I've got a large bookdown project and I want to check that a single chapter works. I've written it so that all of the packages I need are loaded in Index.Rmd

However, when i try to either render the book or preview a chapter, it fails as funtion %>% is not found, even though library(dplyr) is in Index.Rmd.

Is it really necessary to load the same packages at the start of each chapter in a bookdown project? or am I missing something?

r.bot
  • 5,309
  • 1
  • 34
  • 45

1 Answers1

2
Is it really necessary to load the same packages 
at the start of each chapter in a bookdown project? 

It depends on your knitr approach:

Merging all chapters into one Rmd file and knitting it is one way to render the book in bookdown. There is actually another way: you may knit each chapter in a separate R session, and bookdown will merge the Markdown output of all chapters to render the book. We call these two approaches “Merge and Knit” (M-K) and “Knit and Merge” (K-M), respectively. …

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); for K-M, all chapters are isolated from each other.

If you don't want to load the library you could also refer to the function with the :: notation, e.g. knitr::kable(head(iris, 20))

Added: For more info how to proceed have a look into the bookdown manual.

Community
  • 1
  • 1
petzi
  • 1,430
  • 1
  • 17
  • 32
  • Thanks. So it would appear that I am using Knit and Merge. How can I switch to merge and knit? – r.bot May 27 '18 at 18:53
  • 1
    The default approach is M-K. Use the "Build" Menu (right above pane) and then the "Build Book" menu. To switch to K-M, you either use the argument `new_session = TRUE` when calling `render_book()`, or set `new_session: yes` in the configuration file `_bookdown.yml`. All this you will find explained in the [bookdown manual](https://bookdown.org/yihui/bookdown/new-session.html). -- (I recommend that you stay with M-K until you feel more comfortable.) – petzi May 27 '18 at 19:17
  • You will find what you have to do in the bookdown manual: https://bookdown.org/yihui/bookdown/new-session.html – petzi May 27 '18 at 19:24