问题描述
我正在构建一个用于跟踪鲑鱼产卵运行的闪亮应用程序。我已经构建了该应用程序,使其可以用于当前正在运行的某些物种,但是按照我的外部查询的设置方式,除非无法在2020季节收集一条鱼,否则它无法构建表格和图表。对于这些尚未开始运行的物种,我想显示去年的表格和图表。在Shiny中,有没有一种方法可以使用条件面板引用sysDate()甚至更好的原始r代码,以便可以正确确定是绘制2020年数据还是2019年数据?
作为一个例子。 Coho直到八月才开始跑步,直到第二年二月才继续跑步。如果最终用户在这几个月之间查看该应用程序,我希望它显示当前的每日数据。但是,如果最终用户在那几个月之外查看该应用程序,我希望它显示不为“ 2020年运行尚未开始”的去年数据。
ui <- tabsetPanel(id="species",tabPanel(id = "co",title = "Coho",selectInput("coho_origin_select",label = h3("Select origin"),choices = list("Hatchery","Wild"),selected = "Hatchery"),fluidRow(
column(4,offset = 1,plotOutput("co_curvePlot2020")),column(4,plotOutput("co_dailyPlot2020"))),formattableOutput("co_table2020"))
server <- function(input,output) {
output$co_dailyPlot2020 <- renderPlot({
if(input$co_origin_select =="Hatchery") {print(co_daily_plot_AD2020)}
if(input$co_origin_select =="Wild") {print(co_daily_plot_UM2020)}
})
output$co_curvePlot2020 <- renderPlot({
if(input$co_origin_select =="Hatchery") {print(co__ad_plot2020)}
if(input$co_origin_select =="Wild") {print(co__um_plot2020)}
})
output$co_table2020 <- renderFormattable({
if(input$co_origin_select =="Hatchery") ({formattable(co_table_ad2020)})
else if(input$co_origin_select =="Wild") ({formattable(co_table_um2020)})
})
理想情况下,如果用户在2020年8月至2021年2月之间查看该应用程序,则最终服务器端将输出co_dailyPlot2020,co_curvePlot2020和co_table2020,如果他们在2020年8月之前查看该应用程序,则将打印co_dailyPlot2019,co_curvePlot2019和co_table2019。
解决方法
您可以使用相同的output$co_dailyPlot
等,并在render
内确定要使用的数据。为此,我创建了一个取决于日期的布尔变量。请注意,您的条件尚不完全清楚(2021/02之后该怎么办?),所以对于2020/08-2021/02,它只是TRUE
:
ui <- tabsetPanel(id="species",tabPanel(id = "co",title = "Coho",selectInput("coho_origin_select",label = h3("Select origin"),choices = list("Hatchery","Wild"),selected = "Hatchery"),fluidRow(
column(4,offset = 1,plotOutput("co_curvePlot")),column(4,plotOutput("co_dailyPlot"))),formattableOutput("co_table"))
server <- function(input,output) {
show_2020_data <-
(as.numeric(format(Sys.Date(),"%m")) >= 8 &&
as.numeric(format(Sys.Date(),"%Y")) == 2020) ||
(as.numeric(format(Sys.Date(),"%m")) <= 2 &&
as.numeric(format(Sys.Date(),"%Y")) == 2021)
output$co_dailyPlot <- renderPlot({
if (show_2020_data) {
if(input$co_origin_select =="Hatchery") {print(co_daily_plot_AD2020)}
if(input$co_origin_select =="Wild") {print(co_daily_plot_UM2020)}
} else {
# old plots
}
})
output$co_curvePlot <- renderPlot({
if (show_2020_data) {
if(input$co_origin_select =="Hatchery") {print(co__ad_plot2020)}
if(input$co_origin_select =="Wild") {print(co__um_plot2020)}
} else {
# old plots
}
})
output$co_table <- renderFormattable({
if (show_2020_data) {
if(input$co_origin_select =="Hatchery") ({formattable(co_table_ad2020)})
else if(input$co_origin_select =="Wild") ({formattable(co_table_um2020)})
} else {
# old plots
}
})