尝试将 updateTabsetPanel 与 R Shiny 中的传单标记单击集成? [英] Trying to integrate updateTabsetPanel with leaflet marker click in R Shiny?

查看:26
本文介绍了尝试将 updateTabsetPanel 与 R Shiny 中的传单标记单击集成?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I was wondering if anyone knew a solution to an issue I'm having in Rshiny. In short, I'm looking to have leaflet-based map markers update the user's current tab. Ideally, my group want users to use the map to navigate between statistics exercises, housed within the tabs (exercises not present in this example).

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

The idea is that certain building markers are relevant to certain tabs, and the user looks at the map, sees markers, clicks to find out more (with popup), and the tab below changes automatically to match the exercise with the marker.

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

I've been trying to implement this so that the mouse click on the map marker registers as input for the 'updateTabsetPanel' command, but seem to have hit a brick wall. I have also tried implementing hyperlink/URLs within the popups to redirect the user to the correct tab, but have had no luck there either.

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

I had read an another example on here that was previously looking to do the same thing, however the person seeking help only provided excerpts from their code, and while I have tried to follow the answers they got, I can't seem to get it to work, making me think that there is perhaps another small issue elsewhere in my code.

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

I have provided a more in depth working example below, perhaps someone would be kind enough to have a look and suggest a fix, or tell me if what I am asking of Shiny/R is much too complex, and that my time is better spent elsewhere.

谢谢!

  **
  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",

对于服务器部分,我已修改您的代码以更新链接单击时的 tabset 面板.我为链接添加了点击事件,并为点击添加了观察事件.

For the server part I have modified your code to update tabset panel on the link click. I have added an click event for the link and added an observe event for the click.

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 事件.您可以在下面看到相同的内容:

Another way to do it would be use input$MAPID_marker_click event. You can see the same below:

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")

  })

})

希望对你有帮助!

这篇关于尝试将 updateTabsetPanel 与 R Shiny 中的传单标记单击集成?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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