简体   繁体   中英

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. Items D, E, and F are filled with either 0 or 1.

Is the reactive subsetdata not showing D, E, and F as 0s and 1s?

Edit I want to add the below code works outside of the server function using the 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.

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)})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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