如何滑动输入日期 [英] How to sliderInput for Dates

查看:6
本文介绍了如何滑动输入日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这给我带来很大的痛苦。

我想简单地使用一个liderInput,它接受一个日期(最好是按月步进),并因此更改一个简单的ggploybar。虽然我可以显示所有内容,但似乎对更改滑块没有响应:

以下是我的代码:

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

服务器。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))


  })


})

推荐答案

我不完全确定您的gglot代码,所以我必须重新编写一些我能理解的内容。

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

这是我创建的数据

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


          })


        })

老实说,我从来没有像您那样使用过gglot(只是在geom中掉了张表等等),所以我不能评论其中是否有对/错。希望您能跟随我的更改。

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

这似乎工作得很好,请让我知道您的进展情况。

这篇关于如何滑动输入日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆