【问题标题】:How to sliderInput for Dates如何为日期滑动输入
【发布时间】:2017-04-15 23:35:29
【问题描述】:

这让我很痛苦。

我想 simlpy 有一个滑块输入,它需要一个日期(最好按月步进)并因此更改一个简单的 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))


  })


})

【问题讨论】:

  • 谢谢@PorkChop,但我的问题不同。我将日期格式化为月份没有问题,但问题更多在于图表没有通过相应地过滤日期来响应滑块
  • 你能做到吗:dput(TotsLactul) 请把数据贴在这里

标签: r shiny


【解决方案1】:

我不完全确定你的 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 <- 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(以匹配我的数据)
  • 过滤发生在绘图中包含的数据中,而不是在几何图形中。

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

【讨论】:

    猜你喜欢
    • 2020-09-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-20
    • 2022-06-16
    • 1970-01-01
    相关资源
    最近更新 更多