B. Davis
B. Davis

Reputation: 3441

plotting points within a date range specified with shiny slider bar

From a larger dataset, I want to only plot points that are within a min and max date that is specified with a shiny slider bar containing a date range. This post builds from a related post linked here. Data are contained at the bottom using dput.

The code/app below sequentially plots points as the date is increased with the slider bar. When I move the 2nd slider bar I want points no longer in the date range to be removed, which currenlty does not happen.

How do I subset the data so that only points (and paths) >= the min date and <= the max date are shown? It is not clear to me how to reference the two dates on the slider bar.

Thanks in advance.

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 = c(as.Date(min(dat$PosiGMT)), 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)

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: 1

Views: 2028

Answers (1)

Victorp
Victorp

Reputation: 13856

Hi input$Order is a vector of length 2, so input$Order[1] is the min and input$Order[2] the max, you can do something like this :

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 = c(as.Date(min(dat$PosiGMT)), as.Date(min(dat$PosiGMT)))),
  plotOutput("PointPlot")
)

server <- function(input, output) {
  output$PointPlot <- renderPlot({
    ### Filter by date
    dat <- dat[as.Date(dat$PosiGMT) >= input$Order[1] & as.Date(dat$PosiGMT) <= input$Order[2] ,]
    ###
    p <- ggplot(dat, (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)

Upvotes: 2

Related Questions