Search code examples
rshinyshinybs

shiny bsModal close button


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.


Solution

  • You can not really do it at the bsModal which runs on client, but you can easily do this in the server:

    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)