简体   繁体   中英

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. I have written the below code:

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)

This code kind of does the job but it shows the plot very compressed? and if you slightly drag the boundaries of the window to the left or right then it will be fixed but I want it to work without the need to do that for fixing the plot! Does anyone know a better way for doing it?

You can use renderPlotly and plotlyOutput to make and display the 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")))
    })

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM