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


The knitr package was designed based on the idea of “Literate Programming” (Knuth 1984), which allows you to intermingle program code with text in a source document. When knitr compiles a document, the program code (in code chunks) will be extracted and executed, and the program output will be displayed together with the original text in the output document. We have introduced the basic syntax in Section 2.3.

knitr 包是基于“Literate Programming”(Knuth 1984)的思想设计的,它允许您将程序代码与源文档中的文本混合在一起。 knitr 编译文档时,会提取并执行程序代码(在代码块中),程序输出将与输出文档中的原始文本一起显示。我们已经在 2.3节介绍了基本语法。

R Markdown is not the only source format that knitr supports. The basic idea can be applied to other computing and authoring languages. For example, knitr also supports the combination of R and LaTeX (*.Rnw documents), and R + HTML (*.Rhtml), etc. You can use other computing languages with knitr as well, such as C++, Python, SQL, and so on. Below is a simple example and you can see http://rmarkdown.rstudio.com/authoring_knitr_engines.html for more.

R Markdown 不是 knitr 支持的唯一源格式。基本思想可以被应用于其他计算和创作语言。例如,knitr 还支持 R 和 LaTeX 的组合( *.Rnw 文档),以及 R+HTML( *.Rhtml )等。您也可以使用 knitr 使用其他计算语言,例如 C++, Python、SQL 等。下面是一个简单的示例,您可以查看 http://rmarkdown.rstudio.com/authoring_knitr_engines.html 了解更多信息。

  1. ```{python}
  2. x = 'Hello, Python World!'
  3. print(x.split(' '))
  1. > Python users may be familiar with IPython or Jupyter Notebooks ([https://jupyter.org](https://jupyter.org/)). In fact, R Markdown can also be used as notebooks, and has some additional benefits; see this blog post for more information: [https://blog.rstudio.org/2016/10/05/r-notebooks/](https://blog.rstudio.org/2016/10/05/r-notebooks/).
  2. Python 用户可能熟悉 IPython Jupyter Notebooks (https://jupyter.org)。事实上,R Markdown 也可以作为笔记本使用,并且还有一些额外的好处;有关更多信息,请参阅此博客文章:https://blog.rstudio.org/2016/10/05/r-notebooks/。
  3. > If you want to show a literal chunk in your document, you can add an inline expression that generates an empty string (``r ''``) before the chunk header, and wrap the code chunk in four backticks,[13](https://bookdown.org/yihui/bookdown/knitr.html#fn13) e.g.,
  4. 如果你想在你的文档中显示一个文字块,你可以添加一个内联表达式,在块头之前生成一个空字符串 (``r ''``) ,并将代码块用四个反引号包裹起来,[13](https://bookdown.org/yihui/bookdown/knitr.html#fn13) 例如,
  1. `r ''````{r}
  2. # a literal code chunk
  3. ```
  1. > After the document is compiled, the inline expression will disappear and you will see:
  2. 文档编译完成后,内联表达式就会消失,你会看到:
  1. # a literal code chunk

```

Normally you do not need to call knitr functions directly when compiling a document, since rmarkdown will call knitr. If you do want to compile a source document without further converting it to other formats, you may use the knitr::knit() function.

通常编译文档时不需要直接调用 knitr 函数,因为 rmarkdown 会调用 knitr。如果您确实想编译源文档而不进一步将其转换为其他格式,则可以使用 knitr::knit() 函数。

References

Knuth, Donald E. 1984. “Literate Programming.” The Computer Journal 27 (2): 97–111.


  1. Follow the indenting rule if the literal code chunk is to be displayed in other environments such as a list: https://pandoc.org/MANUAL.html#block-content-in-list-items↩︎