diff --git a/inst/examples-shiny/01_hello/app.R b/inst/examples-shiny/01_hello/app.R index 4765171f6e..4db836e01f 100644 --- a/inst/examples-shiny/01_hello/app.R +++ b/inst/examples-shiny/01_hello/app.R @@ -18,7 +18,6 @@ ui <- page_sidebar( max = 50, value = 30 ) - ), # Output: Histogram ---- @@ -37,16 +36,18 @@ server <- function(input, output) { # re-executed when inputs (input$bins) change # 2. Its output type is a plot output$distPlot <- renderPlot({ - - x <- faithful$waiting + x <- faithful$waiting bins <- seq(min(x), max(x), length.out = input$bins + 1) - hist(x, breaks = bins, col = "#75AADB", border = "white", - xlab = "Waiting time to next eruption (in mins)", - main = "Histogram of waiting times") - - }) - + hist( + x, + breaks = bins, + col = "#75AADB", + border = "white", + xlab = "Waiting time to next eruption (in mins)", + main = "Histogram of waiting times" + ) + }) } # Create Shiny app ---- diff --git a/inst/examples-shiny/02_text/app.R b/inst/examples-shiny/02_text/app.R index d69b092e2d..69135fd950 100644 --- a/inst/examples-shiny/02_text/app.R +++ b/inst/examples-shiny/02_text/app.R @@ -23,7 +23,6 @@ ui <- page_sidebar( label = "Number of observations to view:", value = 10 ) - ), # Output: Verbatim text for data summary ---- @@ -38,10 +37,12 @@ server <- function(input, output) { # Return the requested dataset ---- datasetInput <- reactive({ - switch(input$dataset, - "rock" = rock, - "pressure" = pressure, - "cars" = cars) + switch( + input$dataset, + "rock" = rock, + "pressure" = pressure, + "cars" = cars + ) }) # Generate a summary of the dataset ---- @@ -54,7 +55,6 @@ server <- function(input, output) { output$view <- renderTable({ head(datasetInput(), n = input$obs) }) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/03_reactivity/app.R b/inst/examples-shiny/03_reactivity/app.R index 789c0ddba5..e8d69c5dad 100644 --- a/inst/examples-shiny/03_reactivity/app.R +++ b/inst/examples-shiny/03_reactivity/app.R @@ -10,22 +10,28 @@ ui <- page_sidebar( # Sidebar panel for inputs ---- sidebar = sidebar( - # Input: Text for providing a caption ---- - # Note: Changes made to the caption in the textInput control - # are updated in the output area immediately as you type - textInput(inputId = "caption", - label = "Caption:", - value = "Data Summary"), - - # Input: Selector for choosing dataset ---- - selectInput(inputId = "dataset", - label = "Choose a dataset:", - choices = c("rock", "pressure", "cars")), - - # Input: Numeric entry for number of obs to view ---- - numericInput(inputId = "obs", - label = "Number of observations to view:", - value = 10) + # Input: Text for providing a caption ---- + # Note: Changes made to the caption in the textInput control + # are updated in the output area immediately as you type + textInput( + inputId = "caption", + label = "Caption:", + value = "Data Summary" + ), + + # Input: Selector for choosing dataset ---- + selectInput( + inputId = "dataset", + label = "Choose a dataset:", + choices = c("rock", "pressure", "cars") + ), + + # Input: Numeric entry for number of obs to view ---- + numericInput( + inputId = "obs", + label = "Number of observations to view:", + value = 10 + ) ), # Output: Formatted text for caption ---- @@ -49,10 +55,12 @@ server <- function(input, output) { # 2. The computation and result are shared by all the callers, # i.e. it only executes a single time datasetInput <- reactive({ - switch(input$dataset, - "rock" = rock, - "pressure" = pressure, - "cars" = cars) + switch( + input$dataset, + "rock" = rock, + "pressure" = pressure, + "cars" = cars + ) }) # Create caption ---- @@ -86,7 +94,6 @@ server <- function(input, output) { output$view <- renderTable({ head(datasetInput(), n = input$obs) }) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/04_mpg/app.R b/inst/examples-shiny/04_mpg/app.R index 6b86ddacbd..377d11fdc9 100644 --- a/inst/examples-shiny/04_mpg/app.R +++ b/inst/examples-shiny/04_mpg/app.R @@ -19,15 +19,19 @@ ui <- page_sidebar( # Sidebar panel for inputs ---- sidebar = sidebar( - # Input: Selector for variable to plot against mpg ---- - selectInput("variable", "Variable:", - c("Cylinders" = "cyl", - "Transmission" = "am", - "Gears" = "gear")), - - # Input: Checkbox for whether outliers should be included ---- - checkboxInput("outliers", "Show outliers", TRUE) - + # Input: Selector for variable to plot against mpg ---- + selectInput( + "variable", + "Variable:", + c( + "Cylinders" = "cyl", + "Transmission" = "am", + "Gears" = "gear" + ) + ), + + # Input: Checkbox for whether outliers should be included ---- + checkboxInput("outliers", "Show outliers", TRUE) ), # Output: Formatted text for caption ---- @@ -55,12 +59,14 @@ server <- function(input, output) { # Generate a plot of the requested variable against mpg ---- # and only exclude outliers if requested output$mpgPlot <- renderPlot({ - boxplot(as.formula(formulaText()), - data = mpgData, - outline = input$outliers, - col = "#75AADB", pch = 19) + boxplot( + as.formula(formulaText()), + data = mpgData, + outline = input$outliers, + col = "#75AADB", + pch = 19 + ) }) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/05_sliders/app.R b/inst/examples-shiny/05_sliders/app.R index 14b5d278ac..a58ecd708a 100644 --- a/inst/examples-shiny/05_sliders/app.R +++ b/inst/examples-shiny/05_sliders/app.R @@ -10,36 +10,59 @@ ui <- page_sidebar( # Sidebar panel for inputs ---- sidebar = sidebar( - # Input: Simple integer interval ---- - sliderInput("integer", "Integer:", - min = 0, max = 1000, - value = 500), - - # Input: Decimal interval with step value ---- - sliderInput("decimal", "Decimal:", - min = 0, max = 1, - value = 0.5, step = 0.1), - - # Input: Specification of range within an interval ---- - sliderInput("range", "Range:", - min = 1, max = 1000, - value = c(200,500)), - - # Input: Custom currency format for with basic animation ---- - sliderInput("format", "Custom Format:", - min = 0, max = 10000, - value = 0, step = 2500, - pre = "$", sep = ",", - animate = TRUE), - - # Input: Animation with custom interval (in ms) ---- - # to control speed, plus looping - sliderInput("animation", "Looping Animation:", - min = 1, max = 2000, - value = 1, step = 10, - animate = - animationOptions(interval = 300, loop = TRUE)) - + # Input: Simple integer interval ---- + sliderInput( + "integer", + "Integer:", + min = 0, + max = 1000, + value = 500 + ), + + # Input: Decimal interval with step value ---- + sliderInput( + "decimal", + "Decimal:", + min = 0, + max = 1, + value = 0.5, + step = 0.1 + ), + + # Input: Specification of range within an interval ---- + sliderInput( + "range", + "Range:", + min = 1, + max = 1000, + value = c(200, 500) + ), + + # Input: Custom currency format for with basic animation ---- + sliderInput( + "format", + "Custom Format:", + min = 0, + max = 10000, + value = 0, + step = 2500, + pre = "$", + sep = ",", + animate = TRUE + ), + + # Input: Animation with custom interval (in ms) ---- + # to control speed, plus looping + sliderInput( + "animation", + "Looping Animation:", + min = 1, + max = 2000, + value = 1, + step = 10, + animate = + animationOptions(interval = 300, loop = TRUE) + ) ), # Output: Table summarizing the values entered ---- @@ -51,27 +74,29 @@ server <- function(input, output) { # Reactive expression to create data frame of all input values ---- sliderValues <- reactive({ - data.frame( - Name = c("Integer", - "Decimal", - "Range", - "Custom Format", - "Animation"), - Value = as.character(c(input$integer, - input$decimal, - paste(input$range, collapse = " "), - input$format, - input$animation)), - stringsAsFactors = FALSE) - + Name = c( + "Integer", + "Decimal", + "Range", + "Custom Format", + "Animation" + ), + Value = as.character(c( + input$integer, + input$decimal, + paste(input$range, collapse = " "), + input$format, + input$animation + )), + stringsAsFactors = FALSE + ) }) # Show the values in an HTML table ---- output$values <- renderTable({ sliderValues() }) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/06_tabsets/app.R b/inst/examples-shiny/06_tabsets/app.R index 63e8f9f0e3..b691511daa 100644 --- a/inst/examples-shiny/06_tabsets/app.R +++ b/inst/examples-shiny/06_tabsets/app.R @@ -12,25 +12,31 @@ ui <- page_sidebar( sidebar = sidebar( # Input: Select the random distribution type ---- - radioButtons("dist", "Distribution type:", - c("Normal" = "norm", - "Uniform" = "unif", - "Log-normal" = "lnorm", - "Exponential" = "exp")), + radioButtons( + "dist", + "Distribution type:", + c( + "Normal" = "norm", + "Uniform" = "unif", + "Log-normal" = "lnorm", + "Exponential" = "exp" + ) + ), # br() element to introduce extra vertical spacing ---- br(), # Input: Slider for the number of observations to generate ---- - sliderInput("n", - "Number of observations:", - value = 500, - min = 1, - max = 1000) + sliderInput( + "n", + "Number of observations:", + value = 500, + min = 1, + max = 1000 + ) ), # Main panel for displaying outputs ---- # Output: A tabset that combines three panels ---- navset_card_underline( - # Panel with plot ---- nav_panel("Plot", plotOutput("plot")), @@ -49,12 +55,14 @@ server <- function(input, output) { # This is called whenever the inputs change. The output functions # defined below then use the value computed from this expression d <- reactive({ - dist <- switch(input$dist, - norm = rnorm, - unif = runif, - lnorm = rlnorm, - exp = rexp, - rnorm) + dist <- switch( + input$dist, + norm = rnorm, + unif = runif, + lnorm = rlnorm, + exp = rexp, + rnorm + ) dist(input$n) }) @@ -68,9 +76,12 @@ server <- function(input, output) { dist <- input$dist n <- input$n - hist(d(), - main = paste("r", dist, "(", n, ")", sep = ""), - col = "#75AADB", border = "white") + hist( + d(), + main = paste("r", dist, "(", n, ")", sep = ""), + col = "#75AADB", + border = "white" + ) }) # Generate a summary of the data ---- @@ -82,7 +93,6 @@ server <- function(input, output) { output$table <- renderTable({ d() }) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/07_widgets/app.R b/inst/examples-shiny/07_widgets/app.R index 66d337c3be..e0b1304fd4 100644 --- a/inst/examples-shiny/07_widgets/app.R +++ b/inst/examples-shiny/07_widgets/app.R @@ -10,25 +10,29 @@ ui <- page_sidebar( # Sidebar panel for inputs ---- sidebar = sidebar( - # Input: Select a dataset ---- - selectInput("dataset", "Choose a dataset:", - choices = c("rock", "pressure", "cars")), - - # Input: Specify the number of observations to view ---- - numericInput("obs", "Number of observations to view:", 10), - - # Include clarifying text ---- - helpText("Note: while the data view will show only the specified", - "number of observations, the summary will still be based", - "on the full dataset."), - - # Input: actionButton() to defer 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. - actionButton("update", "Update View") - + # Input: Select a dataset ---- + selectInput( + "dataset", + "Choose a dataset:", + choices = c("rock", "pressure", "cars") + ), + + # Input: Specify the number of observations to view ---- + numericInput("obs", "Number of observations to view:", 10), + + # Include clarifying text ---- + helpText( + "Note: while the data view will show only the specified", + "number of observations, the summary will still be based", + "on the full dataset." + ), + + # Input: actionButton() to defer 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. + actionButton("update", "Update View") ), # Output: Header + summary of distribution ---- @@ -47,12 +51,18 @@ server <- function(input, output) { # Note that we use eventReactive() here, which depends on # input$update (the action button), so that the output is only # updated when the user clicks the button - datasetInput <- eventReactive(input$update, { - switch(input$dataset, - "rock" = rock, - "pressure" = pressure, - "cars" = cars) - }, ignoreNULL = FALSE) + datasetInput <- eventReactive( + input$update, + { + switch( + input$dataset, + "rock" = rock, + "pressure" = pressure, + "cars" = cars + ) + }, + ignoreNULL = FALSE + ) # Generate a summary of the dataset ---- output$summary <- renderPrint({ @@ -67,7 +77,6 @@ server <- function(input, output) { output$view <- renderTable({ head(datasetInput(), n = isolate(input$obs)) }) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/08_html/app.R b/inst/examples-shiny/08_html/app.R index be8d378415..55e5315023 100644 --- a/inst/examples-shiny/08_html/app.R +++ b/inst/examples-shiny/08_html/app.R @@ -7,12 +7,14 @@ server <- function(input, output) { # This is called whenever the inputs change. The output functions # defined below then use the value computed from this expression d <- reactive({ - dist <- switch(input$dist, - norm = rnorm, - unif = runif, - lnorm = rlnorm, - exp = rexp, - rnorm) + dist <- switch( + input$dist, + norm = rnorm, + unif = runif, + lnorm = rlnorm, + exp = rexp, + rnorm + ) dist(input$n) }) @@ -26,9 +28,12 @@ server <- function(input, output) { dist <- input$dist n <- input$n - hist(d(), - main = paste("r", dist, "(", n, ")", sep = ""), - col = "#75AADB", border = "white") + hist( + d(), + main = paste("r", dist, "(", n, ")", sep = ""), + col = "#75AADB", + border = "white" + ) }) # Generate a summary of the data ---- @@ -40,7 +45,6 @@ server <- function(input, output) { output$table <- renderTable({ head(data.frame(x = d())) }) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/09_upload/app.R b/inst/examples-shiny/09_upload/app.R index d4cbfad459..48c67d3dc5 100644 --- a/inst/examples-shiny/09_upload/app.R +++ b/inst/examples-shiny/09_upload/app.R @@ -10,42 +10,61 @@ ui <- page_sidebar( # Sidebar panel for inputs ---- sidebar = sidebar( - # Input: Select a file ---- - fileInput("file1", "Choose CSV File", - multiple = TRUE, - accept = c("text/csv", - "text/comma-separated-values,text/plain", - ".csv")), - - # Horizontal line ---- - tags$hr(), - - # Input: Checkbox if file has header ---- - checkboxInput("header", "Header", TRUE), - - # Input: Select separator ---- - radioButtons("sep", "Separator", - choices = c(Comma = ",", - Semicolon = ";", - Tab = "\t"), - selected = ","), - - # Input: Select quotes ---- - radioButtons("quote", "Quote", - choices = c(None = "", - "Double Quote" = '"', - "Single Quote" = "'"), - selected = '"'), - - # Horizontal line ---- - tags$hr(), - - # Input: Select number of rows to display ---- - radioButtons("disp", "Display", - choices = c(Head = "head", - All = "all"), - selected = "head") - + # Input: Select a file ---- + fileInput( + "file1", + "Choose CSV File", + multiple = TRUE, + accept = c( + "text/csv", + "text/comma-separated-values,text/plain", + ".csv" + ) + ), + + # Horizontal line ---- + tags$hr(), + + # Input: Checkbox if file has header ---- + checkboxInput("header", "Header", TRUE), + + # Input: Select separator ---- + radioButtons( + "sep", + "Separator", + choices = c( + Comma = ",", + Semicolon = ";", + Tab = "\t" + ), + selected = "," + ), + + # Input: Select quotes ---- + radioButtons( + "quote", + "Quote", + choices = c( + None = "", + "Double Quote" = '"', + "Single Quote" = "'" + ), + selected = '"' + ), + + # Horizontal line ---- + tags$hr(), + + # Input: Select number of rows to display ---- + radioButtons( + "disp", + "Display", + choices = c( + Head = "head", + All = "all" + ), + selected = "head" + ) ), # Output: Data file ---- @@ -54,29 +73,26 @@ ui <- page_sidebar( # Define server logic to read selected file ---- server <- function(input, output) { - output$contents <- renderTable({ - # input$file1 will be NULL initially. After the user selects # and uploads a file, head of that data file by default, # or all rows if selected, will be shown. req(input$file1) - df <- read.csv(input$file1$datapath, - header = input$header, - sep = input$sep, - quote = input$quote) + df <- read.csv( + input$file1$datapath, + header = input$header, + sep = input$sep, + quote = input$quote + ) - if(input$disp == "head") { + if (input$disp == "head") { return(head(df)) - } - else { + } else { return(df) } - }) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/10_download/app.R b/inst/examples-shiny/10_download/app.R index 641e16f6a1..2c7b1418aa 100644 --- a/inst/examples-shiny/10_download/app.R +++ b/inst/examples-shiny/10_download/app.R @@ -10,15 +10,16 @@ ui <- page_sidebar( # Sidebar panel for inputs ---- sidebar = sidebar( - # Input: Choose dataset ---- - selectInput("dataset", "Choose a dataset:", - choices = c("rock", "pressure", "cars")), - - # Button - downloadButton("downloadData", "Download") - + # Input: Choose dataset ---- + selectInput( + "dataset", + "Choose a dataset:", + choices = c("rock", "pressure", "cars") + ), + + # Button + downloadButton("downloadData", "Download") ), - tableOutput("table") ) @@ -27,10 +28,12 @@ server <- function(input, output) { # Reactive value for selected dataset ---- datasetInput <- reactive({ - switch(input$dataset, - "rock" = rock, - "pressure" = pressure, - "cars" = cars) + switch( + input$dataset, + "rock" = rock, + "pressure" = pressure, + "cars" = cars + ) }) # Table of selected dataset ---- @@ -47,7 +50,6 @@ server <- function(input, output) { write.csv(datasetInput(), file, row.names = FALSE) } ) - } # Create Shiny app ---- diff --git a/inst/examples-shiny/11_timer/app.R b/inst/examples-shiny/11_timer/app.R index 0ec5f98591..cfb3c0fa0d 100644 --- a/inst/examples-shiny/11_timer/app.R +++ b/inst/examples-shiny/11_timer/app.R @@ -8,12 +8,10 @@ ui <- page_fluid( # Define server logic to show current time, update every second ---- server <- function(input, output, session) { - output$currentTime <- renderText({ invalidateLater(1000, session) paste("The current time is", Sys.time()) }) - } # Create Shiny app ----