如果 sysDate() 介于两个日期之间,我可以使用条件面板仅显示数据吗?

Can I use a conditional panel to only display data if the sysDate() falls between two dates?

我正在构建一个用于跟踪鲑鱼产卵的闪亮应用程序 运行。我已经构建了适用于当前 运行ning 的某些物种的应用程序,但我的外部查询设置方式无法构建表格和图表,除非在 2020 年季节收集了一条鱼。对于这些尚未开始 运行 的物种,我想展示去年的表格和图表。 Shiny 有没有办法让条件面板引用 sysDate() 或者更好的是原始 r 代码,以便它可以正确确定是绘制 2020 年数据还是 2019 年数据?

举个例子。 Coho 直到 8 月才开始 运行ning,它们的 运行 一直持续到次年 2 月。如果最终用户在这几个月之间查看应用程序,我希望它显示当前的每日数据。但是,如果最终用户在那些月份之外查看应用程序,我希望它显示去年的数据,而不是显示“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, offset = 1, 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 月之间查看该应用,并且如果他们在 8 月之前查看应用,则最终服务器端将输出 co_dailyPlot2020、co_curvePlot2020 和 co_table2020 2020 年它将打印 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, offset = 1, 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
                      }
                    })