我怎样才能防止在一个闪亮的RMD重叠plotly图形?

svgewumm  于 2023-04-09  发布在  其他
关注(0)|答案(1)|浏览(146)

如果shinyr-markdown报表包含React性plotly图,如何防止长图(使用ggplotly生成)重叠?
在示例中,很明显,plotly-long块中生成的图与下面的块中生成的图重叠。

---
title: "A shiny Report"
runtime: shiny
output:
  bookdown::html_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(tidyverse)
library(plotly)

df <- data.frame(type =  c("potato", "carrot", "leek"),
                 count = c(1, 3, 2))
selectizeInput("SELECTION",
               "Choose:",
               selected = c("potato", "leek"),
               multiple = TRUE,
               choices = c("potato", "carrot", "leek"))
df_filt <- reactive({
  df %>%
    filter(type %in% input$SELECTION)
})
ggp <- reactive({
  df_filt() %>%
    ggplot(aes(y = count, fill = type, x = "a_bar")) +
    geom_bar(stat = "identity")
})
renderPlotly({
  ggplotly(ggp(), height = 1600)
})
renderPlotly({
  ggplotly(ggp())
})
2w2cym1i

2w2cym1i1#

renderPlotly替换为renderUI就可以做到这一点。

---
title: "A shiny Report"
runtime: shiny
output:
  bookdown::html_document2
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE)

library(tidyverse)
library(plotly)

df <- data.frame(type =  c("potato", "carrot", "leek"),
                 count = c(1, 3, 2))
selectizeInput("SELECTION",
               "Choose:",
               selected = c("potato", "leek"),
               multiple = TRUE,
               choices = c("potato", "carrot", "leek"))
df_filt <- reactive({
  df %>%
    filter(type %in% input$SELECTION)
})
ggp <- reactive({
  df_filt() %>%
    ggplot(aes(y = count, fill = type, x = "a_bar")) +
    geom_bar(stat = "identity")
})
renderUI({
  ggplotly(ggp(), height = 1600)
})
renderUI({
  ggplotly(ggp())
})

相关问题