1. Basic information
1.1 Shiny layout
布局主要分成三部分,如下图:
library(shiny)
# Define UI ----
ui <- fluidPage(
titlePanel("title panel"),
sidebarLayout(
sidebarPanel("sidebar panel"),
mainPanel("main panel")
)
)
# Define server logic ----
server <- function(input, output) {
}
# Run the app ----
shinyApp(ui = ui, server = server)
1.2 Formatted Text
文本的格式
1.3 Control Widgets
一些控件
1.4 Output
一些输出
2. Examples
shiny 包里面自带很多案例,可以通过代码调用:
library(shiny) runExample("01_hello")
2.1 01_hello
可以改变直方图的bins
library(shiny)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----App名称
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----侧边栏布局
sidebarLayout(
# Sidebar panel for inputs ----输入侧边栏布局
sidebarPanel(
# Input: Slider for the number of bins ----sliderInput是一个滑动控件
sliderInput(inputId = "bins",#给输入起的名称
label = "Number of bins:",#输入显示的名称
min = 1,
max = 50,
value = 30)#value表示默认值
),
# Main panel for displaying outputs ----输出侧边栏布局
mainPanel(
# Output: Histogram ----直方图
plotOutput(outputId = "distPlot")#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)#划分bins
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")#绘制直方图
})
}
2.2 02_text
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是一个选择控件
selectInput(inputId = "dataset",#名称
label = "Choose a dataset:",#显示的名称
choices = c("rock", "pressure", "cars")),#三个选项
# Input: Numeric entry for number of obs to view ----numericInput是一个输入数字的空间
numericInput(inputId = "obs",
label = "Number of observations to view:",
value = 10)#value是默认值
),
# 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 ----根据所输入的不同的数据集输出不同的summary
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
# Show the first "n" observations ----根据所输入的不同的数据集输出不同的table
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
}
# Create Shiny app ----
shinyApp(ui = ui, server = server)
2.3 03_reactivity
相比02只是多了一个caption的输入和输出
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表示三级标题,根据不同的文本输入输出
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)