Reactivity的示例程序与Hello Text很相似,但是用到了反应式编程里更多细节的概念,要运行该例子,请键入:
runExample("03_reactivity")
- 什么是反应式设计
Shiny的web框架从本质上说是使从页面中获取输入值并传递给R变得更容易,然后把R代码的结果以输出值的形式返回给页面。
input values => R code => output values
因为Shiny程序是交互式的,输入值可以随时改变,输出值也应该立即更新,以反映输入输入值的改变。
Shiny中有反应式编程的库,你可以用它来定义你的应用程序逻辑。使用这个库,改变输入值会自动引发R代码中相应的部分重新执行,反过来会更新输出结果。
反应式编程基础
反应式编程是种编程风格,这种风格以反应值开始,反应值是随时间
变化的值,或者由用户输入的值
,在反应值之上绑定
有反应表达式(reactive expression),反应表达式会接收到反应值并执行其他反应表达式。
反应表达式有趣的地方在于,当它执行的时候,会自动跟踪读取
到的反应值以及调用的其他反应表达式。如果反应表达式所依赖的反应值和反应表达式发生了改变,那么该反应表达式的返回值也应该变化(原文是If those “dependencies” become out of date, then they know that their own return value has also become out of date)。因为有这种跟踪机制,所以改变一个反应值会自动引发依赖于它的反应表达式重新执行。
在shiny中使用反应值时,最常见的方式是使用input对象。input对象会被传递给shinyServer函数中,让你可以用类似列表的语法来访问网页上的输入值。从代码上看,你好像是从列表或者数据框里读取了值,但 " 实际上你读取的是反应值”。你不必写监测输入值变化的代码,只需要写反应表达式来读取所需的反应值,Shiny会处理好什么时候调用它们。
创建反应表达式很简单,只需要把一个正常的表达式传递给reactive函数就行。
在本节的示例程序中,下面这个简单的反应表达式的功能是,基于用户在表单中选择的选项来返回R数据框。
datasetInput <- reactive({
switch(input$dataset,
"rock" = rock,
"pressure" = pressure,
"cars" = cars)
})
为了将反应值转化为可以在网页上呈现的输出,我们要将它们赋值给output对象(同样传递给shinyServer函数)。下面是个赋值给输出值的例子,输出值依赖于我们刚才定义的反应表达式datasetInput,以及input$obs:
output$view <- renderTable({
head(datasetInput(), n = input$obs)
})
不管是 datasetInput 还是input$obs,一旦它们的值发生改变,上面这个表达式将会重新执行(它的输出也会在浏览器里重新渲染)。
回到代码上
现在我们已经对一些核心概念有了更多了解,我们再来看看源代码,并尝试更深层次理解。用户接口的定义中,增加了来用定义说明文字(caption)的文本输入框,尽管它与前一个例子还是很相似。
ui.R
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(textOutput("caption", container = span)),
# Output: Verbatim text for data summary ----
verbatimTextOutput("summary"),
# Output: HTML table with requested number of observations ----
tableOutput("view")
)
)
)
服务端脚本
服务端脚本声明了反应表达式datasetInput和三个反应输出值。下面有每个定义的详细注释描述了在反应式系统中是如何运作的:
server.R
# 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)