-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Update examples to use bslib #3963
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
f85e8e7
update 01_hello
skaltman 8eee8dd
Updates 06_tabsets example to use bslib
garrettgman 43bf4af
Pre-emptively adds legacy versions of all of the original examples
garrettgman 45f6175
Switches tabset example to use navset_card_underline()
garrettgman 4a1c8de
Updates example 2
garrettgman d02fa93
Updates 03_reactivity
garrettgman 6925463
Updates 04_mpg
garrettgman 06371af
Updates 05_sliders
garrettgman 69540c1
Updates 07_widgets
garrettgman 3e8a12b
Deletes 08_html_legacy. Original example does not need updated.
garrettgman 987be9b
Updates 09_upload
garrettgman 066dca9
Updates 10_download
garrettgman 950efb3
Updates 11_timer
garrettgman 54836f4
Merge branch 'main' into update-examples-bslib
garrettgman 0070fd0
Keep inst/example unchanged; add new examples under inst/shiny and up…
cpsievert 0c1a39d
Update news
cpsievert a78f6f9
Fix some code formatting issues
cpsievert 38cfb7f
Merge branch 'main' into skaltman-update-examples-bslib
cpsievert 81f489a
Update NEWS.md
cpsievert 7db02cb
Don't default to showcase mode
cpsievert File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -467,7 +467,9 @@ runExample <- function(example=NA, | |
launch.browser = getOption('shiny.launch.browser', interactive()), | ||
host=getOption('shiny.host', '127.0.0.1'), | ||
display.mode=c("auto", "normal", "showcase")) { | ||
examplesDir <- system_file('examples', package='shiny') | ||
legacy <- getOption('shiny.legacy.examples', FALSE) | ||
examplesDir <- if (isTRUE(legacy)) 'examples' else 'shiny' | ||
examplesDir <- system_file(examplesDir, package='shiny') | ||
Comment on lines
+470
to
+472
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @karangattu started working on extending That said, while we're here, I think we should do a very light-weight version of that work and add I'll take that in a follow-up PR. |
||
dir <- resolve(examplesDir, example) | ||
if (is.null(dir)) { | ||
if (is.na(example)) { | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Title: Hello Shiny! | ||
Author: RStudio, Inc. | ||
AuthorUrl: http://www.rstudio.com/ | ||
License: MIT | ||
Tags: getting-started | ||
Type: Shiny |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This small Shiny application demonstrates Shiny's automatic UI updates. | ||
|
||
Move the *Number of bins* slider and notice how the `renderPlot` expression is automatically re-evaluated when its dependant, `input$bins`, changes, causing a histogram with a new number of bins to be rendered. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
library(shiny) | ||
library(bslib) | ||
|
||
# Define UI for app that draws a histogram ---- | ||
ui <- page_sidebar( | ||
|
||
cpsievert marked this conversation as resolved.
Show resolved
Hide resolved
|
||
# App title ---- | ||
title = "Hello Shiny!", | ||
|
||
# Sidebar panel for inputs ---- | ||
sidebar = sidebar( | ||
|
||
# Input: Slider for the number of bins ---- | ||
sliderInput( | ||
inputId = "bins", | ||
label = "Number of bins:", | ||
min = 1, | ||
max = 50, | ||
value = 30 | ||
) | ||
|
||
), | ||
|
||
# Output: Histogram ---- | ||
plotOutput(outputId = "distPlot") | ||
) | ||
|
||
# Define server logic required to draw a histogram ---- | ||
server <- function(input, output) { | ||
|
||
# Histogram of the Old Faithful Geyser Data ---- | ||
# with requested number of bins | ||
# This expression that generates a histogram is wrapped in a call | ||
# to renderPlot to indicate that: | ||
# | ||
# 1. It is "reactive" and therefore should be automatically | ||
# re-executed when inputs (input$bins) change | ||
# 2. Its output type is a plot | ||
output$distPlot <- renderPlot({ | ||
|
||
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") | ||
|
||
}) | ||
|
||
} | ||
|
||
# Create Shiny app ---- | ||
shinyApp(ui = ui, server = server) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Title: Shiny Text | ||
Author: RStudio, Inc. | ||
AuthorUrl: http://www.rstudio.com/ | ||
License: MIT | ||
Tags: getting-started | ||
Type: Shiny |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This example demonstrates output of raw text from R using the `renderPrint` function in `server` and the `verbatimTextOutput` function in `ui`. In this case, a textual summary of the data is shown using R's built-in `summary` function. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
library(shiny) | ||
library(bslib) | ||
|
||
# Define UI for dataset viewer app ---- | ||
ui <- page_sidebar( | ||
|
||
# App title ---- | ||
title = "Shiny Text", | ||
|
||
# Sidebar panel for inputs ---- | ||
sidebar = sidebar( | ||
|
||
# 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: Verbatim text for data summary ---- | ||
verbatimTextOutput("summary"), | ||
|
||
# Output: HTML table with requested number of observations ---- | ||
tableOutput("view") | ||
) | ||
|
||
# Define server logic to summarize and view selected dataset ---- | ||
server <- 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) | ||
}) | ||
|
||
} | ||
|
||
# Create Shiny app ---- | ||
shinyApp(ui = ui, server = server) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Title: Reactivity | ||
Author: RStudio, Inc. | ||
AuthorUrl: http://www.rstudio.com/ | ||
License: MIT | ||
Tags: getting-started | ||
Type: Shiny |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
This example demonstrates a core feature of Shiny: **reactivity**. In the `server` function, a reactive called `datasetInput` is declared. | ||
|
||
Notice that the reactive expression depends on the input expression `input$dataset`, and that it's used by two output expressions: `output$summary` and `output$view`. Try changing the dataset (using *Choose a dataset*) while looking at the reactive and then at the outputs; you will see first the reactive and then its dependencies flash. | ||
|
||
Notice also that the reactive expression doesn't just update whenever anything changes--only the inputs it depends on will trigger an update. Change the "Caption" field and notice how only the `output$caption` expression is re-evaluated; the reactive and its dependents are left alone. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
library(shiny) | ||
library(bslib) | ||
|
||
# Define UI for dataset viewer app ---- | ||
ui <- page_sidebar( | ||
|
||
# App title ---- | ||
title = "Reactivity", | ||
|
||
# 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) | ||
), | ||
|
||
# Output: Formatted text for caption ---- | ||
h3(textOutput("caption", container = span)), | ||
|
||
# Output: Verbatim text for data summary ---- | ||
verbatimTextOutput("summary"), | ||
|
||
# Output: HTML table with requested number of observations ---- | ||
tableOutput("view") | ||
) | ||
|
||
# Define server logic to summarize and view selected dataset ---- | ||
server <- function(input, output) { | ||
|
||
# Return the requested dataset ---- | ||
# By declaring datasetInput as a reactive expression we ensure | ||
# that: | ||
# | ||
# 1. It is only called when the inputs it depends on changes | ||
# 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) | ||
}) | ||
|
||
# Create caption ---- | ||
# The output$caption is computed based on a reactive expression | ||
# that returns input$caption. When the user changes the | ||
# "caption" field: | ||
# | ||
# 1. This function is automatically called to recompute the output | ||
# 2. New caption is pushed back to the browser for re-display | ||
# | ||
# Note that because the data-oriented reactive expressions | ||
# below don't depend on input$caption, those expressions are | ||
# NOT called when input$caption changes | ||
output$caption <- renderText({ | ||
input$caption | ||
}) | ||
|
||
# Generate a summary of the dataset ---- | ||
# The output$summary depends on the datasetInput reactive | ||
# expression, so will be re-executed whenever datasetInput is | ||
# invalidated, i.e. whenever the input$dataset changes | ||
output$summary <- renderPrint({ | ||
dataset <- datasetInput() | ||
summary(dataset) | ||
}) | ||
|
||
# Show the first "n" observations ---- | ||
# The output$view depends on both the databaseInput reactive | ||
# expression and input$obs, so it will be re-executed whenever | ||
# input$dataset or input$obs is changed | ||
output$view <- renderTable({ | ||
head(datasetInput(), n = input$obs) | ||
}) | ||
|
||
} | ||
|
||
# Create Shiny app ---- | ||
shinyApp(ui, server) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Title: Miles Per Gallon | ||
Author: RStudio, Inc. | ||
AuthorUrl: http://www.rstudio.com/ | ||
License: MIT | ||
Tags: getting-started | ||
Type: Shiny |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
This example demonstrates the following concepts: | ||
|
||
- **Global variables**: The `mpgData` variable is declared outside of the `ui` and `server` function definitions. This makes it available anywhere inside `app.R`. The code in `app.R` outside of `ui` and `server` function definitions is only run once when the app starts up, so it can't contain user input. | ||
- **Reactive expressions**: `formulaText` is a reactive expression. Note how it re-evaluates when the Variable field is changed, but not when the Show Outliers box is unchecked. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
library(shiny) | ||
library(bslib) | ||
library(datasets) | ||
|
||
# Data pre-processing ---- | ||
# Tweak the "am" variable to have nicer factor labels -- since this | ||
# doesn't rely on any user inputs, we can do this once at startup | ||
# and then use the value throughout the lifetime of the app | ||
mpgData <- mtcars | ||
mpgData$am <- factor(mpgData$am, labels = c("Automatic", "Manual")) | ||
|
||
|
||
# Define UI for miles per gallon app ---- | ||
ui <- page_sidebar( | ||
|
||
# App title ---- | ||
title = "Miles Per Gallon", | ||
|
||
# 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) | ||
|
||
), | ||
|
||
# Output: Formatted text for caption ---- | ||
h3(textOutput("caption")), | ||
|
||
# Output: Plot of the requested variable against mpg ---- | ||
plotOutput("mpgPlot") | ||
) | ||
|
||
# Define server logic to plot various variables against mpg ---- | ||
server <- function(input, output) { | ||
|
||
# Compute the formula text ---- | ||
# This is in a reactive expression since it is shared by the | ||
# output$caption and output$mpgPlot functions | ||
formulaText <- reactive({ | ||
paste("mpg ~", input$variable) | ||
}) | ||
|
||
# Return the formula text for printing as a caption ---- | ||
output$caption <- renderText({ | ||
formulaText() | ||
}) | ||
|
||
# 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) | ||
}) | ||
|
||
} | ||
|
||
# Create Shiny app ---- | ||
shinyApp(ui, server) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
Title: Sliders | ||
Author: RStudio, Inc. | ||
AuthorUrl: http://www.rstudio.com/ | ||
License: MIT | ||
Tags: getting-started | ||
Type: Shiny |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
This example demonstrates Shiny's versatile `sliderInput` widget. | ||
|
||
Slider inputs can be used to select single values, to select a continuous range of values, and even to animate over a range. |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.