首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在leafletProxy()中绘制整个数据帧的数据?

如何在leafletProxy()中绘制整个数据帧的数据?
EN

Stack Overflow用户
提问于 2018-12-11 21:32:53
回答 1查看 298关注 0票数 1

我正在使用RStudio中的R并创建了一个闪亮的仪表板。在仪表板中,我使用leaflet()函数来绘制地图。现在我正试着在地图上画上标记物,标明芝加哥的犯罪情况。我正在使用selectInput()和sliderInput()函数来选择不同的犯罪类型、地点和年份。如果我在输入字段中选择了一些东西,那么这些标记就会显示在芝加哥地图上,而且效果很好。但我希望当我启动这个闪亮的应用程序时,所有的标记都会显示出来,而不会基于selectInput()进行过滤。这是我的ui.R代码:

代码语言:javascript
复制
  tabItem(tabName = "map",
    div(class="outer",
    tags$head(
    tags$style(type = "text/css", "#map {height: calc(100vh - 80px) !important;}"))),

    leafletOutput("map", width = "100%", height = "100%"),

    absolutePanel(id = "mapControls", fixed = TRUE, draggable = TRUE, top = 150, left = "auto", right = 15, bottom = "auto", width = 200, height = "auto",

    selectInput("mapCrimeType", label= "Select Crime Type", choices = unique(cc$Primary.Type), multiple = TRUE),

    selectInput("mapLocation", label= "Select Location", choices = unique(cc$Location.Description), multiple = TRUE),

    sliderInput("mapYear", label = "Select Year", min = 2001, max = 2016, step = 1, sep = '', value = c(2001,2016))

          )
  ),

这是我的服务器.R代码:

代码语言:javascript
复制
server <- function(input, output) {

  ### Draw Map ###
  output$map = renderLeaflet({
      leaflet() %>% 
      addProviderTiles(providers$Esri.WorldStreetMap) %>% 
      setView(lng = -87.6105, lat = 41.8947, zoom=11)
  }) 

  reactMap = reactive({
    cc %>% 
    filter(Primary.Type %in% input$mapCrimeType &
             Location.Description %in% input$mapLocation &
             Year %in% cbind(input$mapYear[1],input$mapYear[2]))
  })

  observe({
    proxy = leafletProxy("map", data = reactMap()) %>%
      clearMarkers() %>%
      clearMarkerClusters() %>%
      addCircleMarkers(clusterOptions = markerClusterOptions(),
                       lng =~ Longitude, lat =~ Latitude, radius = 5, group = 'Cluster', 
                       popup =~ paste('<b><font color="Black">', 'Crime Information', 
                      '</font></b><br/>', 'Crime Type:', Primary.Type,'<br/>',
                      'Date:', Date,'<br/>', #'Time:', Time,'<br/>',
                      'Location:', Location.Description,'<br/>', 'Block:', Block, '<br/>', 'Arrest:', Arrest, '<br/>'))
  })

我想我必须在server.R文件内的反应函数中更改一些东西。我希望有人能帮助我。

更新:使用此数据集:https://www.kaggle.com/currie32/crimes-in-chicago

EN

回答 1

Stack Overflow用户

发布于 2018-12-12 06:04:15

不需要测试,但这应该可以工作。只需将反应改为:

代码语言:javascript
复制
reactMap <- reactive({

  out_cc <- cc %>% 
    dplyr::filter(., Year %in% cbind(input$mapYear[1],input$mapYear[2]))

  if (!is.null(input$mapCrimeType)) {
    out_cc <- out_cc %>% 
      dplyr::filter(., Primary.Type %in% input$mapCrimeType)
  }

  if (!is.null(input$mapLocation)) {
    out_cc <- out_cc %>% 
      dplyr::filter(., Location.Description %in% input$mapLocation)
  }

  out_cc

})

当在没有“NULL”值的情况下调用时,在带有multiple = TRUE的情况下,选择器在开始处或当您取消选择所有内容时都具有NULL值。因此,当给定变量的相应输入为NULL时,您只需跳过对该变量的过滤。

顺便说一句:我可能错了,但我认为你可能需要把你的“年份”过滤器改成这样:

代码语言:javascript
复制
dplyr::filter(., Year >= input$mapYear[1] & 
                  Year <= input$mapYear[2])

,否则,您将只获得所选年份的最小值和最大值,而不会获得中间年份的值。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53725226

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档