開始使用免費開始

選擇要檢視的洲別

在探索資料集時,同時嘗試篩選多個變數通常很有幫助。例如,你可能只想查看非洲國家,且符合特定預期壽命的資料。

本練習屬於課程

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

檢視課程

練習說明

加入一個下拉式選單,讓使用者可以選擇特定的洲別來檢視。具體來說:

  • 在 UI 中加入一個 ID 為「continent」、標籤為「Continent」的下拉式選單。
  • 在 render 函式裡,使用該洲別輸入值,只選取所選洲別的資料(第 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)
編輯並執行程式碼