# Chapter 9 - Step 1: one input, one output
library(shiny)
library(bslib)
library(dplyr)
library(ggplot2)
library(gapminder)

ui <- page_sidebar(
  title = "Gapminder explorer",
  sidebar = sidebar(
    sliderInput(
      inputId = "year", label = "Year",
      min = 1952, max = 2007, value = 2007, step = 5, sep = ""
    )
  ),
  plotOutput(outputId = "scatter")
)

server <- function(input, output, session) {
  output$scatter <- renderPlot({
    gapminder |>
      filter(year == input$year) |>
      ggplot(aes(x = gdpPercap, y = lifeExp, colour = continent, size = pop)) +
      geom_point(alpha = 0.6) +
      scale_x_log10(labels = scales::label_dollar(accuracy = 1)) +
      scale_size_area(max_size = 15, guide = "none") +
      labs(
        x = "GDP per capita (log scale)", y = "Life expectancy (years)",
        colour = "Continent", title = paste("Year:", input$year)
      ) +
      theme_minimal(base_size = 16)
  })
}

shinyApp(ui, server)
