B. Davis
B. Davis

Reputation: 3441

Maintain date range when using sliderInput in shiny

Diving into my first shinyApp...

I have 25 GPS locations at the bottom of the post, the head of which are below. Dates are formatted as.POSIXct

head(dat)
  GPSUTMNorthing GPSUTMEasting             PosiGMT
1        4947787        600201 2013-02-09 00:00:00
2        4947945        600910 2013-02-09 06:00:00
5        4947957        600911 2013-02-10 00:00:00
6        4947954        600907 2013-02-10 06:00:00
8        4947797        601052 2013-02-10 18:00:00
9        4947835        601038 2013-02-11 00:00:00

The UI and server objects below can be used to make a shinyApp that plots the locations that correspond to the date selected on the sliderInput. Rather than plot only the locations from a specific date, I want to sequentially add locations as the date is moved forward on the sliderInput. In this way the points from the first date are maintained as the the sliderInput is moved sequentially forward.

Any suggestions would be greatly appreciated.

library(ggplot2)
library(shiny)
ui <- fluidPage(
          titlePanel("GPS Data Summary"),
          sliderInput(inputId = "Order",
                  label = "Sequance of Observations",  
                  min = as.Date(min(dat$PosiGMT)), max = as.Date(max(dat$PosiGMT)), value = as.Date(min(dat$PosiGMT))),
          plotOutput("PointPlot")
            )

server <- function(input, output) {
            output$PointPlot <- renderPlot({
              p <- ggplot(dat[as.Date(dat$PosiGMT) == input$Order ,], (aes(x = GPSUTMEasting , y = GPSUTMNorthing ))) + 
                geom_point() + geom_path() + 
                xlim( min(dat$GPSUTMEasting), max(dat$GPSUTMEasting))+
                ylim( min(dat$GPSUTMNorthing), max(dat$GPSUTMNorthing))
              print(p)
              })
}


shinyApp(ui = ui, server = server)

Reproducible data below

dat <- structure(list(GPSUTMNorthing = c(4947787L, 4947945L, 4947957L, 
    4947954L, 4947797L, 4947835L, 4947825L, 4947784L, 4947842L, 4947839L, 
    4947789L, 4947807L, 4947839L, 4947845L, 4947779L, 4947824L, 4947824L, 
    4947772L, 4947824L, 4947821L, 4947816L, 4947809L, 4947840L, 4947829L, 
    4947820L), GPSUTMEasting = c(600201L, 600910L, 600911L, 600907L, 
    601052L, 601038L, 601031L, 601066L, 600998L, 600995L, 601058L, 
    601038L, 600987L, 601071L, 601016L, 601002L, 601003L, 601003L, 
    600917L, 600916L, 600918L, 600923L, 600985L, 600980L, 600914L
    ), PosiGMT = structure(c(1360393200, 1360414800, 1360479600, 
    1360501200, 1360544400, 1360566000, 1360587600, 1360630800, 1360652400, 
    1360674000, 1360695600, 1360717200, 1360738800, 1360803600, 1360825200, 
    1360846800, 1360868400, 1360890000, 1360911600, 1360933200, 1360954800, 
    1360976400, 1360998000, 1361019600, 1361041200), class = c("POSIXct", 
    "POSIXt"), tzone = "")), .Names = c("GPSUTMNorthing", "GPSUTMEasting", 
    "PosiGMT"), row.names = c(1L, 2L, 5L, 6L, 8L, 9L, 10L, 12L, 13L, 
    14L, 15L, 16L, 17L, 20L, 21L, 22L, 23L, 24L, 25L, 26L, 27L, 28L, 
    29L, 30L, 31L), class = "data.frame")

Upvotes: 2

Views: 851

Answers (1)

Chris
Chris

Reputation: 6362

You almost had it, this is not a shiny issue but a subsetting one.

Change:

ggplot(dat[as.Date(dat$PosiGMT) == input$Order ,]

to

ggplot(dat[as.Date(dat$PosiGMT) <= input$Order ,]

I think this is the behaviour you want

Upvotes: 2

Related Questions