【问题标题】:Can I use a conditional panel to only display data if the sysDate() falls between two dates?如果 sysDate() 介于两个日期之间,我可以使用条件面板仅显示数据吗?
【发布时间】:2020-08-26 17:12:36
【问题描述】:

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

举个例子。 Coho 直到 8 月才开始运行,它们的运行一直持续到下一个 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 月之间查看应用程序,最终服务器端将输出 co_dailyPlot2020、co_curvePlot2020 和 co_table2020,如果他们在 2020 年 8 月之前查看它,则会打印 co_dailyPlot2019、co_curvePlot2019 和 co_table2019。

【问题讨论】:

  • 是的,你可以这样做。如果您可以提供一些可以修改的闪亮应用程序代码,我相信您会在遇到特定问题时寻求帮助。
  • @cory 我认为这应该足以提出一些建议。

标签: r shiny shinyapps


【解决方案1】:

您可以使用相同的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
                      }
                    })

【讨论】:

  • @straja 我可以改用 show_present_data = 8) || (as.numeric(format(Sys.Date(), "%m"))
  • 如果您确保当前数据与您要显示的数据相匹配(例如,2021 年 2 月的当前数据来自 2020 年,而 2021 年 8 月的当前数据来自 2021 年),请确保这是选项。您可以使用适合您的需求/存储数据的方式
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-01-09
  • 1970-01-01
  • 2016-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多