我已经在ui.R和server.R文件中设置了此问题的示例。这是ui.R文件。有三个随机数据集。无论用户界面做什么,都会显示带有标记的人。另外两个数据集只有在被要求作为“圈子”时才能被绘制。使用UI打开和关闭数据和传单显示
library(shiny)
library(leaflet)
pageWithSidebar(
headerPanel("Main Title Here"),
sidebarPanel(
checkboxInput('Circles', 'Show Locations', TRUE)
),
mainPanel(
leafletOutput("map")
)
)
该映射由server.R生成,如下所示。这将生成随机数据和纬度和经度位置。
library(shiny)
library(leaflet)
library(ggplot2)
shinyServer(function(input, output) {
output$map <- renderLeaflet({
leaflet() %>%
addTiles(group = "OpenStreetMap") %>%
addMarkers(runif(20,-75,-74),
runif(20, 41, 42))
})
observe({
proxy <- leafletProxy("map")
if (input$Circles == TRUE) {
proxy %>% addCircles(runif(20,-75,-74),
runif(20, 41, 42),
group = "Circles",
color = "red") %>%
addCircles(runif(20,-75,-74),
runif(20, 41, 42),
group = "Circles",
color = "blue") %>%
addLegend(
position = "bottomright",
colors = c("red", "blue"),
labels = c("Group 1", "Group 2")
)
# cannot put group or label on legend.
} else {
proxy %>% hideGroup("Circles")
# %>% hideGroup("Group 2")
}
})
})
程序启动时显示圆圈(TRUE)。我在这里看到两个主要问题。首先,可以关闭这些圈子,但这是任何UI控件的结束。问题是他们不能再次打开。如果我尝试启动checkboxInput为FALSE,UI控件将不会打开它。我不知道这是否标准,或者如果我做错了什么。
第二个主要问题是我无法将图例编程为与圆圈一起关闭。使用hideGroup()函数关闭这些圆圈。这被拒绝在图例上使用。我已经接受了我尝试的命令并将其转换为注释行。继续并删除#号,以便您可以看到它创建的错误。我还没有找到替代方案。
'hideGroup'的对面是'showGroup'。一旦你隐藏了它们,如果你想看到它们,你需要再次'展示'它们。 – SymbolixAU
这些圆圈的原始创建是使用命令“addCircles()”。我会将它们替换为showGroup,还是以某种方式与addCircles结合? –
你可能需要'addCircles()'到原来的'leaflet()'调用。然后'代理'事件或者'show'或'hide',这取决于'input $ Circles'的条件 – SymbolixAU