1

I have a actionButton called Ok. When user clicks this button, it will take the input from a textInput box and show a bsModal message dialog window with some message.

This is the code:

library(shiny)
library(shinydashboard)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(

    textInput("text", "Enter Id:"),
    box(width = 1, background  = 'purple'),
    actionButton("Ok", "Press Ok",style='padding:8px; font-size:100%')
  ),

  dashboardBody(

    bsModal("modalnew", "Greetings", "Ok", size = "small",
            textOutput("text1")
    )

    )
  )

server <- function(input, output) { 

  observeEvent(input$Ok,{

    patid1 <- as.numeric(input$text)
    print(patid1)



    if (is.na(patid1) == TRUE) { output$text1 <- renderText("Please enter 
    a valid ID# without alphabets or special characters")} else {

      #output$text1 <-renderText("")
      output$text1 <-renderText({paste("You enetered", patid1)})
    }

  })

}

shinyApp(ui, server)

What I am trying to do is when the user clicks on Close button on the bsModal window, it should clear the text in the textInput text box. I have no idea how to add a reactive function on the close button in the bsModal message window. Any help is much appreciated.

4

1 回答 1

2

你不能真正bsModal在客户端上运行它,但你可以在服务器上轻松地做到这一点:

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

  observeEvent(input$Ok,{

    patid1 <- as.numeric(input$text)

    # Clear input$text
    updateTextInput(session,"text", value="")


    if (is.na(patid1) == TRUE) { output$text1 <- renderText("Please enter 
    a valid ID# without alphabets or special characters")} else {

      output$text1 <-renderText({
        paste("You enetered", patid1)})
    }

  })

}

shinyApp(ui, server)
于 2016-10-21T23:26:41.990 回答