在 R Shiny 应用程序中的服务器功能中更改 easyButton 状态

问题描述 投票:0回答:1

有谁知道如何更改 R Shiny 应用程序的服务器功能内的 easyButton 状态?

leaflet() %>%
    setView(lng = -70.4803, lat = 40.94404, zoom = 6) %>%
    addProviderTiles('Esri.WorldImagery') %>%
    addEasyButton(
    easyButton(id = 'edit-btn', states = list(
      easyButtonState( 
        stateName = 'add-toolbar',
        icon = icon('toggle-off'),
        title = 'Edit',
        onClick = JS("
          function(btn, map) {
            Shiny.onInputChange('edit_btn', 'TRUE');
            btn.state('remove-toolbar');
          }"
        )
      ),
      easyButtonState(
        stateName = 'remove-toolbar',
        icon = icon('toggle-on'),
        title = 'Editing',
        onClick = JS("
          function(btn, map) {
            Shiny.onInputChange('edit_btn', 'FALSE');
            btn.state('add-toolbar');
          }"
        )
      ))
    )
)

然后我尝试在单击另一个操作按钮时触发的服务器函数中更改此 easyButton 的状态:

runjs("edit-btn.state('add-toolbar');")

但这似乎不起作用。

r shiny r-leaflet
1个回答
1
投票

您可以使用shinyjs

click()
函数来做到这一点。这是一个可重现的示例:

library(shiny)
library(shinyjs)
ui <- fluidPage(
  useShinyjs(),
  actionButton("toggle_button","My Button"),
  actionButton("disable_button","Toggle State"),
  leafletOutput("my_map")
)

server <- function(input, output, session) {
  
  observeEvent(input$toggle_button,{
    shinyjs::click("edit-btn")
  })
  
  observeEvent(input$disable_button,({
    shinyjs::toggleState("edit-btn")
  }))
  
  output$my_map <- renderLeaflet({
    leaflet() %>%
      setView(lng = -70.4803, lat = 40.94404, zoom = 6) %>%
      addProviderTiles('Esri.WorldImagery') %>%
      addEasyButton(
        easyButton(id = 'edit-btn', 
                   states = list(
                     easyButtonState( 
                       stateName = 'add-toolbar',
                       icon = icon('toggle-off'),
                       title = 'Edit',
                       onClick = JS("
          function(btn, map) {
            Shiny.onInputChange('edit_btn', 'TRUE');
            btn.state('remove-toolbar');
          }"
                       )
                     ),
                     easyButtonState(
                       stateName = 'remove-toolbar',
                       icon = icon('toggle-on'),
                       title = 'Editing',
                       onClick = JS("
          function(btn, map) {
            Shiny.onInputChange('edit_btn', 'FALSE');
            btn.state('add-toolbar');
          }"
                       )
                     ))
        )
      )
  })
}

shinyApp(ui, server)
© www.soinside.com 2019 - 2024. All rights reserved.