作者:Yihui Xie
译者:郑宝童
日期:2021.06.17


5.4 RStudio IDE.

We recommend that you upgrade your RStudio IDE if your version is lower than 1.0.0. As mentioned in Section 1.3, all R Markdown files must be encoded in UTF-8. This is important especially when your files contain multibyte characters. To save a file with the UTF-8 encoding, you can use the menu File -> Save with Encoding, and choose UTF-8.

如果您的版本低于 1.0.0,我们建议您升级 RStudio IDE。如第 1.3节所述,所有 R Markdown 文件都必须以 UTF-8 编码。这一点很重要,尤其是当您的文件包含多字节字符时。要使用 UTF-8 编码保存文件,您可以使用菜单 File -> Save with Encoding ,然后选择 UTF-8

When you click the Knit button to compile an R Markdown document in the RStudio IDE, the default function called by RStudio is rmarkdown::render(), which is not what we want for books. To call the function bookdown::render_book() instead, you can set the site field to be bookdown::bookdown_site in the YAML metadata of the R Markdown document index.Rmd, e.g.,

当您在 RStudio IDE 中单击 Knit 按钮编译 R Markdown 文档时,RStudio 调用的默认函数是 rmarkdown::render() ,这不是我们想要的。要调用函数 bookdown::render_book()的话 ,您可以在 R Markdown 文档 index.Rmd 的 YAML 元数据中将 site 字段设置为 bookdown::bookdown_site ,例如,

  1. ---
  2. title: "A Nice Book"
  3. site: bookdown::bookdown_site
  4. output:
  5. bookdown::gitbook: default
  6. ---

When you have set site: bookdown::bookdown_site in index.Rmd, RStudio will be able to discover the directory as a book source directory,11 and you will see a button Build Book in the Build pane. You can click the button to build the whole book in different formats, and if you click the Knit button on the toolbar, RStudio will automatically preview the current chapter, and you do not need to use preview_chapter() explicitly.

当你在 index.Rmd 中设置了 site: bookdown::bookdown_site 后,RStudio 将能够发现该目录作为书籍源目录,11 你将在 Build 窗格中看到一个Build Book按钮 .您可以单击该按钮以不同格式构建整本书,如果您单击工具栏上的 Knit 按钮,RStudio 将自动预览当前章节,您无需显式使用 preview_chapter()

The bookdown package comes with a few addins for RStudio. If you are not familiar with RStudio addins, you may check out the documentation at http://rstudio.github.io/rstudioaddins/. After you have installed the bookdown package and use RStudio v0.99.878 or later, you will see a dropdown menu on the toolbar named “Addins” and menu items like “Preview Book” and “Input LaTeX Math” after you open the menu.

bookdown 包带有一些 RStudio 插件。如果您不熟悉 RStudio 插件,您可以查看 http://rstudio.github.io/rstudioaddins/ 上的文档。安装 bookdown 包并使用 RStudio v0.99.878 或更高版本后,打开菜单后,您将在工具栏上看到一个名为“Addins”的下拉菜单以及“Preview Book”和“Input LaTeX Math”等菜单项。

The addin “Preview Book” calls bookdown::serve_book() to compile and serve the book. It will block your current R session, i.e., when serve_book() is running, you will not be able to do anything in the R console anymore. To avoid blocking the R session, you can daemonize the server using bookdown::serve_book(daemon = TRUE). Note that this addin must be used when the current document opened in RStudio is under the root directory of your book, otherwise serve_book() may not be able to find the book source.

插件“Preview Book”调用 bookdown::serve_book() 来编译和serve这本书。它会屏蔽您当前的 R 会话,即,当 serve_book() 正在运行时,您将无法再在 R 控制台中执行任何操作。为避免阻塞 R 会话,您可以使用 bookdown::serve_book(daemon = TRUE) 来daemonize 服务器。请注意,当在 RStudio 中打开的当前文档位于您的书的根目录下时,必须使用此插件,否则 serve_book() 可能无法找到书源。

The addin “Input LaTeX Math” is essentially a small Shiny application that provides a text box to help you type LaTeX math expressions (Figure 5.1). As you type, you will see the preview of the math expression and its LaTeX source code. This will make it much less error-prone to type math expressions — when you type a long LaTeX math expression without preview, it is easy to make mistakes such as X_ij when you meant X_{ij}, or omitting a closing bracket. If you have selected a LaTeX math expression in the RStudio editor before clicking the addin, the expression will be automatically loaded and rendered in the text box. This addin was built on top of the MathQuill library (http://mathquill.com). It is not meant to provide full support to all LaTeX commands for math expressions, but should help you type some common math expressions.

插件“Input LaTeX Math”本质上是一个小的 Shiny 应用程序,它提供了一个文本框来帮助您输入 LaTeX 数学表达式(图 5.1)。键入时,您将看到数学表达式的预览及其 LaTeX 源代码。这将使得输入数学表达式更不容易出错——当你输入一个长长的 LaTeX 数学表达式而没有预览时,很容易犯错误,比如 X_ij当你的意思是 X_{ij} ,或者漏掉一个结束括号。如果您在单击插件之前在 RStudio 编辑器中选择了 LaTeX 数学表达式,则表达式将自动加载并呈现在文本框中。这个插件建立在 MathQuill 库 (http://mathquill.com) 之上。它并不意味着为数学表达式的所有 LaTeX 命令提供完全支持,而是帮助您键入一些常见的数学表达式。

5.4 RStudio IDE. - 图1

FIGURE 5.1: The RStudio addin to help input LaTeX math. There are also other R packages that provide addins to help you author books. The citr package (Aust 2019) provides an addin named “Insert citations”, which makes it easy to insert citations into R Markdown documents. It scans your bibliography databases, and shows all citation items in a drop-down menu, so you can choose from the list without remembering which citation key corresponds to which citation item (Figure 5.2).

图 5.1:帮助输入 LaTeX 数学函数的 RStudio 插件。
还有其他 R 包提供插件来帮助您创作书籍。 citr 包(Aust 2019)提供了一个名为“Insert citations”的插件,可以很容易地将引文插入到 R Markdown 文档中。它会扫描您的书目数据库,并在下拉菜单中显示所有引文项目,因此您可以从列表中进行选择,而无需记住哪个引文关键字对应于哪个引文项目(图 5.2)。

5.4 RStudio IDE. - 图2
FIGURE 5.2: The RStudio addin to help insert citations.

References

Aust, Frederik. 2019. Citr: RStudio Add-in to Insert Markdown Citations. https://github.com/crsh/citr.


  1. This directory has to be an RStudio project.↩︎