使用日期范围更改服务器 R Shiny 中的直方图范围

Using date range to alter histogram range in server R Shiny

我正在尝试使用日期范围函数来更改用于在 R Shiny 中显示直方图的数据。我有一些不完整的代码,因为我不知道如何在服务器函数中对此进行编码。请参阅下面的最小代码示例以及我认为应该放置一些代码的位置。 :

library(shiny)
set.seed(123)


N<- 500
M<-56

EF<- matrix( rnorm(N*M,mean=23,sd=3), N, M)
WM<- matrix( rnorm(N*M,mean=20,sd=3), N, M)
DP<- matrix( rnorm(N*M,mean=25,sd=3), N, M)

Date <- seq(as.Date("2018-01-01"), as.Date("2018-02-25"), by="days")
Date <- as.POSIXct(Date, format = "%Y-%m-%d %H:%M")

ui <- fluidPage(
  titlePanel(code(strong("Measures"), style = "color:black")),
  sidebarLayout(
    sidebarPanel(
      strong("Tools:"),
      selectInput("Test", 
                  label = "Choose a measure to display",
                  choices = c("EF", 
                              "WM",
                              "DP"
                  ),
                  selected = "EF"),

      dateRangeInput("DateRange", label= "Date Range:", start ="2018-01-01", end = "2018-02-25")),
    mainPanel(
      code(strong("Study Readout")),
      plotOutput("distPlot")
    ))
)


server <- function(input, output) {

  filterData <- reactive({
      x    <- switch(input$Test, 
                     "EF" = EF,
                     "WM" = WM, 
                     "DP" = DP)
      return(x)

  })

  output$distPlot <- renderPlot({
    x <-filterData()

    DateRange <- #????

      hist(x, #????)
  })
}


# Run that shit ----
shinyApp(ui = ui, server = server)

您可以根据日期范围对向量 x 进行子集化,注释掉到 POSIXct 的转换并像这样填充空白。

library(shiny)
set.seed(123)

N<-500
M<-56

EF<- matrix( rnorm(N*M,mean=23,sd=3), N, M)
WM<- matrix( rnorm(N*M,mean=20,sd=3), N, M)
DP<- matrix( rnorm(N*M,mean=25,sd=3), N, M)

Date <- seq(as.Date("2018-01-01"), as.Date("2018-02-25"), by="days")
#Date <- as.POSIXct(Date, format = "%Y-%m-%d")

ui <- fluidPage(
  titlePanel(code(strong("Measures"), style = "color:black")),
  sidebarLayout(
    sidebarPanel(
      strong("Tools:"),
      selectInput("Test", 
                  label = "Choose a measure to display",
                  choices = c("EF", 
                              "WM",
                              "DP"
                  ),
                  selected = "EF"),

      dateRangeInput("DateRange", label= "Date Range:", start ="2018-01-01", end = "2018-02-25")),
    mainPanel(
      code(strong("Study Readout")),
      plotOutput("distPlot")
    ))
)

server <- function(input, output) {

  filterData <- reactive({
    x    <- switch(input$Test, 
                   "EF" = EF,
                   "WM" = WM, 
                   "DP" = DP)
    return(x)

  })

  output$distPlot <- renderPlot({
    x <-filterData()
    hist(x[Date >= min(input$DateRange) & Date <= max(input$DateRange)])
  })
}

shinyApp(ui = ui, server = server)