简体   繁体   中英

R knitr Markdown: How do I output formatting from chunks?

I'm writing a Markdown file that uses a for loop to produce a series of charts based on the contents of the data passed to them. I would like for the file to include some text before each chart that includes data based on the place in the for loop, so it can't be created outside the chunk. But when I try to use

print(paste("##This chart relates to", var[i]))

the output I get looks like

[1] "##This chart relates to Foobar"

What I want, is for the output text to be formatted as a H2 header. I tried adding the option results="asis" to the chunk, but that had no discernible effect. What can I do?

Here's a sample of the relevant code:

```{r groupA, results="asis", warning=FALSE}
group<-"A"
tags<-unique(unlist(data$taglist[data$Group==group]))
for (i in 1:length(tags)) {
  print(paste("###Commitments by ", group, " tagged as ",tags[i]))
  commitcloud(data,group,tags[i])
```

Use pander::pandoc.header() :

pandoc.header(paste("Commitments by", group, "tagged as",tags[i]), level = 3)

Where level indicates the level for the heading.

Per @richard-telford - include a cat("\\n") if you have problems with previous contents. Tables seem to be especially troublesome.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM