How to sliderInput for Dates

后端 未结 1 646
死守一世寂寞
死守一世寂寞 2021-01-18 08:04

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 res

相关标签:
1条回答
  • 2021-01-18 08:45

    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.

    0 讨论(0)
提交回复
热议问题