multiple (R) plotly figures generated in a rmarkdown (knitr chunk) document

前端 未结 3 701
情深已故
情深已故 2021-01-01 00:14

I try to create multiple plotly figures in a Rmarkdown document using loop or lapply.

The R script:

require(plotly)
data(iris)
b <- lapply(setdif         


        
3条回答
  •  悲哀的现实
    2021-01-01 00:59

    Instead of print(b), put b in htmltools::tagList(), e.g.

    ```{r}
    library(plotly)
    
    b <- lapply(
      setdiff(names(iris),
              c("Sepal.Length","Species")),
      function(x) {
        plot_ly(iris, 
                x = iris[["Sepal.Length"]],
                y = iris[[x]], 
                mode = "markers")
      }
    )
    
    htmltools::tagList(b)
    ```
    

    Note: Before Plotly v4 it was necessary to convert the Plotly objects to htmlwidgets using Plotly's as.widget() function. As of Plotly v4 they are htmlwiget objects by default.

    For people who are interested in the technical background, you may see this blog post of mine. In short, only top-level expressions get printed.

提交回复
热议问题