选择要查看的洲别
在探索数据集时,同时对多个变量进行筛选往往很有帮助。例如,您可能只想查看某一特定预期寿命的非洲国家的数据。
本练习是课程的一部分
案例研究:使用 R 的 Shiny 构建 Web 应用
练习说明
添加一个下拉选择输入,让用户可以选择要查看的特定洲别。具体要求:
- 在 UI 中添加一个 ID 为 "continent"、标签为 "Continent" 的选择输入。
- 在渲染函数中,使用该洲别输入的取值,只选择来自所选洲别的数据(第 21 行)。
交互式实操练习
通过完成这段示例代码来试试这个练习。
ui <- fluidPage(
h1("Gapminder"),
sliderInput(inputId = "life", label = "Life expectancy",
min = 0, max = 120,
value = c(30, 50)),
# Add a continent selector dropdown
___(___, ___, choices = levels(gapminder$continent)),
tableOutput("table")
)
server <- function(input, output) {
output$table <- renderTable({
data <- gapminder
data <- subset(
data,
lifeExp >= input$life[1] & lifeExp <= input$life[2]
)
data <- subset(
data,
# Filter the data according to the continent input value
continent == ___
)
data
})
}
shinyApp(ui, server)