Skip to content

Commit bc7d701

Browse files
committed
Make examples runnable with shinyApp()
1 parent 5d6d75b commit bc7d701

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+1244
-438
lines changed

R/bootstrap-layout.R

Lines changed: 109 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@
3131
#' @seealso \code{\link{column}}, \code{\link{sidebarLayout}}
3232
#'
3333
#' @examples
34-
#' fluidPage(
34+
#' ## Only run examples in interactive R sessions
35+
#' if (interactive()) {
36+
#'
37+
#' # Example of UI with fluidPage
38+
#' ui <- fluidPage(
3539
#'
3640
#' # Application title
3741
#' titlePanel("Hello Shiny!"),
@@ -54,7 +58,19 @@
5458
#' )
5559
#' )
5660
#'
57-
#' fluidPage(
61+
#' # Server logic
62+
#' server <- function(input, output) {
63+
#' output$distPlot <- renderPlot({
64+
#' hist(rnorm(input$obs))
65+
#' })
66+
#' }
67+
#'
68+
#' # Complete app with UI and server components
69+
#' shinyApp(ui, server)
70+
#'
71+
#'
72+
#' # UI demonstrating column layouts
73+
#' ui <- fluidPage(
5874
#' title = "Hello Shiny!",
5975
#' fluidRow(
6076
#' column(width = 4,
@@ -66,6 +82,8 @@
6682
#' )
6783
#' )
6884
#'
85+
#' shinyApp(ui, server = function(input, output) { })
86+
#' }
6987
#' @rdname fluidPage
7088
#' @export
7189
fluidPage <- function(..., title = NULL, responsive = NULL, theme = NULL) {
@@ -115,7 +133,10 @@ fluidRow <- function(...) {
115133
#' @seealso \code{\link{column}}
116134
#'
117135
#' @examples
118-
#' fixedPage(
136+
#' ## Only run examples in interactive R sessions
137+
#' if (interactive()) {
138+
#'
139+
#' ui <- fixedPage(
119140
#' title = "Hello, Shiny!",
120141
#' fixedRow(
121142
#' column(width = 4,
@@ -127,6 +148,9 @@ fluidRow <- function(...) {
127148
#' )
128149
#' )
129150
#'
151+
#' shinyApp(ui, server = function(input, output) { })
152+
#' }
153+
#'
130154
#' @rdname fixedPage
131155
#' @export
132156
fixedPage <- function(..., title = NULL, responsive = NULL, theme = NULL) {
@@ -160,24 +184,43 @@ fixedRow <- function(...) {
160184
#' @seealso \code{\link{fluidRow}}, \code{\link{fixedRow}}.
161185
#'
162186
#' @examples
163-
#' fluidRow(
164-
#' column(4,
165-
#' sliderInput("obs", "Number of observations:",
166-
#' min = 1, max = 1000, value = 500)
167-
#' ),
168-
#' column(8,
169-
#' plotOutput("distPlot")
187+
#' ## Only run examples in interactive R sessions
188+
#' if (interactive()) {
189+
#'
190+
#' ui <- fluidPage(
191+
#' fluidRow(
192+
#' column(4,
193+
#' sliderInput("obs", "Number of observations:",
194+
#' min = 1, max = 1000, value = 500)
195+
#' ),
196+
#' column(8,
197+
#' plotOutput("distPlot")
198+
#' )
170199
#' )
171200
#' )
172201
#'
173-
#' fluidRow(
174-
#' column(width = 4,
175-
#' "4"
176-
#' ),
177-
#' column(width = 3, offset = 2,
178-
#' "3 offset 2"
202+
#' server <- function(input, output) {
203+
#' output$distPlot <- renderPlot({
204+
#' hist(rnorm(input$obs))
205+
#' })
206+
#' }
207+
#'
208+
#' shinyApp(ui, server)
209+
#'
210+
#'
211+
#'
212+
#' ui <- fluidPage(
213+
#' fluidRow(
214+
#' column(width = 4,
215+
#' "4"
216+
#' ),
217+
#' column(width = 3, offset = 2,
218+
#' "3 offset 2"
219+
#' )
179220
#' )
180221
#' )
222+
#' shinyApp(ui, server = function(input, output) { })
223+
#' }
181224
#' @export
182225
column <- function(width, ..., offset = 0) {
183226

@@ -202,8 +245,14 @@ column <- function(width, ..., offset = 0) {
202245
#'
203246
#'
204247
#' @examples
205-
#' titlePanel("Hello Shiny!")
248+
#' ## Only run examples in interactive R sessions
249+
#' if (interactive()) {
206250
#'
251+
#' ui <- fluidPage(
252+
#' titlePanel("Hello Shiny!")
253+
#' )
254+
#' shinyApp(ui, server = function(input, output) { })
255+
#' }
207256
#' @export
208257
titlePanel <- function(title, windowTitle=title) {
209258
tagList(
@@ -226,8 +275,11 @@ titlePanel <- function(title, windowTitle=title) {
226275
#' layout.
227276
#'
228277
#' @examples
278+
#' ## Only run examples in interactive R sessions
279+
#' if (interactive()) {
280+
#'
229281
#' # Define UI
230-
#' fluidPage(
282+
#' ui <- fluidPage(
231283
#'
232284
#' # Application title
233285
#' titlePanel("Hello Shiny!"),
@@ -250,6 +302,16 @@ titlePanel <- function(title, windowTitle=title) {
250302
#' )
251303
#' )
252304
#'
305+
#' # Server logic
306+
#' server <- function(input, output) {
307+
#' output$distPlot <- renderPlot({
308+
#' hist(rnorm(input$obs))
309+
#' })
310+
#' }
311+
#'
312+
#' # Complete app with UI and server components
313+
#' shinyApp(ui, server)
314+
#' }
253315
#' @export
254316
sidebarLayout <- function(sidebarPanel,
255317
mainPanel,
@@ -286,13 +348,18 @@ sidebarLayout <- function(sidebarPanel,
286348
#' @seealso \code{\link{fluidPage}}, \code{\link{flowLayout}}
287349
#'
288350
#' @examples
289-
#' fluidPage(
351+
#' ## Only run examples in interactive R sessions
352+
#' if (interactive()) {
353+
#'
354+
#' ui <- fluidPage(
290355
#' verticalLayout(
291356
#' a(href="http://example.com/link1", "Link One"),
292357
#' a(href="http://example.com/link2", "Link Two"),
293358
#' a(href="http://example.com/link3", "Link Three")
294359
#' )
295360
#' )
361+
#' shinyApp(ui, server = function(input, output) { })
362+
#' }
296363
#' @export
297364
verticalLayout <- function(..., fluid = TRUE) {
298365
lapply(list(...), function(row) {
@@ -319,11 +386,16 @@ verticalLayout <- function(..., fluid = TRUE) {
319386
#' @seealso \code{\link{verticalLayout}}
320387
#'
321388
#' @examples
322-
#' flowLayout(
389+
#' ## Only run examples in interactive R sessions
390+
#' if (interactive()) {
391+
#'
392+
#' ui <- flowLayout(
323393
#' numericInput("rows", "How many rows?", 5),
324394
#' selectInput("letter", "Which letter?", LETTERS),
325395
#' sliderInput("value", "What value?", 0, 100, 50)
326396
#' )
397+
#' shinyApp(ui, server = function(input, output) { })
398+
#' }
327399
#' @export
328400
flowLayout <- function(..., cellArgs = list()) {
329401

@@ -369,28 +441,42 @@ inputPanel <- function(...) {
369441
#' of the layout.
370442
#'
371443
#' @examples
444+
#' ## Only run examples in interactive R sessions
445+
#' if (interactive()) {
446+
#'
447+
#' # Server code used for all examples
448+
#' server <- function(input, output) {
449+
#' output$plot1 <- renderPlot(plot(cars))
450+
#' output$plot2 <- renderPlot(plot(pressure))
451+
#' output$plot3 <- renderPlot(plot(AirPassengers))
452+
#' }
453+
#'
372454
#' # Equal sizing
373-
#' splitLayout(
455+
#' ui <- splitLayout(
374456
#' plotOutput("plot1"),
375457
#' plotOutput("plot2")
376458
#' )
459+
#' shinyApp(ui, server)
377460
#'
378461
#' # Custom widths
379-
#' splitLayout(cellWidths = c("25%", "75%"),
462+
#' ui <- splitLayout(cellWidths = c("25%", "75%"),
380463
#' plotOutput("plot1"),
381464
#' plotOutput("plot2")
382465
#' )
466+
#' shinyApp(ui, server)
383467
#'
384468
#' # All cells at 300 pixels wide, with cell padding
385469
#' # and a border around everything
386-
#' splitLayout(
470+
#' ui <- splitLayout(
387471
#' style = "border: 1px solid silver;",
388472
#' cellWidths = 300,
389473
#' cellArgs = list(style = "padding: 6px"),
390474
#' plotOutput("plot1"),
391475
#' plotOutput("plot2"),
392476
#' plotOutput("plot3")
393477
#' )
478+
#' shinyApp(ui, server)
479+
#' }
394480
#' @export
395481
splitLayout <- function(..., cellWidths = NULL, cellArgs = list()) {
396482

@@ -460,10 +546,7 @@ splitLayout <- function(..., cellWidths = NULL, cellArgs = list()) {
460546
#' not determined by the height of its contents.
461547
#'
462548
#' @examples
463-
#' \donttest{
464549
#' # Only run this example in interactive R sessions.
465-
#' # NOTE: This example should be run with example(fillRow, ask = FALSE) to
466-
#' # avoid being prompted to hit Enter during plot rendering.
467550
#' if (interactive()) {
468551
#'
469552
#' ui <- fillPage(fillRow(
@@ -483,7 +566,6 @@ splitLayout <- function(..., cellWidths = NULL, cellArgs = list()) {
483566
#' shinyApp(ui, server)
484567
#'
485568
#' }
486-
#' }
487569
#' @export
488570
fillRow <- function(..., flex = 1, width = "100%", height = "100%") {
489571
flexfill(..., direction = "row", flex = flex, width = width, height = height)

R/input-action.R

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,29 @@
1111
#'
1212
#' @family input elements
1313
#' @examples
14-
#' \dontrun{
15-
#' # In server.R
16-
#' output$distPlot <- renderPlot({
17-
#' # Take a dependency on input$goButton
18-
#' input$goButton
14+
#' ## Only run examples in interactive R sessions
15+
#' if (interactive()) {
1916
#'
20-
#' # Use isolate() to avoid dependency on input$obs
21-
#' dist <- isolate(rnorm(input$obs))
22-
#' hist(dist)
23-
#' })
17+
#' ui <- fluidPage(
18+
#' sliderInput("obs", "Number of observations", 0, 1000, 500),
19+
#' actionButton("goButton", "Go!"),
20+
#' plotOutput("distPlot")
21+
#' )
22+
#'
23+
#' server <- function(input, output) {
24+
#' output$distPlot <- renderPlot({
25+
#' # Take a dependency on input$goButton. This will run once initially,
26+
#' # because the value changes from NULL to 0.
27+
#' input$goButton
28+
#'
29+
#' # Use isolate() to avoid dependency on input$obs
30+
#' dist <- isolate(rnorm(input$obs))
31+
#' hist(dist)
32+
#' })
33+
#' }
34+
#'
35+
#' shinyApp(ui, server)
2436
#'
25-
#' # In ui.R
26-
#' actionButton("goButton", "Go!")
2737
#' }
2838
#'
2939
#' @seealso \code{\link{observeEvent}} and \code{\link{eventReactive}}

R/input-checkbox.R

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,18 @@
1010
#' @seealso \code{\link{checkboxGroupInput}}, \code{\link{updateCheckboxInput}}
1111
#'
1212
#' @examples
13-
#' checkboxInput("outliers", "Show outliers", FALSE)
13+
#' ## Only run examples in interactive R sessions
14+
#' if (interactive()) {
15+
#'
16+
#' ui <- fluidPage(
17+
#' checkboxInput("somevalue", "Some value", FALSE),
18+
#' verbatimTextOutput("value")
19+
#' )
20+
#' server <- function(input, output) {
21+
#' output$value <- renderText({ input$somevalue })
22+
#' }
23+
#' shinyApp(ui, server)
24+
#' }
1425
#' @export
1526
checkboxInput <- function(inputId, label, value = FALSE, width = NULL) {
1627
inputTag <- tags$input(id = inputId, type="checkbox")

R/input-checkboxgroup.R

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,25 @@
1515
#' @seealso \code{\link{checkboxInput}}, \code{\link{updateCheckboxGroupInput}}
1616
#'
1717
#' @examples
18-
#' checkboxGroupInput("variable", "Variable:",
19-
#' c("Cylinders" = "cyl",
20-
#' "Transmission" = "am",
21-
#' "Gears" = "gear"))
18+
#' ## Only run examples in interactive R sessions
19+
#' if (interactive()) {
2220
#'
21+
#' ui <- fluidPage(
22+
#' checkboxGroupInput("variable", "Variables to show:",
23+
#' c("Cylinders" = "cyl",
24+
#' "Transmission" = "am",
25+
#' "Gears" = "gear")),
26+
#' tableOutput("data")
27+
#' )
28+
#'
29+
#' server <- function(input, output) {
30+
#' output$data <- renderTable({
31+
#' mtcars[, c("mpg", input$variable), drop = FALSE]
32+
#' }, rownames = TRUE)
33+
#' }
34+
#'
35+
#' shinyApp(ui, server)
36+
#' }
2337
#' @export
2438
checkboxGroupInput <- function(inputId, label, choices, selected = NULL,
2539
inline = FALSE, width = NULL) {

0 commit comments

Comments
 (0)