Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- For examples 1-11, use the code and descriptions from shiny package verbatim. - For example 12, use the code in the revised datatables article.
- Loading branch information
1 parent
d5c731b
commit 401b0d3
Showing
46 changed files
with
873 additions
and
698 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,4 +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 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 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,59 @@ | ||
library(shiny) | ||
|
||
# Define UI for app that draws a histogram ---- | ||
ui <- fluidPage( | ||
|
||
# App title ---- | ||
titlePanel("Hello Shiny!"), | ||
|
||
# Sidebar layout with input and output definitions ---- | ||
sidebarLayout( | ||
|
||
# Sidebar panel for inputs ---- | ||
sidebarPanel( | ||
|
||
# Input: Slider for the number of bins ---- | ||
sliderInput(inputId = "bins", | ||
label = "Number of bins:", | ||
min = 1, | ||
max = 50, | ||
value = 30) | ||
|
||
), | ||
|
||
# Main panel for displaying outputs ---- | ||
mainPanel( | ||
|
||
# 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1 +1 @@ | ||
This example demonstrates output of raw text from R using the `renderPrint` function in `server.R` and the `verbatimTextOutput` function in `ui.R`. In this case, a textual summary of the data is shown using R's built-in `summary` function. | ||
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 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,64 @@ | ||
library(shiny) | ||
|
||
# Define UI for dataset viewer app ---- | ||
ui <- fluidPage( | ||
|
||
# App title ---- | ||
titlePanel("Shiny Text"), | ||
|
||
# Sidebar layout with a input and output definitions ---- | ||
sidebarLayout( | ||
|
||
# Sidebar panel for inputs ---- | ||
sidebarPanel( | ||
|
||
# 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) | ||
), | ||
|
||
# Main panel for displaying outputs ---- | ||
mainPanel( | ||
|
||
# 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
This example demonstrates a core feature of Shiny: **reactivity**. In `server.R`, a reactive called `datasetInput` is declared. | ||
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 both the output expression `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 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 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,102 @@ | ||
library(shiny) | ||
|
||
# Define UI for dataset viewer app ---- | ||
ui <- fluidPage( | ||
|
||
# App title ---- | ||
titlePanel("Reactivity"), | ||
|
||
# Sidebar layout with input and output definitions ---- | ||
sidebarLayout( | ||
|
||
# Sidebar panel for inputs ---- | ||
sidebarPanel( | ||
|
||
# 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) | ||
|
||
), | ||
|
||
# Main panel for displaying outputs ---- | ||
mainPanel( | ||
|
||
# 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) |
Oops, something went wrong.