如何使用slider输入日期

时间:2016-12-01 10:56:38

标签: r shiny

这给我带来了很大的痛苦。

我想让simlpy有一个带有Date的sliderInput(最好是按月逐步),并更改一个简单的ggplot_bar。虽然我可以显示所有内容但似乎没有响应滑块的更改:

这是我的代码:

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


  })


})

1 个答案:

答案 0 :(得分:2)

我不完全确定你的ggplot代码,所以我不得不重新调整我理解的东西。

我还创建了自己的数据以使其可重现。

以下是我制作的数据

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

这是全年的随机日期。

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

            )
          ))

我将滑块修改为仅采用2016年值,以匹配我生成的数据

服务器

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


          })


        })

我说实话,我从来没有像你一样使用ggplot(只是放在geom等表中),所以我无法评论它是否正确/错误。希望你能跟随我的改变。

  • 将geom_bar更改为geom_hist(以匹配我的数据)
  • 过滤发生在图中包含的数据中,而不是在geom中。

这似乎工作正常,让我知道你是如何继续下去的。

相关问题