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

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

问题描述

我正在尝试使用 tabsetPanel() 函数在 R 闪亮应用程序中创建两个传单地图,这些传单地图放置在不同的选项卡面板中.我想要一个控制两个地图上标记大小的小部件(例如,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天全站免登陆