出力の種類

shinydashboard で作るダッシュボード

Png Kee Seng

Researcher

Output と render 関数

  • shinyApp には出力があります
  • 各出力には関数の組が必要です

    • 1つ目は UI の関数
    • 出力の配置場所を決めます
    • 典型的な形:
      <type>output(<identifying label>, ...)
      
  • 2つ目は server 内で定義します

    • 形は次のとおり:
      output$<identifying label> <- render<type>(...)
      
shinydashboard で作るダッシュボード

例: 睡眠調査

  • まずデータを読み込みます
    library(tidyverse)
    sleep <- read_csv("../data/Time Americans Spend Sleeping.csv")  
    
  • 列は次のとおりです
    $ index                   : num [1:945] 0 1 2 3 4  ...
    $ Year                    : num [1:945] 2003 2004 2005 2006 2007 ...
    $ Period                  : chr [1:945] "Annual" "Annual" "Annual" "Annual" ...
    $ Avg hrs per day sleeping: num [1:945] 8.57 8.55 8.62 8.63  ...
    $ Standard Error          : num [1:945] 0.018 0.026 0.023 0.024  ...
    $ Type of Days            : chr [1:945] "All days" "All days" "All days" ...
    $ Age Group               : chr [1:945] "15 years and over" "15 years and over" ...
    $ Activity                : chr [1:945] "Sleeping" "Sleeping" "Sleeping" ...
    $ Sex                     : chr [1:945] "Both" "Both" "Both" ...
    
shinydashboard で作るダッシュボード

textOutput

  • まず UI に textOutput を置きます
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  textOutput("textlabel"))

server <- function(input, output) {
}

shinyApp(ui, server)
shinydashboard で作るダッシュボード

renderText

  • テキスト出力は server に renderText を置いて描画します
  • output$ 記法を使います
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  textOutput("textlabel"))

server <- function(input, output) { output$textlabel <- renderText("Sleep deprivation is a serious health issue in many major cities around the world.") }
shinyApp(ui, server)
shinydashboard で作るダッシュボード

textOutput の描画結果

入力との連動なしのテキスト出力デモ。

shinydashboard で作るダッシュボード

相互作用する textOutput

  • 入出力を連動させるには、input$ で入力を参照します
input$<type>
shinydashboard で作るダッシュボード

相互作用する textOutput

  • 例として、テキスト入力を使います。
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

textInput("textlabel", "Tell me your name", value = "", placeholder = "Don"),
textOutput("textoutlabel")) server <- function(input, output) { output$textoutlabel <- renderText(paste0("Hello ",
input$textlabel,
"Sleep deprivation is a serious health issue in many major cities around the world.")) } shinyApp(ui, server)
shinydashboard で作るダッシュボード

相互作用する textOutput

相互作用するテキスト出力。

shinydashboard で作るダッシュボード

plotOutput

  • UI に plotOutput() を配置します
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

plotOutput("plotlabel"))
server <- function(input, output) { } shinyApp(ui, server)
shinydashboard で作るダッシュボード

renderPlot

  • server() 内に renderPlot() を置きます
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  plotOutput("plotlabel"))

server <- function(input, output) {

output$plotlabel <- renderPlot({ ggplot(sleep) + geom_histogram(aes(x = `Avg hrs per day sleeping`)) })
} shinyApp(ui, server)
shinydashboard で作るダッシュボード

plotOutput の描画結果

plotOutput のデモ。

shinydashboard で作るダッシュボード

selectInput と plotOutput

  • ユーザー入力で結果を切り替えられます
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  selectInput("selectlabel", 
              "Select an option",
              choices = c("Histogram", "Boxplot")),
  plotOutput("plotlabel"))
shinydashboard で作るダッシュボード

selectInput と plotOutput

server <- function(input, output) {
  output$plotlabel <- renderPlot({
    if (input$selectlabel == "Histogram"){

ggplot(sleep) + geom_histogram(aes(x = `Avg hrs per day sleeping`))
} else if (input$selectlabel == "Boxplot"){
ggplot(sleep) + geom_boxplot(aes(x = `Avg hrs per day sleeping`))
} }) } shinyApp(ui, server)
shinydashboard で作るダッシュボード

selectInput と plotOutput

selectInput で切り替える plotOutput のデモ

shinydashboard で作るダッシュボード

sliderInput と plotOutput

  • 別の例として sliderInput を使います
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 

sliderInput("sliderlabel", "Average hours of sleep", min = 7.5, max = 11, value = c(7.5, 11), step = 0.02),
plotOutput("plotlabel")) server <- function(input, output) {
output$plotlabel <- renderPlot({ filter(sleep, `Avg hrs per day sleeping` >= input$sliderlabel[1], `Avg hrs per day sleeping` <= input$sliderlabel[2]) %>% ggplot() + geom_boxplot(aes(x=`Avg hrs per day sleeping`)) })
} shinyApp(ui, server)
shinydashboard で作るダッシュボード

sliderInput と plotOutput

sliderInput と plotOutput のデモ

shinydashboard で作るダッシュボード

shiny のアイコン

  • shiny には、アプリで使えるアイコンもあります

shiny のアイコン例

1 https://fontawesome.com/icons
shinydashboard で作るダッシュボード

shiny のアイコン: 例

  • icon("...") を使います
  • 先ほどの例を思い出してください
  • 例: icon("bed")
ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  sliderInput("sliderlabel",

list(icon("bed"),
"Select an option:"), min = 7.5, max = 11, value = c(7.5, 11), step = 0.02), plotOutput("plotlabel")) server <- function(input, output) { output$plotlabel <- renderPlot({ filter(sleep, `Avg hrs per day sleeping` >= input$sliderlabel[1], `Avg hrs per day sleeping` <= input$sliderlabel[2]) %>% ggplot() + geom_boxplot(aes(x=`Avg hrs per day sleeping`)) }) } shinyApp(ui, server)
shinydashboard で作るダッシュボード

アイコン付きアプリの描画結果

アイコンのデモ

shinydashboard で作るダッシュボード

Passons à la pratique !

shinydashboard で作るダッシュボード

Preparing Video For Download...