有条件地将CircleMarkers添加到传单

时间:2018-06-22 10:22:13

标签: r shiny leaflet

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

server.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"

在此先感谢您的帮助!

2 个答案:

答案 0 :(得分:1)

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

对于第二个问题(您应该将它们分别单独张贴),请尝试粘贴而不是cat,例如paste(statement1, statement2, sep = "\n")

答案 1 :(得分:0)

更新:

我已经成功使用@RomanLuštrik的建议解决了这个问题,该建议将第一个圆形标记存储为变量,并使用占位符绘制不透明度为0的点,而不是在不应出现任何值的情况下处理NULL值情节,我无法弄清楚。