LoslegenKostenlos loslegen

Select a continent to view

When exploring a dataset, it is often useful to experiment with filtering more than one variable. For example, you might be interested in only seeing data for African countries that had a specific life expectancy.

Diese Übung ist Teil des Kurses

Case Studies: Building Web Applications with Shiny in R

Kurs anzeigen

Anleitung zur Übung

Add a select input that allows the user to select a specific continent to view. Specifically:

  • Add a select input to the UI with ID "continent" and a label of "Continent".
  • Inside the render function, use the continent input value to only select data from the chosen continent (line 21).

Interaktive Übung

Versuche dich an dieser Übung, indem du diesen Beispielcode vervollständigst.

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)
Code bearbeiten und ausführen