Shiny submitButton behavior at page load time

夙愿已清 提交于 2019-12-20 19:10:09

问题


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)
  })
})

回答1:


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.




回答2:


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)
  })
}


来源:https://stackoverflow.com/questions/15840803/shiny-submitbutton-behavior-at-page-load-time

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!