開始使用免費開始

加入輸出的占位元

輸出是任何要顯示給使用者、並由 R 產生的物件,例如圖形或表格。

若要在 Shiny 應用程式中加入輸出,第一步是加入輸出的占位元,用來告訴 Shiny 要把輸出放在「哪裡」。

Shiny 提供多種輸出占位元函式,每種輸出型態各有對應。例如,plotOutput() 用於顯示圖形,tableOutput() 用於輸出表格,而 textOutput() 用於動態文字。

本練習屬於課程

案例研究:使用 R 的 Shiny 建置網頁應用程式

檢視課程

練習說明

  • 在側邊欄面板建立一個 ID 為「name」的文字輸入。
  • 在主面板加入三個輸出占位元:
    • 一個 ID 為「greeting」的文字輸出(第 14 行)。
    • 一個 ID 為「cars_plot」的圖形輸出(第 16 行)。
    • 一個 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)
編輯並執行程式碼