リアクティビティ入門

Rで作るShiny Webアプリケーション

Ramnath Vaidyanathan

VP of Product Research

ユーザーが名前を入力すると、個別の挨拶が表示されるアプリ

Rで作るShiny Webアプリケーション

リアクティブソース

通常はブラウザのUIから受け取るユーザー入力

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

textInput('name', 'Enter Name')
) server <- function(input, output, session){ } shinyApp(ui = ui, server = server)
Rで作るShiny Webアプリケーション

リアクティブエンドポイント

ブラウザに表示される出力(例:プロットや表) `r

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


----CODE_GLUE----
```r
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アプリケーション

練習してみましょう!

Rで作るShiny Webアプリケーション

Preparing Video For Download...