When using R markdown if one wants to add text using code there are some simple ways to do it.
This is also true for tables, using the kable
command is very easy.
However, imagine you want to programmatically insert headers or lists to your report.
```{r, results='asis'}
headers=list("We","are","your","friends")
for (i in list_a){
#add i as header
}
```
and you want this to be the same as writing in your Rmd file:
#We
#are
#your
#friends
Another example would be to automatically create headers instead of lists:
```{r, results='asis'}
list_a=list("We","are","your","friends")
for (i in list_a){
#print i to a rmd list
}
```
as before this should have the same result as writing:
*We
*are
*your
*friends
This is not only a formatting problem as the table of context for Rmd files is created dynamically according to these headers.
Use the pander package, which transform R objects into Pandoc's markdown:
The first example used a helper function to create the headers, while the second demo simply called the generic S3 method, that can robustly transform a variety of R objects into markdown.
For people who want plots below each header, this is how I do it.
In the
plot.Rmd
put your plot code in it, e.g.You need to construct your wanted markdown in R and use that together with the argument
results = 'asis'
in your chunk options. Hence, something like the following will do what you want:The
for
-loop here will create the outputwhich is used directly as input in the .md document.