r - 使用复选框 Leaflet/R 添加标记
问题描述
如果用户勾选复选框,我正在尝试在我的交互式传单地图上添加标记。但是,我无法让复选框外观在 renderLeaflet 函数中工作,但我可以在 renderPlot 函数中工作。
我的代码有效,如下所示:
ui <- fluidPage(
sidebarLayout(
sidebarPanel(
checkboxInput('check1','Show Counties', value = TRUE)),
mainPanel(
# I'm aware that this should be changed to leaftletOutput, when used
# with the in the second code snippet below
plotOutput("mymap")
),
position = 'right'
)
)
server <- function(input, output) {
output$mymap <- renderPlot({
if (input$check1){
hist(rnorm(10))
} else {
hist(rnorm(100))
}
})
}
shinyApp(ui = ui, server = server)
相同的逻辑在 Leaflet 中不起作用,我非常感谢有关如何修改以下代码的输入。我知道您无权访问USstates或coor这两个变量,但我希望可以指出错误,可能是语法错误。输出函数如下所示;
output$mymap <- renderLeaflet({
if (input$check1 == TRUE){
leaflet(USstates) %>%
addTiles() %>%
addPolygons(color = 'navy',
opacity = 1.0,
weight = 1) %>%
addMarkers(lng = coor[,1],lat = coor[,2])
} else {
leaflet(USstates) %>%
addTiles() %>%
addPolygons(color = 'navy',
opacity = 1.0,
weight = 1)
}
})
解决方案
I came up with a solution, of cause right after I created this post. The solution was to wrap the leaflet code in a reactive function and call that one in the renderLeaflet function. The solution is as follows:
server <- function(input, output) {
display <- reactive({
if (input$check1){
leaflet(USstates) %>%
addTiles() %>%
addPolygons(color = 'navy',
opacity = 1.0,
weight = 1) %>%
addMarkers(lng = coor[,1],lat = coor[,2])
} else {
leaflet(USstates) %>%
addTiles() %>%
addPolygons(color = 'navy',
opacity = 1.0,
weight = 1)
}
})
output$mymap <- renderLeaflet({
display()
})
}
推荐阅读
- angular - 无法通过单击在 Angular 中选择单选按钮
- antlr4 - ANTLR 匹配错误的标记
- vba - 在电子邮件正文中使用 Excel VBA IF THEN 语句
- c - 在c中使用递归函数的寻路程序
- css - 使用 SCSS 和 nuxt 进行主题化
- elasticsearch - 您可以将现有的 Elasticsearch 索引与 Elastic App 搜索挂钩吗
- r - 重新编码多个变量的 R 解决方案
- angular - 订阅另一个组件中的服务变量?
- c# - Json 反序列化动态在 .NET Core 3.1 中不起作用
- javascript - 来自 web api 的值没有被转换