Amin Shn
Amin Shn

Reputation: 598

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?

Upvotes: 1

Views: 904

Answers (1)

HubertL
HubertL

Reputation: 19544

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

Upvotes: 1

Related Questions