首页 > 解决方案 > Shiny: Display textOutput() if input is different from the default value

问题描述

I have a Shiny App with multiple inputs of different kinds (checkboxGroupInput, sliderInput, dateRangeInput ...) with default selected values. I am trying to display a text message at the top of my dashboardBody if the input values are different from the default ones.

ui <- dashboardPage(
#one of the inputs
dateRangeInput(
            "date_reception",
            "Sélectionnez une plage de dates",
            start = min(dataset_all$date_reception),
            end = max(dataset_all$date_reception),
          ),
#the output to show if input is different from default
textOutput("warning_filters")


----------


)


server <-  function(input, output) {    

observeEvent(input$date_reception,
                   {
                     if ((input$date_reception[1] != min(dataset_all$date_reception)) |
                         (input$date_reception[2] != max(dataset_all$date_reception))) {
                       output$warning_filters <-
                         renderText({
                           "Warning: filters apply"
                         })
                     } else{
                       NULL
                     }
                   })
}

My issue is that the message is correctly displayed when I change one of the dates but does not disappear when I select again the default value (here, min(dataset_all$date_reception) or max(dataset_all$date_reception)). I have tried playing with ignoreNULL and ignoreInit but nothing changed.

Thank you for your help!

标签: shinyconditional-statements

解决方案


warning_filters当条件为假时,您需要重新分配。现在,warning_filters设置为警告文本,即使您NULL在条件不成立时返回函数,您实际上也不会更改warning_filters. 下面的代码应该可以工作。

observeEvent(input$date_reception,
                   {
                     if ((input$date_reception[1] != min(dataset_all$date_reception)) |
                         (input$date_reception[2] != max(dataset_all$date_reception))) {
                       output$warning_filters <-
                         renderText({
                           "Warning: filters apply"
                         })
                     } else{
                       output$warning_filters <- NULL
                     }
                   })
}


推荐阅读