Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sliderInput for Dates

Tags:

r

shiny

This is causing me a lot of pain.

I would like to simlpy have a sliderInput that takes a Date (preferably stepping by month) and changes a simple ggplot_bar as a result. Although I can show everything there seems to be no response to the changing of the slider:

Here is my code:

ui.r

library(shiny)

# Define UI for application that draws a histogram
shinyUI(fluidPage(

  # Application title
  titlePanel("St Thomas' Physiology Data Console"),

  # Sidebar with a slider input for the number of bins
  sidebarLayout(
    sidebarPanel(
      sliderInput("DatesMerge",
                  "Dates:",
                  min = as.Date("2006-01-01","%Y-%m-%d"),
                  max = as.Date("2016-12-01","%Y-%m-%d"),
                  value=as.Date("2016-12-01"),timeFormat="%Y-%m-%d")
    ),

    # Show a plot of the generated distribution

      mainPanel(
        tabsetPanel(
          tabPanel("Breath Tests",plotOutput("distPlotLactul")),
    )
  )
))

server.r

library(shiny)

source("S:\\Usage.R")

# Define server logic required to draw a histogram
shinyServer(function(input, output) {

  output$distPlotLactul <- renderPlot({
    #Create the data
    DatesMerge<-input$DatesMerge

    # draw the histogram with the specified number of bins

   ggplot(TotsLactul)+
     geom_bar(aes(DatesMerge,fill=year))+
     labs(title=paste("Num")) +
     xlab("Time") +
     ylab("NumP") +
     theme(axis.text.x=element_text(angle=-90)) +
     theme(legend.position="top")+
     theme(axis.text=element_text(size=6))


  })


})
like image 973
Sebastian Zeki Avatar asked Dec 01 '16 10:12

Sebastian Zeki


1 Answers

I wasn't totally sure of your ggplot code, so I had to rejig into something I understood.

I also created my own data to make it reproducible.

Here is the data I made

# Generate random variates
TotsLactul        <- rep(ymd("2016-01-01"),10000)
randomMonths      <- round(runif(n = 10000,min = 0,max = 11),0) 
randomDays        <- round(runif(n = 10000,min = 0,max = 28),0)

# Increments days
month(TotsLactul) <- month(TotsLactul) + randomMonths  
day(TotsLactul)   <- day(TotsLactul)   + randomDays  

# Make it a DT
TotsLactul        <- data.table(x=TotsLactul)

This is just random dates throughout the year.

UI

ui <- shinyUI(fluidPage(

          # Application title
          titlePanel("St Thomas' Physiology Data Console"),

          # Sidebar with a slider input for the number of bins
          sidebarLayout(
            sidebarPanel(
              sliderInput("DatesMerge",
                          "Dates:",
                          min = as.Date("2016-01-01","%Y-%m-%d"),
                          max = as.Date("2016-12-01","%Y-%m-%d"),
                          value=as.Date("2016-12-01"),
                          timeFormat="%Y-%m-%d")
            ),
            mainPanel(
                plotOutput("distPlotLactul"))

            )
          ))

I amended the slider to only take 2016 values, to match my generated data

Server

server <- shinyServer(function(input, output) {

          output$distPlotLactul <- renderPlot({
            #Create the data
            DatesMerge<-input$DatesMerge

            # draw the histogram with the specified number of bins
            ggplot(TotsLactul[month(x) == month(DatesMerge)],mapping=aes(x=x))+
              geom_histogram(bins=100)+
              labs(title=paste("Num")) +
              xlab("Time") +
              ylab("NumP") +
              theme(axis.text.x=element_text(angle=-90)) +
              theme(legend.position="top")+
              theme(axis.text=element_text(size=6))


          })


        })

I'll be honest, I have never used ggplot like you have (just dropped in a table in a geom etc.), so I can't comment on if any of it was right / wrong. Hopefully you can follow my changes.

  • Changed geom_bar to geom_hist (to match my data)
  • The filtering happens in the data included in the plot, not within the geom.

This seems to work fine, let me know how you get on.

like image 97
T.Holme Avatar answered Sep 23 '22 16:09

T.Holme