R Shiny Slider Help

Hey everyone, Im trying to create a reactive slider for a map in my shiny web app but am running into trouble. Firstly, I want the slider to range from the year 1942 to 2021; however, every time I then run the app the slider has the years displayed as such "1,942" and "2,021" instead of "1942" and "2021." I was wondering if anyone knew how I could get R to stop adding the comma and just leave it as is? The slider does not work and has no change on my corresponding map, but I'm assuming that is due to the disparity in "1,942" and the data saying "1942"

data <- read.csv("data.csv")

ui <- fluidPage(
    titlePanel(title=h1("History of Teams in the NHL", align="center")),
    sidebarPanel( p("Adjust slider to show NHL teams"),
                   sliderInput("range", "Year", 1942, 2021, value = c(1942,2021), step = 1
                   )),
    mainPanel(leafletOutput("mymap")))

server <- function(input, output, session) {
    hockey <- reactive({
        data[data$Year >= input$range[1] & data$Year <= input$range[2],]
    })    

    output$mymap <- renderLeaflet({
        leaflet(data) %>% 
            addTiles() %>%
        addMarkers(data = data, ~Long, ~Lat) 
    })
}
shinyApp(ui, server)

Hey!

Don't know about the comma in the years (might be a LOCALE setting, or maybe you could try passing those numbers as a date object).

Regarding the map, I see one main problem, you are using data and not hockey, which is the reactive data.
So, it should look sth like:

    output$mymap <- renderLeaflet({
        leaflet(hockey()) %>% 
            addTiles() %>%
        addMarkers(~Long, ~Lat) 
    })

Related, you could take a look into leafletProxy in case you start running into performance issues (due to re-rendering the map each time your data changes).

HTH


This post was published by an Appsilon team member. Our company can help you get the most out of RShiny and Posit/RStudio products.

Check our open positions here.

Appsilon: Building impactful RShiny Dashboards and providing R coding services.
Appsilon_GIFsmall_whitebg

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.