在传单弹出窗口中提交按钮不会触发observeEvent闪亮

时间:2015-10-01 22:03:20

标签: r shiny leaflet

我需要解决此问题的方法。 我在弹出窗口中有一个提交按钮。当我点击标记时,输入$ map_marker_click $ id现在包含点击标记的id。

弹出窗口中有一个提交按钮。单击提交按钮时,我想将输入$ map_marker_click $ id保存到变量中。使用oberserveEvent或eventReactive都不适合我。

以下是将其另存为app.R的代码

library(shiny)
library(leaflet)

df <- data.frame("id" = c("1", "2"),
                 "lng" = c(-93.65, -93.655),
                 "lat" = c(42.0285, 42.03),
                 "Text" = c("Department of Statistics", "something else"))


ui <- fluidPage(
  leafletOutput("map"),
  textOutput("locationid1"),
  textOutput("locationid2")
)

server <- function(input, output, session) {

  output$map <- renderLeaflet({
    df %>% leaflet() %>%
      addProviderTiles("CartoDB.Positron") %>%
      setView(-93.65, 42.0285, zoom = 15) %>%
      addMarkers(layerId = ~id, popup = ~paste("<b>", Text, "</b></br>
                                               <button id='selectlocation' type='button' class='btn btn-default action-button'>Select Location</button>"))
  })


  id1 <- reactive({
     validate(
       need(!is.null(input$map_marker_click), "Please select a location from the map above")
     )
    input$map_marker_click$id
  })

  id2 <- eventReactive(input$selectlocation, {
    input$map_marker_click$id
  })


  output$locationid1 <- renderText({paste("Location Selected using marker click:", id1())})
  output$locationid2 <- renderText({paste("Location Selected using popup select button click:", id2())})

}

shinyApp(ui, server)

1 个答案:

答案 0 :(得分:1)

问题可能是Shiny不知道你正在添加按钮,因此没有绑定。

一个肮脏的技巧可能是向按钮添加<button onclick='Shiny.onInputChange(\"button_click\", Math.random())' id='selectlocation' type='button' class='btn btn-default action-button'>Select Location</button> 功能,告诉Shiny它已被点击:

例如:

server.R

此JS函数会将随机数发送给Shiny,您可以使用input$button_clickinput$button_click中访问它。

然后,您可以在eventReactive

中使用Shiny.unbindAll()

执行此操作的正确方法可能是在底部使用Shiny.bindAll()"\"Hallo" + (char)10 + "World\"" see here,但我不确定如何在此处执行此操作。

相关问题