CommencerCommencer gratuitement

Change the point size: numeric input

Numeric inputs have a few more arguments that text inputs do not have, such as min and max, which define the minimum and maximum numbers that can be chosen.

Note that when the value of an input is accessed in the server code, Shiny is smart enough to know what type of input was used, and therefore what type of object it should return. This means that if you have a numeric input with ID "foo", then input$foo will return a numeric value.

Cet exercice fait partie du cours

Case Studies: Building Web Applications with Shiny in R

Afficher le cours

Instructions

The code for the Shiny app from the last exercise is provided. Your task is to add a numeric input that the user can use to change the size of the points on the plot. Specifically:

  • Add a numeric input to the UI with ID "size", a label of "Point size", a default value of 1, and a minimum value of 1.
  • Add code to the server so that the numeric input will determine the point size in the plot (line 20).

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Define UI for the application
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      textInput("title", "Title", "GDP vs life exp"),
      # Add a size numeric input
      ___
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

# Define the server logic
server <- function(input, output) {
  output$plot <- renderPlot({
    ggplot(gapminder, aes(gdpPercap, lifeExp)) +
      # Use the size input as the plot point size
      geom_point(size = ___) +
      scale_x_log10() +
      ggtitle(input$title)
  })
}

# Run the application
shinyApp(ui = ui, server = server)
Modifier et exécuter le code