mirror of
https://github.com/rstudio/shiny.git
synced 2026-01-29 16:58:11 -05:00
79 lines
3.3 KiB
Markdown
79 lines
3.3 KiB
Markdown
|
|

|
|
|
|
The Shiny Text application demonstrates printing R objects directly as well as displaying data-frames using HTML tables. To run the example type:
|
|
|
|
<pre><code class="console">> library(shiny)
|
|
> runExample("02_text")
|
|
</code></pre>
|
|
|
|
The first example had a single numeric input specified using a slider and a single plot output. This example has a bit more going on: two inputs and two types of textual output.
|
|
|
|
If you try changing the number of observations to another value you'll see a demonstration of one of the most important attributes of Shiny applications -- inputs and outputs are connected together "live" and changes are propagated immediately (like a spreadsheet). In this case rather than the entire page being reloaded just the table view is updated when the number of observations change.
|
|
|
|
Here is the user interface definition for the application. Notice in particular that the `sidebarPanel` and `mainPanel` functions now take two arguments (corresponding to the two inputs and two outputs displayed):
|
|
|
|
#### ui.R
|
|
|
|
<pre><code class="r">library(shiny)
|
|
|
|
# Define UI for dataset viewer application
|
|
shinyUI(pageWithSidebar(
|
|
|
|
# Application title
|
|
headerPanel("Shiny Text"),
|
|
|
|
# Sidebar with controls to select a dataset and specify the number
|
|
# of observations to view
|
|
sidebarPanel(
|
|
selectInput("dataset", "Choose a dataset:",
|
|
choices = c("rock", "pressure", "cars")),
|
|
|
|
numericInput("obs", "Number of observations to view:", 10)
|
|
),
|
|
|
|
# Show a summary of the dataset and an HTML table with the requested
|
|
# number of observations
|
|
mainPanel(
|
|
verbatimTextOutput("summary"),
|
|
|
|
tableOutput("view")
|
|
)
|
|
))
|
|
</code></pre>
|
|
|
|
The server side of the application has also gotten a bit more complicated. Now rather than just computing a single output we see the definition of a reactive function to return the dataset corresponding to the user choice and two other reactive functions (`reactivePrint` and `reactiveTable`) that return the output$summary and output$view values.
|
|
|
|
These reactive functions work similarly to the `reactivePlot` function used in the first example: by declaring a reactive function you tell Shiny that it should only be executed when it's dependencies change. In this case that's either one of the user input values (input$dataset or input$n)
|
|
|
|
#### server.R
|
|
|
|
<pre><code class="r">library(shiny)
|
|
library(datasets)
|
|
|
|
# Define server logic required to summarize and view the selected dataset
|
|
shinyServer(function(input, output) {
|
|
|
|
# Return the requested dataset
|
|
datasetInput <- reactive(function() {
|
|
switch(input$dataset,
|
|
"rock" = rock,
|
|
"pressure" = pressure,
|
|
"cars" = cars)
|
|
})
|
|
|
|
# Generate a summary of the dataset
|
|
output$summary <- reactivePrint(function() {
|
|
dataset <- datasetInput()
|
|
summary(dataset)
|
|
})
|
|
|
|
# Show the first "n" observations
|
|
output$view <- reactiveTable(function() {
|
|
head(datasetInput(), n = input$obs)
|
|
})
|
|
})
|
|
</code></pre>
|
|
|
|
We're introduced more use of reactive functions but haven't really explained how they work yet. The next example will start with this one as a baseline and expand significantly on how reactive functions work in Shiny.
|