在闪亮的启动上跨选项卡呈现传单标记 [英] render leaflet markers across tabs on shiny startup

查看:29
本文介绍了在闪亮的启动上跨选项卡呈现传单标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 tabsetPanel()函数在R Shiny应用程序中创建两个放置在不同选项卡面板中的传单地图.我想有一个小部件(例如 sliderInput())来控制两个地图上标记的大小.我可以创建一个执行此操作的应用程序,但问题是启动时功能最初并未显示在第二个面板上的地图上.仅在选择面板,然后使用滑块更改输入之后,才在第二个面板上渲染要素.我希望两个地图在启动时都显示其功能,而不必使用滑块初始化第二个地图.

I'm trying to create two leaflet maps in an R shiny app that are placed in different tabpanels using the tabsetPanel() function. I would like to have a widget (e.g., sliderInput()) that controls the size of markers on both maps. I can create an app that does this but the problem is that features are not initially rendered on the map that is on the second panel on startup. Features are rendered on the second panel only after selecting the panel, then changing the input with a slider. I would like both maps to show their features on startup without having to initialize the second with a slider.

library(shiny)
library(leaflet)

pts <- data.frame(
  x = rnorm(10, mean = -93.625), 
  y = rnorm(10, mean = 42.0285)
)


# Define UI 
ui <- fluidPage(

   sliderInput("radius",
           "Point radius:",
           min = 1,
           max = 50,
           value = 30),

    tabsetPanel(

      tabPanel('Map1',

      leafletOutput('map1')   

      ), 

      tabPanel('Map2', 

       leafletOutput('map2')   

      )
   )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$map1 <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
    })
  output$map2 <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
  })

  observe({

    tab1 <- leafletProxy('map1', data = pts) %>%
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = input$radius)

    tab2 <- leafletProxy('map2', data = pts) %>% 
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = input$radius)

  })

}

# Run the application 
shinyApp(ui = ui, server = server)

推荐答案

作为一个简单的解决方法,您可以给您的 tabsetPanel 一个ID,并将相应的输入放入观察者中,以便观察者更改选项卡时也无效.下面给出了工作示例,希望对您有所帮助!

As a simple fix, you could give your tabsetPanel an id, and put the corresponding input in your observer, so that the observer also invalidates when you change tabs. Working example is given below, I hope this helps!

library(shiny)
library(leaflet)

pts <- data.frame(
  x = rnorm(10, mean = -93.625), 
  y = rnorm(10, mean = 42.0285)
)


# Define UI 
ui <- fluidPage(
  sliderInput("radius",
              "Point radius:",
              min = 1,
              max = 50,
              value = 30),
  tabsetPanel(id='my_tabsetPanel',
    tabPanel('Map1',
             leafletOutput('map1')   
    ), 
    tabPanel('Map2', 
             leafletOutput('map2')   
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  output$map1 <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
  })
  output$map2 <- renderLeaflet({
    leaflet() %>% 
      addTiles() %>% 
      setView(-93.65, 42.0285, zoom = 6)
  })

  observe({

    input$my_tabsetPanel

    tab1 <- leafletProxy('map1', data = pts) %>%
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = input$radius)

    tab2 <- leafletProxy('map2', data = pts) %>% 
      clearMarkers() %>% 
      addCircleMarkers(lng = ~x, lat = ~y, radius = input$radius)

  })

}

# Run the application 
shinyApp(ui = ui, server = server)

这篇关于在闪亮的启动上跨选项卡呈现传单标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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