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


2.3 R code

There are two types of R code in R Markdown/knitr documents: R code chunks, and inline R code. The syntax for the latter is r R_CODE, and it can be embedded inline with other document elements. R code chunks look like plain code blocks, but have {r} after the three backticks and (optionally) chunk options inside {}, e.g.,

在R Markdown/knitr文档中有两种R代码类型:R代码块和行内R代码。后者的语法是 r R_CODE,它可以与其他文档元素行内嵌入。R代码块看起来像普通代码块,只是在三个反勾号后面有 {r}{},里面(可选的)是块选项,例如,

  1. ```{r chunk-label, echo = FALSE, fig.cap = 'A figure caption.'}
  2. 1 + 1
  3. rnorm(10) # 10 random numbers
  4. plot(dist ~ speed, cars) # a scatterplot

`` To learn more about **knitr** chunk options, see Xie ([2015](https://bookdown.org/yihui/bookdown/r-code.html#ref-xie2015)) or the web page [http://yihui.org/knitr/options](http://yihui.org/knitr/options). For books, additional R code can be executed before/after each chapter; seebefore_chapter_scriptandafter_chapter_scriptin Section [4.4](https://bookdown.org/yihui/bookdown/configuration.html#configuration).<br />要了解 **knitr** 快选项, 可以查看 Xie ([2015](https://bookdown.org/yihui/bookdown/r-code.html#ref-xie2015)) 或者网页 [http://yihui.org/knitr/options](http://yihui.org/knitr/options). 对于书籍可以自爱每个章节的前后执行额外的代码块; 详见第[4.4](https://bookdown.org/yihui/bookdown/configuration.html#configuration)节的before_chapter_scriptandafter_chapter_script` .

References

Xie, Yihui. 2015. Dynamic Documents with R and Knitr. 2nd ed. Boca Raton, Florida: Chapman; Hall/CRC. http://yihui.org/knitr/.