在闪亮的应用程序中使用 observeEvent 显示情节

Showing plot using observeEvent in Shiny app

我在我的 shiny 应用程序中使用传单,我希望该应用程序在用户点击地图后打开一个 window 包括一个绘图。我写了下面的代码:

library(shiny)
library(leaflet)
library(plotly)
library(dplyr)

ui <- fluidPage(
  
  leafletOutput("mymap"),


)

server <- function(input, output, session) {
  
  points <- eventReactive(input$recalc, {
    cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
  }, ignoreNULL = FALSE)
  
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles(providers$Stamen.TonerLite,
                       options = providerTileOptions(noWrap = TRUE)
      ) %>%
      addMarkers(data = points())
  })
  
  
  
  observeEvent(input$mymap_marker_click,{
    
    df <- data.frame(x = 1:10, y = 1:10)
    plt <- ggplot(df, aes(x,y)) + geom_line()
    pltly <- ggplotly(plt)
    
    
    showModal(modalDialog(
      title = "Important message", size = "l",
      pltly ))
  })
  
}

shinyApp(ui, server)

这段代码可以完成工作,但它显示的情节非常压缩!如果你稍微向左或向右拖动 window 的边界,那么它将被修复,但我希望它能够工作而不需要这样做来修复情节!有谁知道更好的方法吗?

您可以使用renderPlotlyplotlyOutput来制作和显示情节。

  output$my_plotly <- renderPlotly({
      df <- data.frame(x = 1:10, y = 1:10)
      plt <- ggplot(df, aes(x,y)) + geom_line()
      ggplotly(plt)
    })

  observeEvent(input$mymap_marker_click,{
    showModal(modalDialog(plotlyOutput("my_plotly")))
    })