作者:Yihui Xie
译者:郑宝童
日期:2021.06.16
5.2 Preview a chapter
Building the whole book can be slow when the size of the book is big. Two things can affect the speed of building a book: the computation in R code chunks, and the conversion from Markdown to other formats via Pandoc. The former can be improved by enabling caching in knitr using the chunk option
cache = TRUE
, and there is not much you can do to make the latter faster. However, you can choose to render only one chapter at a time using the functionpreview_chapter()
in bookdown, and usually this will be much faster than rendering the whole book. Only the Rmd files passed topreview_chapter()
will be rendered.
当书的大小很大时,构建整本书可能会很慢。有两件事会影响构建一本书的速度:R 代码块中的计算,以及通过 Pandoc 从 Markdown 到其他格式的转换。前者可以通过使用块选项 cache = TRUE
在 knitr 中启用缓存来改进,应该没有其他方式比这个更快。但是,您可以选择使用 bookdown 中的函数 preview_chapter()
一次仅渲染一章,通常这比渲染整本书要快得多。只会呈现传递给 preview_chapter()
的 Rmd 文件。
Previewing the current chapter is helpful when you are only focusing on that chapter, since you can quickly see the actual output as you add more content or revise the chapter. Although the preview works for all output formats, we recommend that you preview the HTML output.
当您只关注当前章节时,预览当前章节会很有帮助,因为您可以在添加更多内容或修改章节时快速查看实际输出。尽管预览适用于所有输出格式,但我们建议您预览 HTML 输出。
One downside of previewing a chapter is that the cross-references to other chapters will not work, since bookdown knows nothing about other chapters in this case. That is a reasonably small price to pay for the gain in speed. Since previewing a chapter only renders the output for that specific chapter, you should not expect that the content of other chapters is correctly rendered as well. For example, when you navigate to a different chapter, you are actually viewing the old output of that chapter (which may not even exist).
预览章节的一个缺点是对其他章节的交叉引用将不起作用,因为在这种情况下 bookdown 对其他章节一无所知。这是为提高速度而付出的相当小的代价。由于预览章节仅呈现该特定章节的输出,因此您不应期望其他章节的内容也能正确呈现。例如,当您导航到不同的章节时,您实际上是在查看该章节的旧输出(甚至可能不存在)。