r - 尝试将 updateTabsetPanel 与 R Shiny 中的传单标记单击集成?

标签 r leaflet shiny

我想知道是否有人知道我在 Rshiny 遇到的问题的解决方案。简而言之,我希望使用基于传单的 map 标记来更新用户的当前选项卡。理想情况下,我的团队希望用户使用 map 在选项卡内的统计练习之间导航(本示例中未出现练习)。

这个想法是,某些建筑标记与某些选项卡相关,用户查看 map ,看到标记,单击以了解更多信息(通过弹出窗口),并且下面的选项卡会自动更改以将练习与标记相匹配。

我一直在尝试实现这一点,以便将鼠标单击 map 标记注册为“updateTabsetPanel”命令的输入,但似乎遇到了障碍。我还尝试在弹出窗口中实现超链接/URL 以将用户重定向到正确的选项卡,但也没有运气。

我在这里读过另一个例子,之前想做同样的事情,但是寻求帮助的人只提供了他们的代码摘录,虽然我试图遵循他们得到的答案,但我似乎无法让它工作,让我觉得我的代码中的其他地方可能还存在另一个小问题。

我在下面提供了一个更深入的工作示例,也许有人会好心地看一下并提出修复建议,或者告诉我我对 Shiny/R 的要求是否太复杂,并且我的时间最好花在其他地方。

谢谢!

  **
  library(leaflet)
  library(shiny)
  library(dplyr)

  shinyServer(function(input, output, session) {

  #I've tried hyperlinking to the tab with an action link/hyperlink 
  ##that would   appear in the popup
  Popcontent1 <- paste(sep = "<br/>",
                   actionLink("?url=inTabset/Home", "Learn about A"),
                   "This would be the First Marker",
                   "and would update the tabs to a specific tab below")
  Popcontent2 <- paste(sep = "<br/>",
                   actionLink("link_to_tabpanel_B", "Learn about B"),
                   "This one would also update to another tab")

  output$London <-  renderLeaflet({
  London <- leaflet(options = leafletOptions(zoomControl = FALSE, 
                 minZoom = 16,     maxZoom = 16)) %>%
  setView(lng = 0.12783, lat =51.50741, zoom = 16)%>%
    addTiles(options = providerTileOptions(opacity = 0.45)) %>%
     addCircleMarkers(lng=0.12783, lat=51.50741, radius = 13,
              opacity = 0.5, color = "#008B45", 
                 popup=Popcontent1)%>% #MarkerHome
     addCircleMarkers(lng=0.12774, lat=51.50700, radius = 13, 
                 color =  "#48D1CC",popup=Popcontent2) #Marker2
   PopTEST <- addCircleMarkers(London, lng=0.12800, lat=51.50650,
                 color = "#9400D3", popup=Popcontent1) #TestMarker
  })  

  ## Attempt at making the markers in the above map interactive. 
  ## Ideally, clicking on the markers above would change the tabs, 
  ## meaning users  click on certain building marker and get relevant tab

  observe({
  event <- input$London_PopTEST_click
  updateTabsetPanel(session, "inTabset", selected = event$A)
  }) 

  observeEvent(input$switchtab, {
  event <- input$London_PopTEST_click
  updateTabsetPanel(session, "inTabset", selected = event$A)
   })
  })


#########UI

shinyUI(fluidPage(
titlePanel("This is the Map"),
leafletOutput("London", width = "100%", height = 600),
br(),
tabsetPanel(id = "inTabset", 
          tabPanel(title = "Home", id = "Home", 
                   h4("This is the Home Tab"),
                   br(),
                   mainPanel(),
                   fluidRow(
                     column(12,
                            p("This would be the introductory tab.")
                     ))),
          ######################################## Tab A 
          tabPanel("Tab A", id = "A",
                   h4("This tab would be the next step"),
                   br(),
                   fluidRow(
                     column(12,
                            p("This tab would be brought up by the 
                              marker/popup click in the map above.")
                     ))))

 ))

 **

最佳答案

在 ui 部分,您需要更改 tabPanel("Tab A", id = "A",tabPanel("Tab A", value= "A",

对于服务器部分,我修改了您的代码以更新链接单击上的选项卡集面板。 我为链接添加了一个点击事件,并为点击添加了一个观察事件。

shinyServer(function(input, output, session) {

  #I've tried hyperlinking to the tab with an action link/hyperlink 
  ##that would   appear in the popup
  Popcontent1 <- paste(sep = "<br/>",
                       ##Here I have added and event which needs to be updated on clicking the link called "link_click"
                       actionLink("?url=inTabset/Home", "Learn about A", onclick = 'Shiny.onInputChange(\"link_click\",  Math.random())'),
                       "This would be the First Marker",
                       "and would update the tabs to a specific tab below")
  Popcontent2 <- paste(sep = "<br/>",
                       actionLink("link_to_tabpanel_B", "Learn about B"),
                       "This one would also update to another tab")

  output$London <-  renderLeaflet({
    London <- leaflet(options = leafletOptions(zoomControl = FALSE, 
                                               minZoom = 16,     maxZoom = 16)) %>%
      setView(lng = 0.12783, lat =51.50741, zoom = 16)%>%
      addTiles(options = providerTileOptions(opacity = 0.45)) %>%
      addCircleMarkers(lng=0.12783, lat=51.50741, radius = 13,
                       opacity = 0.5, color = "#008B45", 
                       popup=Popcontent1)%>% #MarkerHome
      addCircleMarkers(lng=0.12774, lat=51.50700, radius = 13, 
                       color =  "#48D1CC",popup=Popcontent2) #Marker2
    PopTEST <- addCircleMarkers(London, lng=0.12800, lat=51.50650,
                                color = "#9400D3", popup=Popcontent1) #TestMarker
  })  


  #Here I have the observEvent for link_click which updates the tab
  observeEvent(input$link_click,{
    updateTabsetPanel(session, "inTabset", "A")

  })

})

另一种方法是使用 input$MAPID_marker_click 事件。您可以在下面看到相同的内容:

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

  #I've tried hyperlinking to the tab with an action link/hyperlink 
  ##that would   appear in the popup
  Popcontent1 <- paste(sep = "<br/>",
                       actionLink("?url=inTabset/Home", "Learn about A"),
                       "This would be the First Marker",
                       "and would update the tabs to a specific tab below")
  Popcontent2 <- paste(sep = "<br/>",
                       actionLink("link_to_tabpanel_B", "Learn about B"),
                       "This one would also update to another tab")

  output$London <-  renderLeaflet({
    London <- leaflet(options = leafletOptions(zoomControl = FALSE, 
                                               minZoom = 16,     maxZoom = 16)) %>%
      setView(lng = 0.12783, lat =51.50741, zoom = 16)%>%
      addTiles(options = providerTileOptions(opacity = 0.45)) %>%
      addCircleMarkers(lng=0.12783, lat=51.50741, radius = 13,
                       opacity = 0.5, color = "#008B45", 
                       popup=Popcontent1)%>% #MarkerHome
      addCircleMarkers(lng=0.12774, lat=51.50700, radius = 13, 
                       color =  "#48D1CC",popup=Popcontent2) #Marker2
    PopTEST <- addCircleMarkers(London, lng=0.12800, lat=51.50650,
                                color = "#9400D3", popup=Popcontent1) #TestMarker
  })  


#This is the marker click event
  observeEvent(input$London_marker_click,{

    updateTabsetPanel(session, "inTabset", "A")

  })

})

希望对你有帮助!

关于r - 尝试将 updateTabsetPanel 与 R Shiny 中的传单标记单击集成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42652197/

相关文章:

r - 带有 Rcpp 代码的包不会在 Windows 上构建

javascript - 如何在传单 map 中设置标记图标?

r - 如何在安装了防火墙的情况下连接到shinyapps?

r - 在 Shiny 的应用程序启动时显示通知

r - Shiny - 自定义警告/错误消息?

r - 使用 R 中的 tidymodels 获取 catboost 模型的摘要形状图

r - 了解如何读取设备列表

r - 对由两个变量分组的变量进行计数

javascript - 使用传单进行最大纬度 map 平移

javascript - 如何使用 Leaflet Routing Machine 和 Mapzen 获取行人路线?