简体   繁体   中英

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM