mirror of
https://github.com/rstudio/shiny.git
synced 2026-02-05 20:25:13 -05:00
70 lines
1.6 KiB
Markdown
70 lines
1.6 KiB
Markdown
|
|

|
|
|
|
To run the example type:
|
|
|
|
<pre><code class="console">> library(shiny)
|
|
> runExample("02_text")
|
|
</code></pre>
|
|
|
|
#### 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>
|
|
|
|
#### 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() {
|
|
obs <- as.integer(input$obs)
|
|
head(datasetInput(), n = obs)
|
|
})
|
|
})
|
|
</code></pre>
|
|
|
|
|