如何滑动日期输入

Seb*_*eki 8 r shiny

这让我很痛苦。

我想 simlpy 有一个带有日期的滑块输入(最好按月步进)并因此更改一个简单的 ggplot_bar 。尽管我可以显示所有内容,但似乎对滑块的更改没有响应:

这是我的代码:

用户界面

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")),
    )
  )
))
Run Code Online (Sandbox Code Playgroud)

服务器文件

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


  })


})
Run Code Online (Sandbox Code Playgroud)

T.H*_*lme 6

我不完全确定你的 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)
Run Code Online (Sandbox Code Playgroud)

这只是全年的随机日期。

用户界面

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

            )
          ))
Run Code Online (Sandbox Code Playgroud)

我将滑块修改为仅采用 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))


          })


        })
Run Code Online (Sandbox Code Playgroud)

老实说,我从来没有像你一样使用过 ggplot(只是放在一个几何图形的桌子上),所以我不能评论它是否是对的/错的。希望你能关注我的变化。

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

这似乎工作正常,让我知道你的进展情况。