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

เมนู Dropdown

คุณได้เห็นแล้วว่าเมนู drop-down สามารถเพิ่มลงใน shinyApp ได้อย่างไร ฟีเจอร์นี้ช่วยให้ผู้ใช้แอปเลือกตัวเลือกจากรายการได้โดยใช้พื้นที่น้อย ซึ่งเหมาะสำหรับการออกแบบ UI ที่มีฟีเจอร์อื่นอยู่มากแล้ว

ในแบบฝึกหัดนี้ ข้อมูลถูกเก็บไว้ในตัวแปร sleep และพล็อตชื่อ p ถูกสร้างไว้แล้ว รวมถึงโหลดไลบรารี shiny และ tidyverse เรียบร้อยแล้ว เมื่อเขียนโค้ดครบแล้ว สามารถเปิด shinyApp ใน HTML Viewer เพื่อดูผลลัพธ์แบบเต็มได้

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

การสร้างแดชบอร์ดด้วย shinydashboard

ดูคอร์ส

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

  • เพิ่ม selectInput() พร้อมตัวเลือก 2 รายการ ได้แก่ "Plot" และ "Table"
  • เพิ่ม plotOutput() และ tableOutput() โดยตั้งชื่อว่า "plot" และ "table"
  • เพิ่ม plot output ลงใน server
  • เพิ่ม table output ลงใน server

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

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

ui <- fluidPage(
  titlePanel("Sleeping habits in America"), 
  # Place a selectInput with two choices, "Plot" and "Table"
  ___("choice", "Choose an output",
              choices = ___),
  # Place plot and table outputs called "plot" and "table"
  ___("plot"), ___)
server <- function(input, output) {
  # Add renderPlot
  output$plot <- ___({
    if(input$choice == "Plot") p
  })
  # Add renderTable
  output$___ <- ___({
    if(input$choice == "Table") sleep
  }) }
shinyApp(ui, server)
แก้ไขและรันโค้ด