gpt4 book ai didi

在 Shiny 启动时跨选项卡呈现传单标记

转载 作者:行者123 更新时间:2023-12-01 23:38:05 26 4
gpt4 key购买 nike

我正在尝试使用 tabsetPanel() 在 R Shiny 应用程序中创建两个传单 map ,这些传单 map 放置在不同的选项卡面板中。功能。我想要一个小部件(例如 sliderInput() )来控制两个 map 上标记的大小。我可以创建一个执行此操作的应用程序,但问题是功能最初并未呈现在启动时第二个面板上的 map 上。只有在选择面板后,功能才会在第二个面板上呈现,然后使用 slider 更改输入。我希望两张 map 在启动时都显示它们的功能,而不必使用 slider 初始化第二张 map 。

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,并将相应的输入放在你的观察者中,这样当你改变标签时观察者也会失效。下面给出了工作示例,希望对您有所帮助!

enter image description here

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)

关于在 Shiny 启动时跨选项卡呈现传单标记,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49503672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com