สำรวจชุดข้อมูลด้วย Shiny

กรณีศึกษา: การสร้างเว็บแอปพลิเคชันด้วย Shiny ใน R

Dean Attali

Shiny Consultant

สำรวจชุดข้อมูลด้วย Shiny

ชุดข้อมูล

+ สภาพแวดล้อมเชิงโต้ตอบ

+ ดูข้อมูล

+ กรองข้อมูล

+ ดาวน์โหลดข้อมูล

= Shiny app

กรณีศึกษา: การสร้างเว็บแอปพลิเคชันด้วย Shiny ใน R

แสดงข้อมูลในรูปตาราง

country continent year lifeExp pop gdpPercap
Afghanistan Asia 1952 28.801 8425333 779.445315
Afghanistan Asia 1957 30.332 9240934 820.85303
Afghanistan Asia 1962 31.997 10267083 853.10071
Afghanistan Asia 1967 34.02 11537966 836.197138
Afghanistan Asia 1972 36.088 13079460 739.981106
Afghanistan Asia 1977 38.438 14880372 786.11336
Afghanistan Asia 1982 39.854 12881816 978.011439
Afghanistan Asia 1987 40.822 13867957 852.39595
Afghanistan Asia 1992 41.674 16317921 649.34140
Afghanistan Asia 1997 41.763 22227415 635.34135
กรณีศึกษา: การสร้างเว็บแอปพลิเคชันด้วย Shiny ใน R

ตารางใน Shiny

  • ตารางคือ output

    • Output ใช้ฟังก์ชัน placeholder ใน UI:
    tableOutput("my_table")
    
    • Output ใช้ฟังก์ชัน render ใน server:
    output$my_table <- renderTable({
        gapminder
    })
    
กรณีศึกษา: การสร้างเว็บแอปพลิเคชันด้วย Shiny ใน R

การกรองข้อมูลในตาราง

  • ใช้ input เพื่อกรองข้อมูลได้

    • เพิ่ม input ใน UI:
    selectInput("country", "Country",
                choices = levels(gapminder$country))
    
    • กรองข้อมูลด้วย input:
  output$my_table <- renderTable({
      subset(gapminder, country == input$country)
  })
กรณีศึกษา: การสร้างเว็บแอปพลิเคชันด้วย Shiny ใน R

ตัวเลือกของ select input

  • อาร์กิวเมนต์ choices ของ selectInput() รับค่าเป็น list ของสตริงใดก็ได้

    • choices สามารถเป็นส่วนหนึ่งของตัวแปร
    selectInput("country", "Country",
                choices = levels(gapminder$country)[1:10])
    
    • choices สามารถเพิ่มค่าใหม่ได้
    selectInput("country", "Country",
                choices = c("any", levels(gapminder$country)))
    
กรณีศึกษา: การสร้างเว็บแอปพลิเคชันด้วย Shiny ใน R

มาฝึกกันเถอะ!

กรณีศึกษา: การสร้างเว็บแอปพลิเคชันด้วย Shiny ใน R

Preparing Video For Download...