Shiny 是RStudio公司開發(fā)的R包居砖,利用Shiny 可以輕松構(gòu)建交互式Web應(yīng)用程序(App)沿猜。
#安裝Shiny
> install.packages("shiny")
Shiny包中內(nèi)置了11個例子來展示Shiny的使用澳厢。
#Example 1: Hello Shiny
Hello Shiny 使用faithful數(shù)據(jù)畫了一個直方圖夯到,這個直方圖可以調(diào)節(jié)bin的個數(shù)房铭。
Hello Shiny運行
> library(shiny)
> runExample("01_hello")
Shiny App的架構(gòu)
Shiny App 本質(zhì)上基于一個R腳本(app.R).
app.R 有三個組成成分:
- 一個用戶界面
- 一個服務(wù)器功能
- 調(diào)用
shinyApp
函數(shù)
用戶界面(ui
)包含了App的的布局和外觀掺出。
服務(wù)器功能(server)包含了電腦構(gòu)建app需要的指令。
shinyApp 函數(shù)根據(jù)UI/server創(chuàng)建Shiny app惭等。
注:版本0.10.2以前珍手,Shiny 不支持一個文件構(gòu)建App,ui
和server
各自需要一個單獨的文件:i.R
and server.R
辞做。
#Hello Shiny
##用戶界面:ui
library(shiny)
# Define UI for app that draws a histogram ----
ui <- fluidPage(
# App title ----
titlePanel("Hello Shiny!"),
# Sidebar layout with input and output definitions ----
sidebarLayout(
# Sidebar panel for inputs ----
sidebarPanel(
# Input: Slider for the number of bins ----
sliderInput(inputId = "bins",
label = "Number of bins:",
min = 1,
max = 50,
value = 30)
),
# Main panel for displaying outputs ----
mainPanel(
# Output: Histogram ----
plotOutput(outputId = "distPlot")
)
)
)
##服務(wù)器功能:server
# 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)
hist(x, breaks = bins, col = "#75AADB", border = "white",
xlab = "Waiting time to next eruption (in mins)",
main = "Histogram of waiting times")
})
}
##shiny App 運行
shinyApp函數(shù)根據(jù)上面構(gòu)建的UI/server創(chuàng)建Shiny app
shinyApp(ui, server)
#Example 2: Shiny Text
library(shiny)
runExample("02_text")
##ui
# 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(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: Verbatim text for data summary ----
verbatimTextOutput("summary"),
# Output: HTML table with requested number of observations ----
tableOutput("view")
)
)
)
##server
# 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 ----
output$summary <- renderPrint({
dataset <- datasetInput()
summary(dataset)
})
# Show the first "n" observations ----
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
}
#Example 3: Reactivity
[站外圖片上傳中...(image-930865-1560176024515)]
library(shiny)
runExample("03_reactivity")
- 響應(yīng)式編程
響應(yīng)式編程是一種面向數(shù)據(jù)流和變化傳播的編程范式琳要。
input values => R code => output values
input values變化時,R code會自動執(zhí)行秤茅,同時更新結(jié)果
使用reactive()可以創(chuàng)建響應(yīng)式表達式
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
改變input
或者input$obs
, 下面的表達式都會重新運行稚补。
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
##ui
# 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(textOutput("caption", container = span)),
# Output: Verbatim text for data summary ----
verbatimTextOutput("summary"),
# Output: HTML table with requested number of observations ----
tableOutput("view")
)
)
)
##server
# 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)
})
}
#原文:
The basic parts of a Shiny app
系列文章
R shiny教程-1:一個 Shiny app的基本組成部分
R shiny教程-2:布局用戶界面
Shiny Server安裝