R: how do you embed plots into a tab in RMarkdown in a procedural fashion?

前端 未结 2 1647
误落风尘
误落风尘 2020-12-21 07:46

I can embed plots using just RMarkdown\'s {.tabset}

#### Heading  {.tabset}
##### Subheading 1
```{r, echo=F}
df[[1]]    
```

相关标签:
2条回答
  • 2020-12-21 08:09

    Adding both plot.new(), dev.off() inside the for loop solves the problem of adding all the figures in the last tab. See the complete solution here.

    0 讨论(0)
  • 2020-12-21 08:18

    I played around a little and found a solution. You have to use print within the asis code chunk...

    ```{r}
    library(ggplot2)
    gg0 <- list()
    gg0[[1]] <- ggplot(mtcars, aes(mpg, hp)) + geom_point()
    gg0[[2]] <- ggplot(mtcars, aes(mpg, disp)) + geom_point()
    gg0[[3]] <- ggplot(mtcars, aes(mpg, drat)) + geom_point()
    
    headings <- c('hp','disp','drat')
    ```
    
    #### Heading  {.tabset}
    ```{r, results='asis', echo = FALSE}
    for (i in 1:length(gg0)) {
      cat("##### ",headings[i],"\n")
      print(gg0[[i]])
      cat('\n\n')
    }
    ```
    

    As an explanation, the cat command together with results='asis' produces the markdown code for a lower level headline and prints the ggplot graph afterwards. Since we used `{.tabset} in the parent headline, it creates the plots in separate tabs.

    0 讨论(0)
提交回复
热议问题