Variable as title in rmarkdown

丶灬走出姿态 提交于 2019-12-23 02:34:37

问题


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

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