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

问题描述 投票:2回答:1

我正在尝试使用日期范围功能来改变用于在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)
r shiny date-range
1个回答
3
投票

您可以根据日期范围对您的矢量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)
© www.soinside.com 2019 - 2024. All rights reserved.