Aniket Schneider
Aniket Schneider

Reputation: 944

Programmatically generating formatted text in R markdown

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?

Upvotes: 4

Views: 875

Answers (1)

romants
romants

Reputation: 3648

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 |

Upvotes: 2

Related Questions