shinyBS createAlert无法工作

时间:2018-04-15 20:30:30

标签: shiny shinybs

createAlert的部分不起作用。任何人都可以帮我修复它吗?任何帮助表示赞赏。

library(shiny)
library(shinyBS)

shinyUI(fluidPage(

    titlePanel("Ouais...Ouais..."),

    sidebarLayout(
        sidebarPanel(
            helpText("A new learner"),

            selectInput("var", 
                        label = "Choose a variable to display",
                        choices = c("begin", "win", "again", "give up"),
                        selected = "begin"),

            radioButtons(
                "filtre",
                label = h5("Do you like me ?"),
                choices = list("Yes" = 1, "No" = 0, "Get out" = 2),
                selected = 1),

           actionButton("initia", label = img(src="imgR.png", height = 200, width = 170) ) 

        ),

        mainPanel(
            **bsAlert("alert1"),**
            textOutput("text1"),
            textOutput("text2")
        )
    )
))
library(shiny)
library(shinyBS)

shinyServer(function(input, output, session) {

    output$text1 <- renderText({
        paste("You have selected", input$var)
    })

    output$text2 <- renderText({
        paste("Do you want to go ? ", input$filtre)
    })

    **output$textalert <- renderText({
        popupvar <- input$var
        if(popupvar == "give up") {
            createAlert(session, "alert1", "exampleAlert", title = "Oops",
                        content = "Go on, you'll win", append = FALSE)
        } else if(popupvar == "begin") {
            createAlert(session, "alert1", "exampleAlert", title = "Oops",
                        content = "Bravo.", append = FALSE)
        } else {
            closeAlert(session, "exampleAlert")
            return("Greateful")
        }

    })**

    observe({
        input$initia
        updateSelectInput(session, "var", selected = "begin")
        updateRadioButtons(session, "filtre", selected = 1 )
    }) 

})

1 个答案:

答案 0 :(得分:0)

您可以使用observeEvent来实现这一目标。看看下面修改后的代码:

library(shiny)
library(shinyBS)

ui <- shinyUI(fluidPage(

  titlePanel("Ouais...Ouais..."),

  sidebarLayout(
    sidebarPanel(
      helpText("A new learner"),

      selectInput("var", 
                  label = "Choose a variable to display",
                  choices = c("begin", "win", "again", "give up"),
                  selected = "begin"),

      radioButtons(
        "filtre",
        label = h5("Do you like me ?"),
        choices = list("Yes" = 1, "No" = 0, "Get out" = 2),
        selected = 1),

      actionButton("initia", label = img(src="imgR.png", height = 200, width = 170) ) 

    ),

    mainPanel(
      bsAlert("alert1"),
        textOutput("text1"),
      textOutput("text2")
    )
  )
))


server <- shinyServer(function(input, output, session) {

  output$text1 <- renderText({
    paste("You have selected", input$var)
  })

  output$text2 <- renderText({
    paste("Do you want to go ? ", input$filtre)
  })

  observeEvent(input$var,{
      popupvar <- input$var
      if(popupvar == "give up") {
        createAlert(session, "alert1", "exampleAlert", title = "Oops",
                    content = "Go on, you'll win", append = FALSE)
      } else if(popupvar == "begin") {
        createAlert(session, "alert1", "exampleAlert", title = "Oops",
                    content = "Bravo.", append = FALSE)
      } else {
        closeAlert(session, "exampleAlert")
        return("Greateful")
      }

  })


    observe({
      input$initia
      updateSelectInput(session, "var", selected = "begin")
      updateRadioButtons(session, "filtre", selected = 1 )
    }) 

})

shinyApp(ui,server)

希望它有所帮助!