传单弹出窗口中的提交按钮不会触发闪亮的watchEvent [英] submit button in leaflet popup doesn't trigger observeEvent in shiny

查看:65
本文介绍了传单弹出窗口中的提交按钮不会触发闪亮的watchEvent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need a workaround for this problem. I have a submit button inside a popup. When I click on the marker, the input$map_marker_click$id now contains the id of the marker clicked.

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

There is a submit button in the popup. When the submit button is clicked I want to save the input$map_marker_click$id to a variable. Using oberserveEvent or eventReactive both are not working for me.

这是代码将其另存为app.R

Here is the code save it as 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)

推荐答案

问题可能是Shiny不知道您要添加按钮,因此没有绑定.

The issue might be that Shiny does not know you are adding buttons and so there are no bindings.

一个肮脏的把戏可能是在告诉Shiny已被单击的按钮上添加onclick函数:

A dirty trick could be to add an onclick function to the buttons that tells Shiny it has been clicked:

例如:

<button onclick='Shiny.onInputChange(\"button_click\",  Math.random())' id='selectlocation' type='button' class='btn btn-default action-button'>Select Location</button>

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

This JS function will send the random number to Shiny, and you can access it in server.R using input$button_click.

然后您可以在eventReactive

执行此操作的正确方法可能是使用Shiny.unbindAll()Shiny.bindAll() 请参阅底部的.但是我不确定如何在此处进行操作.

The proper way to do this is probably to use Shiny.unbindAll() and Shiny.bindAll() see here at the bottom, but I'm not sure how to do that here.

这篇关于传单弹出窗口中的提交按钮不会触发闪亮的watchEvent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆