Skip to content

Commit c8e1f89

Browse files
committed
Improve documentation for submitButton and change 07_widgets example to use an action button (#1475)
* update 07_widgets example * improved documentation for submitButton (including a warnign section and an full-app example) * typo * update documentation based on Winton's feedback
1 parent e4a6228 commit c8e1f89

File tree

5 files changed

+107
-26
lines changed

5 files changed

+107
-26
lines changed

R/input-submit.R

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,27 @@
11
#' Create a submit button
22
#'
3-
#' Create a submit button for an input form. Forms that include a submit
3+
#' Create a submit button for an app. Apps that include a submit
44
#' button do not automatically update their outputs when inputs change,
55
#' rather they wait until the user explicitly clicks the submit button.
6+
#' The use of \code{submitButton} is generally discouraged in favor of
7+
#' the more versatile \code{\link{actionButton}} (see details below).
8+
#'
9+
#' Submit buttons are unusual Shiny inputs, and we recommend using
10+
#' \code{\link{actionButton}} instead of \code{submitButton} when you
11+
#' want to delay a reaction.
12+
#' See \href{http://shiny.rstudio.com/articles/action-buttons.html}{this
13+
#' article} for more information (including a demo of how to "translate"
14+
#' code using a \code{submitButton} to code using an \code{actionButton}).
15+
#'
16+
#' In essence, the presence of a submit button stops all inputs from
17+
#' sending their values automatically to the server. This means, for
18+
#' instance, that if there are \emph{two} submit buttons in the same app,
19+
#' clicking either one will cause all inputs in the app to send their
20+
#' values to the server. This is probably not what you'd want, which is
21+
#' why submit button are unwieldy for all but the simplest apps. There
22+
#' are other problems with submit buttons: for example, dynamically
23+
#' created submit buttons (for example, with \code{\link{renderUI}}
24+
#' or \code{\link{insertUI}}) will not work.
625
#'
726
#' @param text Button caption
827
#' @param icon Optional \code{\link{icon}} to appear on the button
@@ -13,8 +32,26 @@
1332
#' @family input elements
1433
#'
1534
#' @examples
16-
#' submitButton("Update View")
17-
#' submitButton("Update View", icon("refresh"))
35+
#' if (interactive()) {
36+
#'
37+
#' shinyApp(
38+
#' ui = basicPage(
39+
#' numericInput("num", label = "Make changes", value = 1),
40+
#' submitButton("Update View", icon("refresh")),
41+
#' helpText("When you click the button above, you should see",
42+
#' "the output below update to reflect the value you",
43+
#' "entered at the top:"),
44+
#' verbatimTextOutput("value")
45+
#' ),
46+
#' server = function(input, output) {
47+
#'
48+
#' # submit buttons do not have a value of their own,
49+
#' # they control when the app accesses values of other widgets.
50+
#' # input$num is the value of the number widget.
51+
#' output$value <- renderPrint({ input$num })
52+
#' }
53+
#' )
54+
#' }
1855
#' @export
1956
submitButton <- function(text = "Apply Changes", icon = NULL, width = NULL) {
2057
div(

inst/examples/07_widgets/Readme.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
This example demonstrates some additional widgets included in Shiny, such as `helpText` and `submitButton`. The latter is used to delay rendering output until the user explicitly requests it.
1+
This example demonstrates some additional widgets included in Shiny, such as `helpText` and `actionButton`. The latter is used to delay rendering output until the user explicitly requests it (a construct which also introduces two important server functions, `eventReactive` and `isolate`).

inst/examples/07_widgets/server.R

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
11
library(shiny)
22
library(datasets)
33

4-
# Define server logic required to summarize and view the
4+
# Define server logic required to summarize and view the
55
# selected dataset
66
function(input, output) {
7-
8-
# Return the requested dataset
9-
datasetInput <- reactive({
7+
8+
# Return the requested dataset. Note that we use `eventReactive()`
9+
# here, which takes a dependency on input$update (the action
10+
# button), so that the output is only updated when the user
11+
# clicks the button.
12+
datasetInput <- eventReactive(input$update, {
1013
switch(input$dataset,
1114
"rock" = rock,
1215
"pressure" = pressure,
1316
"cars" = cars)
14-
})
15-
17+
}, ignoreNULL = FALSE)
18+
1619
# Generate a summary of the dataset
1720
output$summary <- renderPrint({
1821
dataset <- datasetInput()
1922
summary(dataset)
2023
})
21-
22-
# Show the first "n" observations
24+
25+
# Show the first "n" observations. The use of `isolate()` here
26+
# is necessary because we don't want the table to update
27+
# whenever input$obs changes (only when the user clicks the
28+
# action button).
2329
output$view <- renderTable({
24-
head(datasetInput(), n = input$obs)
30+
head(datasetInput(), n = isolate(input$obs))
2531
})
2632
}

inst/examples/07_widgets/ui.R

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,40 +2,40 @@ library(shiny)
22

33
# Define UI for dataset viewer application
44
fluidPage(
5-
5+
66
# Application title.
77
titlePanel("More Widgets"),
8-
8+
99
# Sidebar with controls to select a dataset and specify the
1010
# number of observations to view. The helpText function is
1111
# also used to include clarifying text. Most notably, the
12-
# inclusion of a submitButton defers the rendering of output
12+
# inclusion of an actionButton defers the rendering of output
1313
# until the user explicitly clicks the button (rather than
1414
# doing it immediately when inputs change). This is useful if
1515
# the computations required to render output are inordinately
1616
# time-consuming.
1717
sidebarLayout(
1818
sidebarPanel(
19-
selectInput("dataset", "Choose a dataset:",
19+
selectInput("dataset", "Choose a dataset:",
2020
choices = c("rock", "pressure", "cars")),
21-
21+
2222
numericInput("obs", "Number of observations to view:", 10),
23-
23+
2424
helpText("Note: while the data view will show only the specified",
2525
"number of observations, the summary will still be based",
2626
"on the full dataset."),
27-
28-
submitButton("Update View")
27+
28+
actionButton("update", "Update View")
2929
),
30-
30+
3131
# Show a summary of the dataset and an HTML table with the
3232
# requested number of observations. Note the use of the h4
3333
# function to provide an additional header above each output
3434
# section.
3535
mainPanel(
3636
h4("Summary"),
3737
verbatimTextOutput("summary"),
38-
38+
3939
h4("Observations"),
4040
tableOutput("view")
4141
)

man/submitButton.Rd

Lines changed: 41 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)