下拉菜单
您已经看到如何在 shinyApp 中添加下拉菜单。它允许应用用户从一组选项中选择一项,同时只占用很小的界面空间。如果应用里已经有很多功能,这通常有助于做出更好的 UI 设计。
在本练习中,数据已存为 sleep,一个名为 p 的图也已保存。shiny 和 tidyverse 库也已加载。完成代码后,您可以在 HTML Viewer 中打开 shinyApp,以获得更完整的显示效果。
本练习是课程的一部分
使用 shinydashboard 构建仪表板
练习说明
- 放置一个
selectInput(),包含两个选项,名称为 "Plot" 和 "Table"。 - 放置一个
plotOutput()和一个tableOutput(),分别命名为 "plot" 和 "table"。 - 在 server 中添加图形输出。
- 在 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)