Using `ggplotly` and `DT` from a `for` loop in Rmarkdown

后端 未结 2 1683
轮回少年
轮回少年 2021-01-15 00:14

Is that possible to use ggplotly() or datatable() in RMarkdown from inside a for loop or function? Example:

---
title:          


        
2条回答
  •  春和景丽
    2021-01-15 01:02

    This is the solution from the post I added in my comment adapted to your case:

    ---
    title: "Using `ggplotly` and `DT` from a `for` loop in Rmarkdown"
    output: html_document
    ---
    
    ```{r setup, include=FALSE}
    library(plotly); library(DT)
    ```
    
    ```{r, include=FALSE}
    # Init Step to make sure that the dependencies are loaded
    htmltools::tagList(datatable(cars))
    htmltools::tagList(ggplotly(ggplot()))
    ```
    
    ```{r, results='asis'}
    for( col in 1:ncol(cars)) {
      
      print(htmltools::tagList(datatable(cars)))
      
      g <- ggplot(cars) + geom_histogram(aes_string(x=names(cars)[col] ) )
    
      print(htmltools::tagList(ggplotly(g)))
    
    }
    ```
    

提交回复
热议问题