rshinyfeedbackselectinputuser-feedback

How to display different feedbackWarnings depending on the input in shiny?


I am trying to create an app where I can have different warnings (using feedbackWarning from the package shinyFeedback) depending on the user's input.

I saw this post where they were talking about this, but although I added if statements, the warnings don't work as they should.

As you can see in the code below, if you first selection is "second" you will see the message "second", but if you select "third" later the previous message is still there and it doesn't update.

im1

img2

Note that I don't want to control the selectInput with actionButtons since I want to show the message before clicking anything else.

library(shiny)
library(shinyFeedback)

ui <- fluidPage(
  useShinyFeedback(), # include shinyFeedback
  
  selectInput(
    inputId = "test", 
    label="Multiple warnings",
    choices = c("One", "Second", "Third")
  )
)

server <- function(input, output) {
  observeEvent(input$test, {
    if (input$test == "One") {
      feedbackWarning(
        inputId = "test",
        show = input$test == "One",
        text = "First"
      )
    }
    if (input$test == "Second") {
      feedbackWarning(
        inputId = "test",
        show = input$test == "Second",
        text = "Second"
      )
    }else{
      feedbackWarning(
        inputId = "test",
        show = input$test == "Third",
        text = "Third"
      )
    }
  })
}

shinyApp(ui, server)

Does anybody know how to fix it?

Thanks in advance


Solution

  • You'll need to reset the previous warning via hideFeedback:

    library(shiny)
    library(shinyFeedback)
    
    ui <- fluidPage(
      useShinyFeedback(), # include shinyFeedback
      selectInput(
        inputId = "test", 
        label="Multiple warnings",
        choices = c("One", "Second", "Third")
      )
    )
    
    server <- function(input, output) {
      observeEvent(input$test, {
        hideFeedback("test")
        my_text <- ""
        if(input$test == "Second"){
          my_text <- "Second text"
        } else if (input$test == "Third"){
          my_text <- "Third text"
        }
        feedbackWarning(
          inputId = "test",
          show = input$test == "Second" || input$test == "Third",
          text = my_text
        )
      })
    }
    
    shinyApp(ui, server)