大綱

  1. shiny能幫助企業(或自己)做什麼?

  2. shiny簡介

  3. 第一個shiny範例(“01_hello”)

  4. 結論

1.shiny能幫助企業(或自己)做什麼?

  • 建立企業級商業智慧,營運智慧與儀表板應用

  • 營運資料互動式分析與視覺化應用(產銷人發財)

  • 辦公室自動化分析應用

flowchart LR
  A(匯入) --> B(複製)
  B --> C(貼上)
  C --> D{篩選}
  D --> E[樞紐分析]
  D --> F[合併彙算]
  E --> G[繪圖]
  F --> G[繪圖]
  G --> H(匯出)

範例1-CWB 1,600萬筆互動分析平台

範例2-汽車產險預測模型

範例3-空間圓餅圖離群值分析

範例4-2020新型冠狀病毒視覺化

http://rwepa.blogspot.com/2020/02/2019nCoV.html

範例5-品質管制圖應用

http://rwepa.blogspot.com/2021/10/r-shiny-quality-control-chart.html

範例6-離子資料分析與視覺化應用

範例7-顧客連接分析-demo

http://rwepa.blogspot.com/2021/10/r-shiny-quality-control-chart.html

範例8-Taiwan Stock App-demo

https://rwepa.shinyapps.io/shinyStockVis/

2.shiny簡介

  • 2.1 shiny套件
  • 2.2 shiny架構
  • 2.3 shiny特性
  • 2.4 shiny安裝
  • 2.5 shiny使用

2.1 shiny套件

  • Google -> r cran shiny
  • Shiny, https://cran.r-project.org/web/packages/shiny/
  • shiny: Web Application Framework for R.
    • Makes it incredibly easy to build interactive web applications with R.

    • Automatic “reactive” binding between inputs and outputs and extensive prebuilt widgets.

    • Make it possible to build beautiful, responsive, and powerful applications with minimal effort.

2.2 shiny架構

2.3 shiny特性

  • 輸入-包括文字方塊,下拉式選單,按鈕等控制項
  • 處理-執行商業邏輯運算與運用機器學習建立預測模型(forecasting models)
  • 輸出-網頁,報表,檔案中呈現文字摘要,表格,靜態與動態圖表

2.4 shiny安裝

install.packages("shiny")

2.5 shiny使用

  • 方法1: app.R (簡易架構,適用於小型方案)

  • 方法2: ui.R + server.R (完整架構,適用於企業應用)

3.第一個shiny範例(“01_hello”)

# 顯示內建11個範例
dir(paste0(.libPaths(), "/shiny/examples"))
 [1] "01_hello"      "02_text"       "03_reactivity" "04_mpg"       
 [5] "05_sliders"    "06_tabsets"    "07_widgets"    "08_html"      
 [9] "09_upload"     "10_download"   "11_timer"     

runExample(“01_hello”)

  • 執行內建範例的方式: runExample(…)
library(shiny)
runExample("01_hello")

ui.R

# 載入套件
library(shiny)

# 第1部分 Define UI for app that draws a histogram ----
ui <- fluidPage(

  # 1.1 App title ----
  titlePanel("Hello Shiny!"),

  # 1.2 Sidebar layout with input and output definitions ----
  sidebarLayout(

    # 1.2.1 Sidebar panel for inputs ----
    sidebarPanel(

      # Input: Slider for the number of bins ----
      sliderInput(inputId = "bins",
                  label = "Number of bins:",
                  min = 1,
                  max = 50,
                  value = 30)

    ),

    # 1.2.2 Main panel for displaying outputs ----
    mainPanel(

      # Output: Histogram ----
      plotOutput(outputId = "distPlot")

    )
  )
)

server.R

# 第2部分 Define server logic required to draw a histogram ----
server <- function(input, output) {

  # Histogram of the Old Faithful Geyser Data ----
  # with requested number of bins
  # This expression that generates a histogram is wrapped in a call
  # to renderPlot to indicate that:
  #
  # 1. It is "reactive" and therefore should be automatically
  #    re-executed when inputs (input$bins) change
  # 2. Its output type is a plot
  output$distPlot <- renderPlot({

    x    <- faithful$waiting
    bins <- seq(min(x), max(x), length.out = input$bins + 1)

    hist(x, breaks = bins, col = "#75AADB", border = "white",
         xlab = "Waiting time to next eruption (in mins)",
         main = "Histogram of waiting times")

    })

}

shiny執行

  • 設定 ui
  • 設定 server
shinyApp(ui = ui, server = server)

shiny-四大區塊

  1. library()
  2. ui()
  3. server()
  4. shinyApp()

4.結論

  • shiny能幫助企業(或自己)做什麼 --> 互動式預測模型(Interactive Predictive Model)

  • 理解 shiny 套件的運作方法

  • “01_hello”

  • 四大區塊: library, ui, server, runApp

參考資料

  1. RWPEA | R - shiny企業實務應用, https://github.com/rwepa/teaching-r-shiny
  2. RWEPA, http://rwepa.blogspot.com/
  3. Shiny, https://shiny.rstudio.com/
  4. Posit Videos, https://www.rstudio.com/resources/webinars/