Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shiny submitButton behavior at page load time

Tags:

r

shiny

The submit button is useful, but I have not found an elegant way to suppress output on initial page load.

For example the Shiny tutorial renders output on load.
http://rstudio.github.com/shiny/tutorial/#more-widgets

How do I make sure that no reactive functions are called until the submit button is pressed?

Here is the code inline for the example linked above.

#ui.R
library(shiny)

# Define UI for dataset viewer application
shinyUI(pageWithSidebar(

  # Application title.
  headerPanel("More Widgets"),

  # Sidebar with controls to select a dataset and specify the number
  # of observations to view. The helpText function is also used to 
  # include clarifying text. Most notably, the inclusion of a 
  # submitButton defers the rendering of output until the user 
  # explicitly clicks the button (rather than doing it immediately
  # when inputs change). This is useful if the computations required
  # to render output are inordinately time-consuming.
  sidebarPanel(
    selectInput("dataset", "Choose a dataset:", 
                choices = c("rock", "pressure", "cars")),

    numericInput("obs", "Number of observations to view:", 10),

    helpText("Note: while the data view will show only the specified",
             "number of observations, the summary will still be based",
             "on the full dataset."),

    submitButton("Update View")
  ),

  # Show a summary of the dataset and an HTML table with the requested
  # number of observations. Note the use of the h4 function to provide
  # an additional header above each output section.
  mainPanel(
    h4("Summary"),
    verbatimTextOutput("summary"),

    h4("Observations"),
    tableOutput("view")
  )
))



#server.R
library(shiny)
library(datasets)

# Define server logic required to summarize and view the selected dataset
shinyServer(function(input, output) {

  # Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })

  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })
})
like image 490
Jesse Avatar asked Apr 05 '13 18:04

Jesse


Video Answer


2 Answers

One way is to use actionButton (from the shiny-incubator package) and isolate. Here's a writeup that explains how to use the two together. This is an inherently more flexible approach than submitButton, which is a bit too heavy-handed and not flexible enough.

like image 110
Joe Cheng Avatar answered Oct 02 '22 06:10

Joe Cheng


I had the same problem for a text field input and I used a VERY dirty work around:

I set the initial field value to something like "please_type_stuff_here". In server.R I used an if clause to decide if there is something to be returned to the ui or not:

if (input$data=="please_type_stuff_here") {

} else {
# Return the requested dataset
  datasetInput <- reactive({
    switch(input$dataset,
           "rock" = rock,
           "pressure" = pressure,
           "cars" = cars)
  })

  # Generate a summary of the dataset
  output$summary <- renderPrint({
    dataset <- datasetInput()
    summary(dataset)
  })

  # Show the first "n" observations
  output$view <- renderTable({
    head(datasetInput(), n = input$obs)
  })
}
like image 34
Robert Adams Avatar answered Oct 02 '22 06:10

Robert Adams