I'm working on a Shiny app where a leaflet map animates through time, but you can pause it and then use a slider to look at specific years. I've made a dummy version of the functionality and posted it here: https://seth127.shinyapps.io/slider
My issue is, the sliderInput to pick the year (which is inside renderUI in the server.R) doesn't appear until you've first clicked the goButton (which pauses the animation). I can't seem to figure out why.
I want it to default to year 2000 (when input$animate==0). I added a text input to verify that this functionality should work, and it works for the renderText, but not the renderUI. Any help is much appreciated. Code is below:
server.R
require(maps)
require(leaflet)
require(shiny)
fakeData <- data.frame(year=seq(1974,2014,by=1),
lat=seq(34, 40, by=0.1463415),
long=seq(-118,-73, by=1.097561),
artist="fake",
album="fake",
hometown="fake",
chart="PJ")
pickYear <- function(year, data) {
yearCities <- data[as.numeric(data$year)==year, ]
yearCities
}
shinyServer(
function(input, output) {
#refresh animation every 60 seconds
getit <- reactive({
invalidateLater(60000, session=NULL)
Sys.time()
})
#change animation year every 2 seconds
year <- reactive({
if(input$animate %% 2 != 0) {
invalidateLater(2000, session=NULL)
1984 + (round(as.numeric(Sys.time() - getit())/2))
} else {1984 + (round(as.numeric(Sys.time() - getit())/2))}
})
output$pressedGo <- reactive({if(input$animate==0) {"you haven't pressed go"} else {"now you have"}})
output$reactiveSlider <- renderUI({
yearVal <- if(input$animate == 0) {2000} else {year()}
sliderInput('year', label='Pick A Year:', value = yearVal,
min = 1984, max = 2015, step = 1, sep="")
})
output$map <- renderLeaflet({
leaflet(data = pickYear(input$year, fakeData)) %>% addTiles() %>%
addMarkers(~long, ~lat) %>% setView(-97.887318, 40.422487, 4)
})
})
ui.R
require(maps)
require(leaflet)
require(shiny)
shinyUI(
pageWithSidebar(
# Application title
headerPanel(
""
),
sidebarPanel(
),
mainPanel(
uiOutput("reactiveSlider"),
actionButton('animate', "Animate/Pause Map"),
h3(textOutput("pressedGo")),
p(" "),
leafletOutput("map")
)
)
)
reactiveSlider
to something that doesnt start with 'r', it works. – Rorschach