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

|
|
|
|
The Hello Shiny example is a simple application that generates a random distribution with a configurable number of observations and then plots it. To run the example type:
|
|
|
|
<pre><code class="console">> library(shiny)
|
|
> runExample("01_hello")
|
|
</code></pre>
|
|
|
|
#### ui.R
|
|
|
|
<pre><code class="r">library(shiny)
|
|
|
|
# Define UI for application that plots random distributions
|
|
shinyUI(pageWithSidebar(
|
|
|
|
# Application title
|
|
headerPanel("Hello Shiny!"),
|
|
|
|
# Sidebar with a slider input for number of observations
|
|
sidebarPanel(
|
|
sliderInput("obs",
|
|
"Number of observations:",
|
|
min = 0,
|
|
max = 1000,
|
|
value = 500)
|
|
),
|
|
|
|
# Show a plot of the generated distribution
|
|
mainPanel(
|
|
plotOutput("distPlot")
|
|
)
|
|
))
|
|
</code></pre>
|
|
|
|
#### server.R
|
|
|
|
<pre><code class="r">library(shiny)
|
|
|
|
# Define server logic required to generate and plot a random distribution
|
|
shinyServer(function(input, output) {
|
|
|
|
# Function that generates a plot of the distribution. The function
|
|
# is wrapped in a call to reactivePlot to indicate that:
|
|
#
|
|
# 1) It is "reactive" and therefore should be automatically
|
|
# re-executed when inputs change
|
|
# 2) Its output type is a plot
|
|
#
|
|
output$distPlot <- reactivePlot(function() {
|
|
|
|
# convert string input to an integer
|
|
obs <- as.integer(input$obs)
|
|
|
|
# generate an rnorm distribution and plot it
|
|
dist <- rnorm(obs)
|
|
hist(dist)
|
|
})
|
|
})
|
|
</code></pre>
|