Hi,
I am working on creating an interactive map to visualize geographical drafting trends in the NHL.
I am working off my R knowledge as well as the example of a Shiny interactive map detailed here: https://www.shafquatarefeen.com/r-shiny-permits/
My Leaflet code works to create the map fine outside the Shiny environment:
bio_leaflet <- leaflet(bio_data) %>% setView(lng = -10, lat = 45, zoom = 2) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addMarkers(
clusterOptions = markerClusterOptions(),
#color = ~pal(first_season),
lng = bio_data$lon, lat = bio_data$lat, # designate the lon and lat values to be plotted as markers on the map
icon = stick_puckIcon,
popup = paste("Player:", bio_data$Player, "<br>",
"Born in: ", bio_data$origin, "<br>",
"Born on: ", bio_data$DOB, "<br>",
"Draft Year: ", bio_data$draft_year, "<br>",
"Round: ", bio_data$Round, "<br>",
"Overall: ", bio_data$Overall, "<br>",
"NHL Debut: ", bio_data$first_season
));
However, when I try to get it to work within a Shiny app I get the following error message:
Listening on http://127.0.0.1:6937
Warning: Error in : Must subset rows with a valid subscript vector.
Logical subscripts must match the size of the indexed input.
x Input has size 7167 but subscriptrows
has size 0.
115:
Warning: Error in : Must subset rows with a valid subscript vector.
Logical subscripts must match the size of the indexed input.
x Input has size 7167 but subscriptrows
has size 0.
112:
My Shiny code:
stick_puckIcon <- makeIcon(
iconUrl = "~/Nextcloud/dysoncloud/Projects/data_vis/origins/stick_puck.png",
iconWidth = 50, iconHeight = 50,
iconAnchorX = 22, iconAnchorY = 94)
ui <- fluidPage(
sidebarPanel( # selection panel
sliderInput(inputId = "debut", # slider to choose range of years for debut NHL season
label = "Choose a Range of NHL Debut Years",
min=1917,
max=2019,
value=c(1917, 2019),
sep = ""),
selectInput(inputId = "country", # selection box to pick a specific country
label = "Choose a Country of Origin",
choices = c("All", sort(unique(as.character(bio_data$Ctry))))),
selectInput(inputId = "position", # selection box to pick a specific position
label = "Choose Position",
choices = c("All", sort(unique(as.character(bio_data$Pos)))))
),
mainPanel(
leafletOutput("bio_leaflet", height = 1040) # panel for the map to render
))
server <- function(input, output, session){
filtered_map <- reactive({ # generate filtered map for the inputs from the selection panel in the ui
rows <- bio_data$first_season<=input$debut[2] & bio_data$first_season>=input$debut[1] &
(input$country == "All" | bio_data$Ctry==input$country) &
(input$position == "All" | bio_data$Pos==input$postition)
bio_data[rows,,drop = FALSE] # remove NULL data, without this line I get "Warning: Error in $: $ operator is invalid for atomic vectors [No stack trace available]"
})
observeEvent( # observe the events in the input frames
input$debut,{
updateSelectInput(session,"country",choices=c("All", sort(unique(as.character(filtered_map()$country)))))
updateSelectInput(session,"position",choices=c("All", sort(unique(as.character(filtered_map()$position)))))
})
output$bio_leaflet <- renderLeaflet({ # output the map and markers based on the filtered_map data
leaflet() %>%
setView(lng = -10, lat = 45, zoom = 3) %>%
addProviderTiles(providers$CartoDB.Positron) %>%
addMarkers(lng = filtered_map()$lon, lat = filtered_map()$lat, # designate the lon and lat values to be plotted as markers on the map
icon = stick_puckIcon, # use the icon defined above
popup = paste("Player:", bio_data$Player, "<br>", # paste various data on the icon popups
"Born in: ", bio_data$origin, "<br>",
"Born on: ", bio_data$DOB, "<br>",
"Draft Year: ", bio_data$draft_year, "<br>",
"Round: ", bio_data$Round, "<br>",
"Overall: ", bio_data$Overall, "<br>",
"NHL Debut: ", bio_data$first_season),
clusterOptions = markerClusterOptions() # cluster markers close to each other
)
})
}
shinyApp(ui = ui, server = server)
I am not sure where I am going wrong. I do not think it has to do with NULL data (there are missing data in the original data frame) as I have a line that removes NULLs in the from the filtered map without which I get: "Warning: Error in : operator is invalid for atomic vectors [No stack trace available]".
Any incites welcome.
Thanks