开发者

R Shiny: Why Doesn't My Leaflet Map Project Two Political Races?

StackOverflow! I am a hobbyist coder, longtime lurker, first-time poster, and am completely stuck on how to fix a bug in a Shiny dashboard I wrote in R. Using this tutorial as a starting point, I want to map the 2022 General Election results in the County I live in Ohio. I have collected the data, created new shapefiles to project the winning margin, and saved two .csv files to display the results at the overall and precinct levels.

Here is my server code:

library(DT)
library(leaflet)
library(leafpop)
library(sf)
library(shiny)
library(tidyr)

election.df <- read.csv("data/election.df.csv")
overall.df <- read.csv("data/overall.df.csv")

colnames(election.df)[colnames(election.df) == "precName"] <- "Precinct"

dem_df <- st_read("data/dem_df.shp")
rep_df <- st_read("data/rep_df.shp")

dem_df[dem_df$RACE == "223", "MARGIN"] <- (dem_df[dem_df$RACE == "223", "MARGIN"]/3)[,1]
rep_df[rep_df$RACE == "223", "MARGIN"] <- (rep_df[rep_df$RACE == "223", "MARGIN"]/3)[,1]

dem_min_max_values <- range(abs(dem_df$MARGIN), na.rm = TRUE)
rep_min_max_values <- range(abs(rep_df$MARGIN), na.rm = TRUE)

dem_palette <- colorNumeric(palette = "Blues", 
                            domain=c(dem_min_max_values[1], dem_min_max_values[[2]]))

rep_palette <- colorNumeric(palette = "Reds", 
                            domain=c(rep_min_max_values[1], rep_min_max_values[2]))

server <- function(input, output, session){ 
  overall <- reactive({
    overall.df[overall.df$raceNo %in% input$selectRace, c("Candidate", "Party", "Votes")]
  })
  
  output$overallDT <- renderDataTable(
    datatable(overall(),
              options = list(dom = 't'),
              rownames= FALSE) %>%
      formatCurrency("Votes", 
                     currency = "", 
                     interval = 3, 
                     mark = ",",
                     digits = 0)
    
  )
  
  precinct <- reactive({
    pivot_wider(data = election.df[election.df$raceNo %in% input$selectRace, c("precNo", "Precinct", "candName", "votes", "Turnout")], 
                names_from = candName, 
                values_from = votes,
                names_sort = TRUE, 
                names_glue = gsub("\\.", " ", "{candName}"))
  })
  
  output$precinctDT <- renderDataTable(
    datatable(precinct()[,-1])
  )
  
  dem_map <- reactive({
    merge(dem_df[dem_df$RACE %in% input$selectRace, c("PRECINC", "MARGIN", "geometry")], precinct(), by.x = "PRECINC", by.y = "precNo")
  })
  
  rep_map <- reactive({
    merge(rep_df[rep_df$RACE %in% input$selectRace, c("PRECINC", "MARGIN", "geometry")], precinct(), by.x = "PRECINC", by.y = "precNo")
  })

  output$myMap <- renderLeaflet({
    leaflet() %>%
      addProviderTiles("CartoDB.Positron")  %>%
      
      addPolygons(
        data = dem_map(),
        fillColor = ~dem_palette(dem_map()$MARGIN),
        popup = popupTable(x = dem_map(),
                           zcol = c(-1:-2, -ncol(dem_map())),
                           row.numbers = F, 
                           feature.id = F),
        stroke = TRUE,
        smoothFactor = 0.2,
        fillOpacity = 2,
        color = "#666",
        weight = 1) %>%
      
      addPolygons(
        data = rep_map(),
        fillColor = ~rep_palet开发者_如何转开发te(rep_map()$MARGIN),
        popup = popupTable(x = rep_map(),
                           zcol = c(-1:-2, -ncol(rep_map())),
                           row.numbers = F, 
                           feature.id = F),
        stroke = TRUE,
        smoothFactor = 0.2,
        fillOpacity = 2,
        color = "#666",
        weight = 1)
  })
  
}

Here is the Git Hub repository if you would like to download it yourself.

Now, my issue is that I am unable to project the leaflet map for the Twinsburg and Hudson mayoral races. I expect to see the mapped results, and when a precinct is clicked, a popup window come up that displays the name, the turnout percentage, and the number of votes each candidate received. However, in R Studio I receive the message, "Error: subscript out of bounds". This only happens with the Twinsburg and Hudson mayoral elections and only after I had added the popupTable function to the popup option. Without that argument, the map displays correctly. The app has no problem working with the other election data.

Thanks in advance! I know I have to be missing something.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜