12

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))


  })


})
Sebastian Zeki
  • 6,690
  • 11
  • 60
  • 125
  • have a look here http://stackoverflow.com/questions/37689689/shiny-slider-input-step-by-month/37695860#37695860 – Pork Chop Dec 01 '16 at 11:09
  • Thanks @PorkChop but my question is different. I have no problem formatting the dates to month but the problem is more that the graph doesnt respond to the slider by filtering the dates accordingly – Sebastian Zeki Dec 01 '16 at 11:43
  • can you do: `dput(TotsLactul)` and post the data here please – Pork Chop Dec 01 '16 at 11:44

1 Answers1

11

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.

T.Holme
  • 473
  • 4
  • 17