How to speed up bookdown generation?

怎甘沉沦 提交于 2021-02-08 07:53:49

问题


I'm currently working on a book using bookdown. It uses some code snippets which take time to compile, execute and get output. I use the following commands to build HTML, PDF and EPUB files for the book:

Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::gitbook')"
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::pdf_book')"
Rscript -e "bookdown::render_book('index.Rmd', 'bookdown::epub_book')"

So, every time the book files are generated the actual computation takes place increasing the overall generation time ×3.

Is there any way to produce some intermediate file on the first run and than use it to build HTML, PDF and EPUB outputs?


回答1:


I managed to significantly reduce the regeneration time by adding the following snippet to my index.Rmd:

```{r include=FALSE}
knitr::opts_chunk$set(cache = TRUE)
```

Update:

As Yuriy Barvinchenko and Yihui Xie both suggested, it's better to cache only time-consuming code chunks:

```{go time-sleep-demo cache = TRUE}
package main

import (
    "fmt"
    "time"
)

func main() {
    fmt.Print("Hello ")
    time.Sleep(10 * time.Second)
    fmt.Println("world!")
}
```


来源:https://stackoverflow.com/questions/56541371/how-to-speed-up-bookdown-generation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!