slides available at https://statds.github.io/topic-presentation-wenjie_wang/
source code of the slides: https://github.com/statds/topic-presentation-wenjie_wang/
R packages needed for following examples:
By execution order:
global.R
: an optional script for code needed in ui.R
and server.R
ui.R
: define user interface (UI) design
server.R
: define server-side logic
app.R
ui <- fluidPage(
titlePanel("my title panel"),
sidebarLayout(
sidebarPanel("my sidebar panel"),
mainPanel("my main panel")
)
)
Shiny uses Twitter Bootstrap 3, the probably most popular HTML, CSS, and JS framework for developing responsive, mobile first projects on the web.
the essential HTML code defined in ui
:
The more fundamental layout constructor functions are fluidRow()
and column()
.
Shiny | HTML5 | creates |
---|---|---|
p()
|
<p>
|
A paragraph of text |
h1(), …, h6()
|
<h1>, …, <h6>
|
a first, …, sixth level header |
a()
|
<a>
|
A hyper link |
br()
|
<br>
|
A line break |
div()
|
<div>
|
A division with a uniform style |
span()
|
<span>
|
An in-line version of division |
strong()
|
<strong>
|
Bold text |
em()
|
<em>
|
Italicized text |
…
|
…
|
… |
HTML()
|
Directly passes character strings as HTML |
names(tags)
returns a complete valid HTML5 tag list.id
for widget name: users will not see the name, but you can use it to access the widget’s value. The name should be a character string.label
for widget label: this label will appear with the widget in your app. It should be a character string, but it can be an empty string ""
.The standard Shiny widgets include
function | widget |
---|---|
actionButton
|
Action Button |
checkboxGroupInput
|
A group of check boxes |
checkboxInput
|
A single check box |
dateInput
|
A calendar to aid date selection |
dateRangeInput
|
A pair of calendars for selecting a date range |
fileInput
|
A file upload control wizard |
helpText
|
Help text that can be added to an input form |
numericInput
|
A field to enter numbers |
radioButtons
|
A set of radio buttons |
selectInput
|
A box with choices to select from |
sliderInput
|
A slider bar |
submitButton
|
A submit button |
textInput
|
A field to enter text |
*Output
functions in ui
or ui.R
turn R objects into output of UI.
Output function | Creates |
---|---|
htmlOutput
|
raw HTML |
imageOutput
|
image |
plotOutput
|
plot |
tableOutput
|
table |
textOutput
|
text |
uiOutput
|
raw HTML |
verbatimTextOutput
|
text |
*Output
functions take output name/ID as input.render*
functions in server
or server.R
render function | creates |
---|---|
renderImage
|
images (saved as a link to a source file) |
renderPlot
|
plots |
renderPrint
|
any printed output |
renderTable
|
data frame, matrix, other table like structures |
renderText
|
character strings |
renderUI
|
a Shiny tag object or HTML |
render*
functions take a single argument: an R expression surrounded by {}
.render*
functions once each time a user changes the value of a widget.ui <- fluidPage(
titlePanel("a simple example of reactive output"),
sidebarLayout(
sidebarPanel(
selectInput("cyl", "Cylinders",
choices = sort(unique(mtcars$cyl)),
selected = 4)
),
mainPanel(plotOutput("mpg_boxplot"))
)
)
server <- function(input, output) {
## the `renderPlot` runs every time a user changes input$cyl
output$mpg_boxplot <- renderPlot({
## filtering the cars models in the mtcars dataset
## by the input cylinder number
dat <- subset(mtcars, cyl == input$cyl)
## draw a boxplot of mpg for the filtered data
with(dat, boxplot(mpg))
})
}
shinyApp(ui, server)
reactive()
function, which takes an R expression surrounded by {}
similar to render*
functions.server <- function(input, output, session) {
## e.g., a simple reactive expression for filtering the cars models
## in the mtcars dataset by the input cylinder
dataInput <- reactive({
subset(mtcars, cyl == input$cyl)
})
## draw a boxplot of mpg for the filtered data
output$mpg_boxplot <- renderPlot({
dat <- dataInput()
with(dat, boxplot(mpg))
})
}
share as R scripts by runApp()
, runUrl()
, runGitHub()
or runGist()