为输出添加占位符
输出是指需要展示给用户、并由 R 生成的任何对象,例如图表或表格。
在 Shiny 应用中添加输出,首先需要添加一个输出占位符,用来告诉 Shiny 将输出放在"哪里"。
Shiny 为不同类型的输出提供了多种占位符函数。例如,plotOutput() 用于显示图表,tableOutput() 用于显示表格,而 textOutput() 用于动态文本。
本练习是课程的一部分
案例研究:使用 R 的 Shiny 构建 Web 应用
练习说明
- 在侧边栏面板中创建一个 ID 为 "name" 的文本输入。
- 在主面板中添加 3 个输出占位符:
- 一个 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)