เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

เลือกทวีปที่ต้องการดู

เมื่อสำรวจชุดข้อมูล การทดลองกรองข้อมูลมากกว่าหนึ่งตัวแปรมักมีประโยชน์มาก ตัวอย่างเช่น อาจต้องการดูเฉพาะข้อมูลของประเทศในแอฟริกาที่มีค่าอายุขัยเฉลี่ยในช่วงที่กำหนด

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

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

ดูคอร์ส

คำแนะนำการฝึกหัด

เพิ่ม select input เพื่อให้ผู้ใช้เลือกทวีปที่ต้องการดูได้ โดยมีรายละเอียดดังนี้

  • เพิ่ม select input ใน UI โดยกำหนด ID เป็น "continent" และ label เป็น "Continent"
  • ภายใน render function ให้ใช้ค่า input ของทวีปเพื่อกรองเฉพาะข้อมูลจากทวีปที่เลือก (บรรทัดที่ 21)

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

ui <- fluidPage(
  h1("Gapminder"),
  sliderInput(inputId = "life", label = "Life expectancy",
              min = 0, max = 120,
              value = c(30, 50)),
  # Add a continent selector dropdown
  ___(___, ___, choices = levels(gapminder$continent)),
  tableOutput("table")
)

server <- function(input, output) {
  output$table <- renderTable({
    data <- gapminder
    data <- subset(
      data,
      lifeExp >= input$life[1] & lifeExp <= input$life[2]
    )
    data <- subset(
      data,
      # Filter the data according to the continent input value
      continent == ___
    )
    data
  })
}

shinyApp(ui, server)
แก้ไขและรันโค้ด