观察器与反应式

使用 R 构建 Shiny Web 应用

Ramnath Vaidyanathan

VP of Product Research

反应式流程

显示 Shiny 应用中反应式流程的示意图

使用 R 构建 Shiny Web 应用

观察器(1/2)

一个应用:用户输入姓名后,在模态对话框中收到个性化问候

使用 R 构建 Shiny Web 应用

观察器(2/2)

ui <- fluidPage(
  textInput('name', 'Enter your name')
)

server <- function(input, output, session){
  observe({
    showNotification(
      paste("You entered the name", input$name)
    )
  })
}
使用 R 构建 Shiny Web 应用

观察器与反应式

作用

  • reactive() 用于计算值,无副作用。
  • observe() 用于执行动作,有副作用。

差异

  • 返回值:反应式表达式返回值;观察器不返回。
  • 求值:观察器对依赖变更立即响应;反应式为惰性。
  • 副作用:观察器主要用于其副作用;反应式不得有副作用。
使用 R 构建 Shiny Web 应用

Passons à la pratique !

使用 R 构建 Shiny Web 应用

Preparing Video For Download...