金子邦彦研究室情報工学全般データシステム演習(R システム,Shiny を使用)(全3回)

データシステム演習(R システム,Shiny を使用)(全3回)

「Shiny によるデータシステム演習」では,Web 情報システムに関する次のことを演習形式で学ぶ.

1. Shiny のギャラリー

YouTube 動画: https://www.youtube.com/watch?v=POOORv2BFCM 資料: Shiny のギャラリー [PDF], [パワーポイント]

URL: https://shiny.rstudio.com/gallery/

Shiny の機能

2. Shiny の仕組み

YouTube 動画: https://www.youtube.com/watch?v=-Qb2LNNovZY 資料: Shiny の仕組み [PDF], [パワーポイント]

Shiny の機能

R システムの標準オブジェクト

shiny のインストール

install.packages("shiny") 

shiny を動かしてみる

  1. 次の 2つのファイルを作成.

    ファイル名はこの通りにすること. 2つのファイルは、同じディレクトリ(フォルダ) に置くこと

    ui.R

    library(shiny)
    
    shinyUI(fluidPage(
      sidebarLayout(
          sidebarPanel(
                sliderInput("breaks", 
                            "please select a number:",
                            min = 1, 
                            max = 50, 
                            value = 30)
        ),
        
        mainPanel(
          plotOutput("distPlot")
        )
      )
    ))
    

    server.R

    library(shiny)
    
    shinyServer(function(input, output) {
      output$distPlot <- renderPlot({
        hist(faithful[,2], breaks = input$breaks)
      })
    })
    
  2. ui.R, server.R のディレクトリ名(フォルダ名)を確認
  3. R システムのコンソールで,次のように操作 「C:/Users/user」の部分は、 実際に ui.R, server.R があるディレクトリに読み替える
    library(shiny)
    runApp("C:/Users/user")
    

3. Shiny のウィジェット

YouTube 動画: https://www.youtube.com/watch?v=qA-BVSfzbYQ 資料: Shiny のウィジェット [PDF], [パワーポイント]

library(shiny)

shinyUI(fluidPage(
  sidebarLayout(
      sidebarPanel(
            sliderInput("breaks", 
                        "please select a number:",
                        min = 1, 
                        max = 50, 
                        value = 30)
    ),
    
    mainPanel(
      textOutput("distPrint")
    )
  )
))
library(shiny)

shinyServer(function(input, output) {
  output$distPrint <- renderText({
    input$breaks * 12
  })
})
library(shiny)

shinyUI(fluidPage(
  sidebarLayout(
          sidebarPanel (
            numericInput("breaks",
                        "value = ?", value=0)

    ),
    
    mainPanel(
      textOutput("distPrint")
    )
  )
))