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

การเพิ่ม placeholder สำหรับผลลัพธ์

ผลลัพธ์ (output) คือออบเจ็กต์ที่สร้างขึ้นใน R และแสดงให้ผู้ใช้เห็น เช่น กราฟหรือตาราง

ในการเพิ่ม output ลงใน Shiny app สิ่งแรกที่ต้องทำคือเพิ่ม placeholder เพื่อบอก Shiny ว่าจะวาง output ไว้ที่ใด

Shiny มีฟังก์ชัน placeholder สำหรับ output หลายประเภท ตัวอย่างเช่น plotOutput() ใช้สำหรับแสดงกราฟ, tableOutput() ใช้สำหรับแสดงตาราง และ textOutput() ใช้สำหรับแสดงข้อความแบบไดนามิก

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

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

ดูคอร์ส

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

  • สร้าง text input ที่มี ID ว่า "name" ใน sidebar panel
  • เพิ่ม output placeholder 3 รายการใน main panel ดังนี้:
    • text output ที่มี ID ว่า "greeting" (บรรทัดที่ 14)
    • plot output ที่มี ID ว่า "cars_plot" (บรรทัดที่ 16)
    • table output ที่มี ID ว่า "iris_table" (บรรทัดที่ 18)

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

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

library(shiny)

# Define UI for the application
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      # Create a text input with an ID of "name"
      ___(___, "What is your name?", "Dean"),
      numericInput("num", "Number of flowers to show data for",
                   10, 1, nrow(iris))
    ),
    mainPanel(
      # Add a placeholder for a text output with ID "greeting"
      textOutput(outputId = ___),
      # Add a placeholder for a plot with ID "cars_plot"
      ___("cars_plot"),
      # Add a placeholder for a table with ID "iris_table"
      ___(___)
    )
  )
)

# Define the server logic
server <- function(input, output) {}

# Run the application
shinyApp(ui = ui, server = server)
แก้ไขและรันโค้ด