简体   繁体   English

R - 使用 dplyr、DT 和 Shiny 在地图中创建数据表

[英]R - Using dplyr, DT, and Shiny to create data table in map

My code is我的代码是

server <- function(input, output) {
  subsetdata = reactive ({
  newdata = popdatamerged[popdatamerged$`A` %in% input$'B',]
  return((newdata))
})

output$map = renderLeaflet({ 
  leaflet() %>%
  addTiles()
})

observe({
  leafletProxy('map') %>%
  clearMarkerClusters %>%
  addMarkers(data= subsetdata(), lng = ~Long, lat = ~Lat, 
             clusterOptions = markerClusterOptions(zoomToBoundsOnClick = TRUE, spiderfyOnMaxZoom = FALSE))
})

output$mytable = DT::renderDataTable({
  subsetdata() %>%
    group_by_(~CityCountry) %>%
    summarize_("D" = sum(~D), "E" = sum(~E), "F" = sum(~F), na.rm=TRUE)
})

The error I get in output (which is a Map) is under the header 'Data Table'我在输出(这是一个地图)中得到的错误在标题“数据表”下

invalid 'type' (language) of argument

I am pretty sure it is coming from the dplyr's summarize part.我很确定它来自 dplyr 的总结部分。 Items D, E, and F are filled with either 0 or 1.项目 D、E 和 F 填充为 0 或 1。

Is the reactive subsetdata not showing D, E, and F as 0s and 1s?反应性子集数据是否未将 D、E 和 F 显示为 0 和 1?

Edit I want to add the below code works outside of the server function using the popdatamerged编辑我想使用 popdatamerged 添加以下代码在服务器功能之外工作

popdatamerged() %>%
    group_by_(~CityCountry) %>%
    summarize_("D" = sum(~D), "E" = sum(~E), "F" = sum(~F), na.rm=TRUE)

I was able to find a solution.我能够找到解决方案。 I believe the observe syntax fixed the issue with the sum in the summarize line.我相信观察语法解决了总结行中的总和问题。 Hopefully this can be helpful to others.希望这可以对其他人有所帮助。

The datatabledata below is properly integrated in the map.下面的数据表数据已正确集成到地图中。 The data table has a list of all unique CityCountry then sums up D, E, and F for each CityCountry.数据表包含所有唯一 CityCountry 的列表,然后汇总每个 CityCountry 的 D、E 和 F。

Thank you for viewing.感谢您的观看。

server <- function(input, output) {
  subsetdata = reactive ({
  newdata = popdatamerged[popdatamerged$`A` %in% input$'B',]
  return((newdata))
 })

output$map = renderLeaflet({ 
  leaflet() %>%
  addTiles()
})

observe({
  leafletProxy('map') %>%
  clearMarkerClusters %>%
  addMarkers(data= subsetdata(), lng = ~Long, lat = ~Lat, 
         clusterOptions = markerClusterOptions(zoomToBoundsOnClick = TRUE, 
         spiderfyOnMaxZoom = FALSE))
})

  observe({
    datatabledata = DT:: datatable ({
      subsetdata() %>%
        dplyr:: group_by_(~CityCountry) %>%
        dplyr:: summarize( "D" = sum(D), "E" = sum(E), "F" = sum(F))
})

output$mytable = DT:: renderDataTable(datatabledata)})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM