簡體   English   中英

在 Shiny 應用程序中使用 observeEvent 顯示 plot

[英]Showing plot using observeEvent in Shiny app

I'm using leaflet in my shiny app and I want the app to open a window including a plot once the user clicks on the map. 我寫了下面的代碼:

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)

這段代碼可以完成這項工作,但它顯示 plot 非常壓縮? 如果您將 window 的邊界稍微向左或向右拖動,那么它將被修復,但我希望它能夠工作而無需為修復情節而這樣做! 有誰知道更好的方法嗎?

您可以使用renderPlotlyplotlyOutput來制作和顯示 plot。

  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")))
    })

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM