我正在尝试使用日期范围函数来更改用于在 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)
关于r - 使用日期范围更改服务器 R Shiny 中的直方图范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49059909/