Home > Back-end >  How to display different feedbackWarnings depending on the input in shiny?
How to display different feedbackWarnings depending on the input in shiny?

Time:09-06

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 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

CodePudding user response:

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)
  • Related