我刚开始使用Shiny并使用它来显示所选站点的风频输出(速度和方向)。
每个数据集都有不同的日期范围,当用户选择其他网站时,我似乎无法将这些可变日期范围返回到UI中的dateRangeInput。
如果选择的日期超出所选数据集范围,则发光错误并在任何地方显示红色文本。为了解决这个问题,我使用最小/最大范围来输入dataRangeInput。
用户从用户界面中的下拉菜单中选择一个网站" selectInput'它创建变量input / ID" df"。
我可以获得任何工作的唯一方法是使用特定数据集的日期范围将日期范围作为全局变量传递,但这实际上是一个手动过程:
创建一些样本数据(在实际数据中,日期是日期/时间格式化):
library(shiny)
library(openair)
library(ggplot2)
date<- c("2017-01-01","2017-01-02","2017-01-03","2017-01-04")
ws<-c(rep(0:3,1))
SiteA <- data.frame(date,ws,stringsAsFactors = FALSE)
date<- c("2017-01-01","2017-01-02","2017-01-03","2017-01-04","2017-01-05", "2017-01-06")
ws<-c(rep(0:5,1))
SiteB <- data.frame(date,ws,stringsAsFactors = FALSE)
从数据中提取最小值和最大值(这是我需要帮助的地方):
# Date ranges currently fixed to SiteA (need this to be dynamic based on selection)
mystartdate <- as.character(format(as.Date(min(SiteA$date))),"yyyy-mm-dd")
myenddate <- as.character(format(as.Date(max(SiteA$date))),"yyyy-mm-dd")
然后将这些日期传递到用户界面中的日期范围:
ui <- fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
selectInput("df","Choose a Site:",
choices = c(
"Site A" = 'SiteA',
"Site B" = 'SiteB')
),
dateRangeInput("date", "Select the date range:",
start = mystartdate, # Start date of the selected df
end = myenddate, # End date of the selected df
min = mystartdate,
max = myenddate,
format = "yyyy-mm-dd")
),
mainPanel(
strong(paste("Frequency Distribution:")),
plotOutput("hist")
)
)
)
然后将日期范围作为输入传递,以过滤输入$ date [1]并输入$ date [2]。
server <- function(input, output) {
df <- reactive({
x <- get(input$df)
})
output$hist <- renderPlot({
df <- df() # df is the user selected dataset
dfhist<- selectByDate(df, start = input$date[1], end = input$date[2])
ggplot(df, aes(df$ws)) +
geom_histogram(fill = "blue", bins = 50) +
labs(x="Wind Speed (m/s)", y="Count")
})
}
shinyApp(ui = ui, server = server)
如何从所选数据集(SiteA或SiteB)中提取最小/最大日期并将这些日期返回到dateRangeInput单元格?
任何想法/指针都会非常感激。谢谢
答案 0 :(得分:2)
我从所选数据集(SiteA或SiteB)中提取最小/最大数据,并将这些数据返回到dateRangeInput单元格。我根据dataRangeInput上选择的日期在数据框中创建了子集以显示图。我相信这可以满足您的所有应用。
library(shiny)
library(openair)
library(ggplot2)
library(readr)
ui <- fluidPage(
titlePanel("title"),
sidebarLayout(
sidebarPanel(
selectInput("df","Choose a Site:",
choices = c(
"Site A" = 'SiteA',
"Site B" = 'SiteB')
),
uiOutput("dateRange")
),
mainPanel(
strong(paste("Frequency Distribution:")),
plotOutput("hist")
)
)
)
server <- function(input, output, session) {
df <- reactive({
if(input$df == "SiteA"){
date<- c("2017-01-01","2017-01-02","2017-01-03","2017-01-04")
ws<-c(rep(0:3,1))
SiteA <- data.frame(date,ws,stringsAsFactors = FALSE)
x <- SiteA
}
if(input$df == "SiteB"){
date<- c("2017-01-01","2017-01-02","2017-01-03",
"2017-01-04","2017-01-05", "2017-01-06")
ws<-c(rep(0:5,1))
SiteB <- data.frame(date,ws,stringsAsFactors = FALSE)
x <- SiteB
}
return(x)
})
output$dateRange <- renderUI({
dateRangeInput("date", "Select the date range:",
start =
as.character(format(as.Date(min(df()$date))),"yyyy-mm-dd"), # Start
end =
as.character(format(as.Date(max(df()$date))),"yyyy-mm-dd"), # End
min =
as.character(format(as.Date(min(df()$date))),"yyyy-mm-dd"),
max =
as.character(format(as.Date(max(df()$date))),"yyyy-mm-dd"),
format = "yyyy-mm-dd")
})
output$hist <- renderPlot({
df <- df() # df is the user selected dataset
dfhist<- subset(df, date >= as.Date(input$date[1]) & date <=
as.Date(input$date[2]) )
ggplot(dfhist, aes(dfhist$ws)) +
geom_histogram(fill = "blue", bins = 50) +
labs(x="Wind Speed (m/s)", y="Count")
})
}
shinyApp(ui=ui, server = server)