如何仅在单击提交按钮时更新 Shiny 应用程序?

How to update a Shiny application only when the submit button is clicked?

我想创建一个 that is updated only when the submit button is clicked. I looked a lot online to find an answer to my question an found this very useful webpage: (https://shiny.rstudio.com/articles/action-buttons.html)。我理解代码并且能够重现它,但是当我对自己的代码使用相同的逻辑时,更新按钮毫无用处并且不执行任何操作。

这是我的代码:

library(shiny)
library(leaflet)


ui <- fluidPage(

  checkboxGroupInput(
    inputId = "selected_dates",
    label = "Choose the year",
    choices = c("20","19","18","17","16","15"),
    selected = c("20","19","18","17","16","15")
  ),

  actionButton("go", "Update"),
  leafletOutput("map")

)

server <- function(input, output) {

  dates <- eventReactive(input$go, {
    input$years
  })

  output$map <- renderLeaflet({


    m <- addTiles(setView(leaflet(), lng = -73.731368, lat = 45.531368, zoom = 10))
    addCircles(m, data = vol_voiture[vol_voiture$Year %in% input$years,], lat = ~ LATITUDE, lng = ~ LONGITUDE, popup = ~ DATE, opacity = 0.5, weight = 0.1)

  })
}

shinyApp(ui, server)

澄清一下,vol_voiture 是我的 。我知道这可能只是我的一个简单错误或误解,但我已经为此工作了很多个小时,但似乎找不到解决方案。非常感谢您的帮助!

您没有在任何地方使用您的 eventReactive 功能。我会尝试用 dates()

替换 renderLeaflet 调用中的 input$years
library(shiny)
library(leaflet)


ui <- fluidPage(

  checkboxGroupInput(
    inputId = "selected_dates",
    label = "Choose the year",
    choices = c("20","19","18","17","16","15"),
    selected = c("20","19","18","17","16","15")
  ),

  actionButton("go", "Update"),
  leafletOutput("map")

)

server <- function(input, output) {

  dates <- eventReactive(input$go, {
    input$years
  })

  output$map <- renderLeaflet({

    m <- addTiles(setView(leaflet(), lng = -73.731368, lat = 45.531368, zoom = 10))
    addCircles(m, data = vol_voiture[vol_voiture$Year %in% dates(),], lat = ~ LATITUDE, lng = ~ LONGITUDE, popup = ~ DATE, opacity = 0.5, weight = 0.1)

  })
}

shinyApp(ui, server)

希望对您有所帮助!!