Cannot get my regression analysis to run, my map is getting an error

The map is saying that addMarkers requires numeric longitude/latitude values, but my dataset has them in there.


title: "Visual Analysis of NYPD Arrests Data"
output:
flexdashboard::flex_dashboard:
theme: united
orientation: columns
vertical_layout: fill
runtime: shiny

@import url('https://fonts.googleapis.com/css2?family=Yusei+Magic&display=swap'); body { background-color: lightgray; color: darkgreen; text-align: left; } .navbar { position: fixed; display: flex; justify-content: center; width: 100%; } .shiny-input-container { color: purple; } .navbg { background-color: blue; color: white; }
library(flexdashboard)
library(shiny)
library(plotly)
library(ggplot2)
library(tidyverse)
library(leaflet)  
library(sf)

nypd_arrests <- st_read("https://raw.githubusercontent.com/kyleknox3/STA553/main/final/nypd_arrests.csv");
nypd_arrests <- na.omit(nypd_arrests)
nypd_arrests$arrest_boro <- factor(nypd_arrests$arrest_boro,
                                   levels = c("S", "K", "M", "Q", "B"),
                                   labels = c("Staten Island", "Brooklyn", "Manhattan", "Queens", "Bronx"))
nypd_arrests$perp_sex <- factor(nypd_arrests$perp_sex,
                                levels = c("M", "F"),
                                labels = c("Male", "Female"))

Column {.sidebar .navbg data-width=200}



selectInput("selected_race", "Select Race:", choices = unique(nypd_arrests$perp_race))
selectInput("selected_gender", "Select Gender:", choices = c("Male" = "Male", "Female" = "Female"), selected = "Male")
selectInput("selected_boro", "Select Borough:", choices = c("Bronx", "Staten Island", "Brooklyn", "Manhattan", "Queens"))
workDat <- reactive({
  data <- nypd_arrests
  
  if (input$selected_boro != "All") {
    data <- data[data$arrest_boro == input$selected_borough, ]
  }
  
  if (input$selected_gender != "All") {
    data <- data[data$perp_sex == input$selected_gender, ]
  }
  
  return(data)
})

Column {data-width=450 .tabset .tabset-pills}

Arrest Count by Race

output$racePlot <- renderPlot({
  filtered_data <- nypd_arrests %>%
    filter(perp_race == input$selected_race)
  ggplot(filtered_data, aes(x = arrest_boro, fill = arrest_boro)) +
    geom_bar() +
    labs(title = paste("Arrest Count in Each Borough for", input$selected_race),
         x = "Borough", y = "Count")
})

plotOutput("racePlot")

Arrest Count by Gender

output$genderPlot <- renderPlot({
  filtered_data <- nypd_arrests %>%
    filter(perp_sex == input$selected_gender)

  ggplot(filtered_data, aes(x = arrest_boro, fill = arrest_boro)) +
    geom_bar(stat = "count") +  
    labs(title = paste("Arrest Count in Each Borough for", input$selected_gender),
         x = "Borough", y = "Count")
})

plotOutput("genderPlot")

Regression Analysis


output$regressionPlot <- renderPlot({
  data = workDat()
  
  if (nrow(data) > 1) {  
    data$arrest_boro_numeric <- as.numeric(factor(data$arrest_boro))
    data$gender_numeric <- ifelse(data$perp_sex == "Male", 1, 0)   
    model <- lm(gender_numeric ~ arrest_boro_numeric, data = data)
    plot(data$arrest_boro_numeric, data$gender_numeric, main = "Linear Regression of Gender on Borough",
         xlab = "Borough (Numeric)", ylab = "Gender (Binary)", pch = 19, col = "blue")
    abline(model, lwd = 2, col = "red")
  } else {
    plot.new()
    text(0.5, 0.5, "Insufficient data for regression", cex = 1.5)
  }
})

plotOutput("regressionPlot")

Statistics Summary


Map Visualization

renderLeaflet({
  filtered_data <- nypd_arrests[nypd_arrests$arrest_boro == input$selected_boro, ]

  leaflet(filtered_data) %>%
    addTiles() %>%
    addMarkers(
      lng = ~longitude, 
      lat = ~latitude, 
      popup = ~paste("Arrest Date: ", arrest_date, "<br/>",
                     "Gender: ", ifelse(perp_sex == "m", "Male", "Female"), "<br/>",
                     "Race: ", perp_race),
      clusterOptions = markerClusterOptions()
    )
})


Column {data-width=340}

Arrests by Borough

renderPlot({
  filtered_data <- nypd_arrests[nypd_arrests$arrest_boro == input$selected_boro, ]
  ggplot(filtered_data, aes(x = perp_sex, fill = perp_race)) +
    geom_bar(position = "dodge") +
    labs(title = paste("Distribution of Arrests by Gender and Race in", input$selected_boro),
         x = "Gender", y = "Count")
})