问题
In short: how can I print a html-header inside a chunk in rmarkdown (use a variable to create a header inside a code-chunk)?
In long:
Let's say I have a number of variables in my dataset (names also stored in the vector 'Vars'), and each group of 3 variables belongs to the same subject.
I can make a loop to plot all the variables, but I want the subject in a title. Something like:
Subject 1
Plot Var1
Plot Var2
Plot Var3
Subject 2
Plot Var 4
Plot Var 5
...
Following pseudo-code hopefully explains what I want to do:
```{r}
for (i in Vars)
if(i%%3 == 1){
print(## Subject[ceiling(i/3)])}
plot(i)
```
回答1:
- Don't use print, it will add decorative stuff.
- Note the required double "\n"; check what happens with one only, so you are prepared when you forget it next time
```{r, results="asis", echo = FALSE}
i = 1
cat("## Subject", i, "\n")
plot(i)
i = 2
cat("\n\n## Subject", i, "\n")
plot(i)
```
来源:https://stackoverflow.com/questions/40892121/variable-as-title-in-rmarkdown