Interactive Explorer

This page contains interactive content that may take a moment to load, please be patient. If you refresh this page, it will have to load again.

Use the controls in the sidebar to explore relationships between penguin measurements. Select variables for the X and Y axes, and filter by species.

#| '!! shinylive warning !!': |
#|   shinylive does not work in self-contained HTML documents.
#|   Please set `embed-resources: false` in your metadata.
#| standalone: true
#| viewerHeight: 800

library(shiny)
library(bslib)
library(echarts4r)

# Load data from CSV (palmerpenguins package not available in shinylive)
penguins_data <- read.csv(
  "https://raw.githubusercontent.com/allisonhorst/palmerpenguins/main/inst/extdata/penguins.csv",
  stringsAsFactors = TRUE
)
penguins_data <- penguins_data[complete.cases(penguins_data), ]

# Variable choices
var_choices <- c(
  "Bill Length (mm)"    = "bill_length_mm",
  "Bill Depth (mm)"    = "bill_depth_mm",
  "Flipper Length (mm)" = "flipper_length_mm",
  "Body Mass (g)"      = "body_mass_g"
)

species_colors <- c("#ff6b35", "#6c5ce7", "#00b894")

ui <- page_sidebar(
  title = "Penguin Explorer",
  sidebar = sidebar(
    selectInput("x_var", "X-Axis Variable",
      choices = var_choices, selected = "bill_length_mm"),
    selectInput("y_var", "Y-Axis Variable",
      choices = var_choices, selected = "body_mass_g"),
    checkboxGroupInput("species", "Species",
      choices = c("Adelie", "Chinstrap", "Gentoo"),
      selected = c("Adelie", "Chinstrap", "Gentoo")),
    hr(),
    h6("Summary Statistics"),
    tableOutput("summary_table")
  ),
  card(
    card_header("Scatter Plot"),
    echarts4rOutput("scatter", height = "500px")
  )
)

server <- function(input, output, session) {
  filtered_data <- reactive({
    penguins_data[penguins_data$species %in% input$species, ]
  })

  output$scatter <- renderEcharts4r({
    req(nrow(filtered_data()) > 0)

    x_label <- names(var_choices)[var_choices == input$x_var]
    y_label <- names(var_choices)[var_choices == input$y_var]

    filtered_data() |>
      group_by(species) |>
      e_charts_(input$x_var) |>
      e_scatter_(input$y_var, symbol_size = 8) |>
      e_tooltip(trigger = "item") |>
      e_x_axis(name = x_label, scale = TRUE) |>
      e_y_axis(name = y_label, scale = TRUE) |>
      e_legend(
        show = TRUE, orient = "horizontal",
        left = "center", bottom = 0
      ) |>
      e_grid(bottom = 80) |>
      e_color(species_colors) |>
      e_color(background = "transparent") |>
      e_toolbox_feature(feature = "dataZoom") |>
      e_toolbox_feature(feature = "saveAsImage")
  })

  output$summary_table <- renderTable({
    req(nrow(filtered_data()) > 0)
    data <- filtered_data()
    data.frame(
      Species = unique(data$species),
      Count = tapply(data$species, data$species, length)[unique(data$species)],
      row.names = NULL
    )
  })
}

shinyApp(ui, server)