尝试将updateTabsetPanel与单张标记集成在R Shiny中?

时间:2017-03-07 15:31:31

标签: r leaflet shiny

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

这个想法是某些建筑标记与某些标签相关,并且用户查看地图,查看标记,点击以查找更多(使用弹出窗口),并且下方的标签会自动更改以使练习与标记相匹配。

我一直在尝试实现这一点,以便鼠标点击地图标记注册为“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.")
                     ))))

 ))

 **

1 个答案:

答案 0 :(得分:0)

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

对于服务器部分,我已修改您的代码以更新链接点击上的tabset面板。 我为链接添加了一个click事件,并为点击添加了一个observe事件。

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

  })

})

另一种方法是使用输入$ 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")

  })

})

希望它有所帮助!