Warm tip: This article is reproduced from serverfault.com, please click

Showing plot using observeEvent in Shiny app

发布于 2020-12-01 22:13:05

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?

Questioner
Amin Shn
Viewed
0
HubertL 2020-12-02 10:17:18

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