Programmatically generating formatted text in R markdown

久未见 提交于 2019-12-24 11:27:33

问题


I am fairly new to R markdown (and R itself). I am using RStudio to create an R Markdown file. I would like to create a report that has several different plots across different time windows, each with accompanying text. Something like this:

for (i in seq(0, max)) {
  # generate some text with markdown formatting including the value of i
}

I know that it is possible to embed R values inline in markdown text. Is there also a way to generate markdown text inline within R code?


回答1:


Probably Pander package is what you are looking for. It support markdown renderings for R objects.

Toy example of printing lines of data frame.

d> m <- mtcars[1:4, 1:6]
d> for (i in 1:4)
+     pander(m[i,], style="rmarkdown")



|     &nbsp;      |  mpg  |  cyl  |  disp  |  hp  |  drat  |  wt  |
|:---------------:|:-----:|:-----:|:------:|:----:|:------:|:----:|
|  **Mazda RX4**  |  21   |   6   |  160   | 110  |  3.9   | 2.62 |



|       &nbsp;        |  mpg  |  cyl  |  disp  |  hp  |  drat  |  wt   |
|:-------------------:|:-----:|:-----:|:------:|:----:|:------:|:-----:|
|  **Mazda RX4 Wag**  |  21   |   6   |  160   | 110  |  3.9   | 2.875 |



|      &nbsp;      |  mpg  |  cyl  |  disp  |  hp  |  drat  |  wt  |
|:----------------:|:-----:|:-----:|:------:|:----:|:------:|:----:|
|  **Datsun 710**  | 22.8  |   4   |  108   |  93  |  3.85  | 2.32 |



|        &nbsp;        |  mpg  |  cyl  |  disp  |  hp  |  drat  |  wt   |
|:--------------------:|:-----:|:-----:|:------:|:----:|:------:|:-----:|
|  **Hornet 4 Drive**  | 21.4  |   6   |  258   | 110  |  3.08  | 3.215 |


来源:https://stackoverflow.com/questions/24539251/programmatically-generating-formatted-text-in-r-markdown

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