Brian P
Brian P

Reputation: 1516

Shiny application -- how to suppress function and rendering on launch?

I developed a Shiny App with RStudio that takes input, performs a search of a look-up table, and then returns a value. The search is not supposed to be performed until the user hits the submit button. However, upon launch, the App automatically performs and returns the first set of values from the lookup table. After this initial search, the app works exactly as expected. Is it possible to suppress this initial search (or have a default value) and perform the search ONLY when the submit button is pressed? I can't present reproducible code, but here is the structure of my input (server.R) and my user-interface (ui.R) code:

#server.R snippet
output$Word <- renderText({
    predictWord <- input$gram
    predict.function(predictWord)   #User-defined function in global.r file
    })


#ui.R snippet

tabPanel("Word Prediction",
     sidebarPanel(
     textInput("gram", "Enter up to three words"),
     submitButton("Predict")),

mainPanel(
    h4("Word Prediction"),
    textOutput("predictWord")))

Upvotes: 1

Views: 293

Answers (1)

cdeterman
cdeterman

Reputation: 19960

One way would be to substitute actionButton in place of submitButton and wrap whichever component in an if statement. Here is a simple example to display a number only slightly modified from the Shiny Widget Gallery.

require(shiny)
runApp(list(
  ui = pageWithSidebar(
    headerPanel("actionButton test"),
    sidebarPanel(
      numericInput("n", "N:", min = 0, max = 100, value = 50),
      br(),
      actionButton("goButton", "Go!"),
      p("Click the button to update the value displayed in the main panel.")
    ),
    mainPanel(
      verbatimTextOutput("nText")
    )
  ),
  server = function(input, output){
    output$nText <- renderText({
      # Take a dependency on input$goButton
      if(input$goButton >= 1){

        # Use isolate() to avoid dependency on input$n
        isolate(input$n)
      }
    })
  }
  )
  )

Upvotes: 1

Related Questions