R语言 模式对话框仅在单击actionButton一轮后出现

o3imoua4  于 2023-04-03  发布在  其他
关注(0)|答案(1)|浏览(92)

我希望这个应用程序在点击actionButton时显示一个标题为“所有位置”的模式对话框,在点击Map标记时显示Map位置的ID。但是,与Map标记关联的对话框只会在点击actionButton一轮后显示。我也不希望模式对话框在开始时显示(我探索其他选项时发生过这种情况)。我错过了什么?

library(shiny)
library(leaflet)

# Define UI for application that draws a histogram
data <- PlantGrowth
data$Lat <- runif(nrow(data), 40, 41)
data$Lon <- runif(nrow(data), -1, 3)
data <- rbind(data[1,], data[11,])

ui <- bootstrapPage(
  leafletOutput("map", height="100vh"),
  absolutePanel(style="padding-left: 30px; padding-right: 30px; padding-top: 10px; padding-bottom: 10px",
                top = 10, left = 10, width = 300, height = "auto",
                actionButton("button", "Show all data")
  )
)

server <- function(input, output) {
  
  Title <- reactiveVal(NULL)
  
  observeEvent(input$button, {
    Title("All locations")
  })
  
  observeEvent(input$map_marker_click, {
    Title(input$map_marker_click$id)
          }) 
  
    observeEvent({input$map_marker_click 
                  input$button}, {
      showModal(
        modalDialog(
          title = Title() 
        )
      )
    })
  
  output$map<-
    renderLeaflet({
      plot.map <-
        leaflet(
          data = data, options = leafletOptions(zoomControl = F)
        ) %>% 
        addTiles() %>% 
        addCircleMarkers(
          lat = ~ Lat, lng = ~ Lon,
          weight = 1,
          layerId = ~ group,
          fillOpacity = 0.8,
          color = "black",
          opacity = 0.7,
          options = markerOptions(riseOnHover = TRUE))
      return(plot.map)
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
46scxncf

46scxncf1#

这是可行的:

observeEvent(list(input$map_marker_click, input$button), {
    showModal(
      modalDialog(
        title = Title() 
      )
    )
  }, ignoreInit = TRUE)

相关问题