--- title: "Dashboard de indicadores mundiales" output: flexdashboard::flex_dashboard: orientation: rows runtime: shiny --- ```{r setup, include=FALSE} library(flexdashboard) library(shiny) library(plotly) library(DT) library(openxlsx) library(tidyverse) owid_country <- read.xlsx("https://ctim.es/AEDV/data/owid_country.xlsx", sheet = 1) |> as_tibble() ``` Sidebar {.sidebar data-width=230} -------------------------------------------------- **Instrucciones:** Seleccione un continente y un indicador para explorar la relación con el PIB per cápita. ```{r} selectInput("continente", "Continente:", choices = c("Todos", unique(owid_country$continent)), selected = "Todos") selectInput("indicador_y", "Indicador eje Y:", choices = c("life_expectancy", "median_age", "human_development_index", "hospital_beds_per_thousand"), selected = "life_expectancy") ``` ```{r} datos <- reactive({ if (input$continente == "Todos") { owid_country } else { owid_country |> filter(continent == input$continente) } }) ``` ```{r} renderTable({ datos() |> select(País = location, Valor = !!sym(input$indicador_y)) |> arrange(desc(Valor)) }, digits = 2) ``` Row {data-height=150} -------------------------------------------------- ### Países ```{r} renderValueBox({ n <- nrow(datos()) valueBox(n, caption = "Países", icon = "fa-globe", color = "primary") }) ``` ### Esperanza de vida media ```{r} renderValueBox({ media <- round(mean(datos()$life_expectancy, na.rm = TRUE), 1) # El color cambia condicionalmente según el valor: semáforo de salud color <- ifelse(media > 75, "success", ifelse(media > 65, "warning", "danger")) valueBox(paste0(media, " años"), caption = "Esperanza de vida", icon = "fa-heartbeat", color = color) }) ``` ### PIB per cápita medio ```{r} renderValueBox({ media <- round(mean(datos()$gdp_per_capit, na.rm = TRUE), 0) valueBox(paste0(media, " $"), caption = "PIB per cápita", icon = "fa-dollar-sign", color = "info") }) ``` Row {.tabset data-height=650} -------------------------------------------------- ### Dispersión ```{r} renderPlotly({ validate(need(nrow(datos()) > 0, "No hay datos para esta selección")) p <- datos() |> ggplot(aes(x = gdp_per_capit, y = .data[[input$indicador_y]], label = location)) + geom_point(color = "steelblue", size = 2) + scale_x_continuous(transform = "log2") + geom_smooth(method = lm, se = FALSE, color = "firebrick") + labs(x = "PIB per cápita (log)", y = input$indicador_y) ggplotly(p) }) ``` ### Tabla ```{r} DT::renderDataTable({ validate(need(nrow(datos()) > 0, "No hay datos")) datos() |> select(location, life_expectancy, gdp_per_capit, !!sym(input$indicador_y)) |> arrange(desc(.data[[input$indicador_y]])) |> DT::datatable(options = list(pageLength = 15, scrollX = TRUE)) |> DT::formatRound(columns = c("life_expectancy", "gdp_per_capit"), digits = 1) }) ```