有条件地将CircleMarkers添加到传单中

问题描述 投票:0回答:2

我有一堆基于用户输入的反应式过滤器。仅当过滤器不返回 NULL 值时,我才想在传单地图上添加CircleMarkers。如何有条件地将CircleMarkers添加到Shiny中的传单地图中?现在看来,它只绘制第二个过滤器的结果,而不是两个过滤器的结果,即使用户输入的两个过滤器都不为 NULL。我的猜测是第二个 addCircleMarkers 函数会覆盖第一个函数,而不是向地图添加更多圆圈。下面是我的服务器代码:

服务器.R

server <- function(input, output) {


relig_pal <- colorFactor("magma", unique(all_cleaned$religion))
denom_pal <- colorFactor("viridis", unique(all_cleaned$denom))
  
output$mymap <- renderLeaflet({
  input$years_map
  input$map_button
  isolate({
    map <- leaflet() %>% addTiles() 
    if(!is.null(geography_1())) {
    marker_1 <- addCircleMarkers(map = map, data = geography_1(),
                     radius = ~ifelse(is.na(denom), log(religion_population), 
                                      log(denom_pop)),
                     color = ~ifelse(is.na(denom), relig_pal(religion),
                                     denom_pal(denom)),
                     label = ~ifelse(is.na(denom), 
                                            paste("Religion:",religion,
                                                  "Population:",religion_population),
                                            paste("Denomination:", denom,               
                                                  "Population:", denom_pop)) 
    )
    }
    if(!is.null(geography_2())) {
    marker_1 %>% addCircleMarkers(data = geography_2(),
                     radius = ~ifelse(is.na(denom), log(religion_population), 
                                      log(denom_pop)),
                   color = ~ifelse(is.na(denom), relig_pal(religion),
                                   denom_pal(denom)),
                   label = ~ifelse(is.na(denom), 
                                          paste("Religion:", religion,
                                                "Population:", religion_population),             
                                          paste("Denomination:", denom,              
                                                "Population:", denom_pop))
    )
    }
  })
})

year <- reactive({
  req(input$years_map)
  all_cleaned %>% filter(year == input$years_map)
})

religion_1 <- reactive({
  req(input$religion_1_map)
  if(input$religion_1_map == "All") {
    year()
  }
  else if(input$religion_1_map == "None") {
    return()
  }
  else {
    year() %>% filter(religion == input$religion_1_map)
  }
})

denom_1 <- reactive({
  req(input$denom_1_map)
  if(input$denom_1_map == "All") {
    religion_1()
  }
  else if(input$denom_1_map == "None") {
    religion_1() %>% filter(is.na(denom))
  }
  else {
    religion_1() %>% filter(denom == input$denom_1_map)
  }
})

geography_1 <- reactive({
  req(input$geography_1_map)
  if(input$geography_1_map == "All") {
    denom_1()
  }
  else if(input$geography_1_map == "None") {
    return()
  }
  else {
    denom_1() %>% filter(country_name == input$geography_1_map)
  }
})


religion_2 <- reactive({
  req(input$religion_2_map)
  if(input$religion_2_map == "All") {
    year()
  }
  else if(input$religion_2_map == "None") {
    return()
  }
  else {
    year() %>% filter(religion == input$religion_2_map)
  }
})

denom_2 <- reactive({
  req(input$denom_2_map)
  if(input$denom_2_map == "All") {
    religion_2()
  }
  else if(input$denom_2_map == "None") {
    religion_2() %>% filter(is.na(denom))
  }
  else {
    religion_2() %>% filter(denom == input$denom_2_map)
  }
})

geography_2 <- reactive({
  req(input$geography_2_map)
  if(input$geography_2_map == "All") {
    denom_2()
  }
  else if(input$geography_2_map == "None") {
    return()
  }
  else {
    denom_2() %>% filter(country_name == input$geography_2_map)
  }
})

}

错误信息:

Error:no applicable method for 'filter_' applied to an object of class "NULL"
r shiny r-leaflet
2个回答
1
投票

在地图上绘制标记后,您需要将它们保存到变量中(例如,查看我们如何解决此问题此处

对于第二个问题(你真的应该单独发布它们),尝试粘贴而不是 cat,例如

paste(statement1, statement2, sep = "\n")


0
投票

更新:

我已经成功地使用@Roman Luštrik的建议解决了这个问题,即将第一个圆形标记存储为变量,并使用占位符绘制不透明度为0的点,而不是在绘图上不应该出现任何内容时处理NULL值,我不太明白。

© www.soinside.com 2019 - 2024. All rights reserved.