响应式入门

使用 R 构建 Shiny Web 应用

Ramnath Vaidyanathan

VP of Product Research

一个应用:用户输入姓名后,会得到个性化问候

使用 R 构建 Shiny Web 应用

响应源

通过浏览器界面传入的用户输入,通常为

ui <- fluidPage(
  titlePanel('Greeting'),

textInput('name', 'Enter Name')
) server <- function(input, output, session){ } shinyApp(ui = ui, server = server)
使用 R 构建 Shiny Web 应用

响应端点

通常显示在浏览器窗口中的输出, 如图表或数据表

ui <- fluidPage(
  titlePanel('Greeting'),
  textInput('name', 'Enter Name'),
  textOutput('greeting')
)

server <- function(input, output, session){
output$greeting <- renderText({ paste("Hello", input$name) })
}
使用 R 构建 Shiny Web 应用

响应导体

依赖响应源,和/或更新响应端点的中间体。

server <- function(input, output, session){
  output$plot_trendy_names <- plotly::renderPlotly({
    babynames %>% 
      filter(name == input$name) %>%
      ggplot(val_bnames, aes(x = year, y = n)) +
      geom_col() 
  })
  output$table_trendy_names <- DT::renderDT({
     babynames %>% 
      filter(name == input$name)
  })
}
使用 R 构建 Shiny Web 应用

响应表达式

响应表达式是惰性的,并带缓存。

server <- function(input, output, session){
  rval_babynames <- reactive({
    babynames %>% 
      filter(name == input$name)
  })
  output$plot_trendy_names <- plotly::renderPlotly({
     rval_babynames() %>%
      ggplot(val_bnames, aes(x = year, y = n)) +
      geom_col() 
  })
  output$table_trendy_names <- DT::renderDT({
     rval_babynames()
  })
}
使用 R 构建 Shiny Web 应用

¡Vamos a practicar!

使用 R 构建 Shiny Web 应用

Preparing Video For Download...