# 1.Carga de librerias
library(readr)
library(dplyr)
## Warning: package 'dplyr' was built under R version 4.6.1
## 
## Adjuntando el paquete: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(tidyr)
library(kableExtra)
## 
## Adjuntando el paquete: 'kableExtra'
## The following object is masked from 'package:dplyr':
## 
##     group_rows
load("C:/Users/mendo/OneDrive/Desktop/MAE/comercio exter/data_comercio_exterior (1).RData")
# 2. Tabla con códigos ISO (si ya existen en el data frame)
data_comercio_exterior %>%
  head(10) %>%
  kable(caption = "Datos con códigos ISO") %>%
  kable_styling(bootstrap_options = c("striped", "hover"))
Datos con códigos ISO
pais sac anio mes valor_cif kilogramos_importaciones valor_fob kilogramos_exportaciones
AFGANISTAN 8708990000 2022 2 220.50 0.29 0 0
AFGANISTAN 8471900000 2022 5 665.90 4.00 0 0
AFGANISTAN 5702310000 2022 6 913.74 22.02 0 0
AFGANISTAN 5702502000 2022 6 566.35 11.36 0 0
AFGANISTAN 6111200000 2022 8 213.69 1.56 0 0
AFGANISTAN 8536509000 2022 9 42.28 2.00 0 0
AFGANISTAN 6210400000 2023 7 204.75 0.10 0 0
AFGANISTAN 0901210000 2024 4 0.00 0.00 440 21
AFGANISTAN 4909000000 2024 4 0.07 0.15 0 0
AFGANISTAN 8473300000 2024 6 60.18 1.00 0 0
# Carga de script de funciones
source(
  file = "C:/Users/mendo/OneDrive/Desktop/MAE/funciones_comercio_exterior.R",
  echo = TRUE,
  max.deparse.length = 100,
  encoding = "utf-8"
) #Usar en el Rdata en el "Funciones_comercio_exterior" la ruta de acceso que usaste en save 2 chunks arriba
## 
## > options(scipen = 999)
## 
## > library(dplyr)
## 
## > library(readxl)
## 
## > library(stringr)
## Warning: package 'stringr' was built under R version 4.6.1
## 
## > load("C:/Users/mendo/OneDrive/Desktop/MAE/comercio exter/data_comercio_exterior (1).RData")
## 
## > nombre_archivo <- ("C:/Users/mendo/OneDrive/Desktop/MAE/nombres_iso_paises.xlsx")
## 
## > nombres_iso_paises <- read_excel(nombre_archivo)
## 
## > nombres_iso_paises$nom_pais_esp <- toupper(nombres_iso_paises$nom_pais_esp)
## 
## > data_comercio_exterior <- data_comercio_exterior %>% 
## +     left_join(nombres_iso_paises, by = c(p .... [TRUNCATED] 
## 
## > herramientas_mostrar_paises <- function() {
## +     paises <- nombres_iso_paises %>% select(nom_pais .... [TRUNCATED] 
## 
## > herramientas_mostrar_regiones <- function() {
## +     nombres_iso_paises %>% group_by(region, cod_re .... [TRUNCATED] 
## 
## > herramientas_mostrar_paises_en_tabla <- function() {
## +     data_comercio_exterior %>% group_by(pai .... [TRUNCATED] 
## 
## > indicadores_IVCR_capitulo <- function(codigo_pais, 
## +     capitulo, anio) {
## +     codigo_pais <- e .... [TRUNCATED] 
## 
## > indicadores_IVCR_partida <- function(codigo_pais, 
## +     partida, anio) {
## +     codigo_pais <- enq .... [TRUNCATED] 
## 
## > indicadores_Balassa_capitulo <- function(codigo_pais, 
## +     capitulo, anio, normalizado = TRUE) { .... [TRUNCATED] 
## 
## > indicadores_Balassa_partida <- function(codigo_pais, 
## +     partida, anio, normalizado = TRUE) {
## + .... [TRUNCATED] 
## 
## > indicadores_IHH_Herfindahl_Hirschmann_anual_texto <- function(codigo_pais, 
## +     anio, normalizad .... [TRUNCATED] 
## 
## > indicadores_IHH_Herfindahl_Hirschmann_anual <- function(codigo_pais, 
## +     anio, normalizado = TR .... [TRUNCATED]
#Seleccionar Años
anios_ranking<-2022:2026
data_comercio_exterior %>% 
  filter(anio %in% anios_ranking) ->data_ranking

data_ranking %>% 
  group_by(anio,iso_3) %>% 
  summarise(total=sum(valor_fob)) %>% mutate(percent=round(prop.table(total)*100,2)) %>% 
  slice_max(n = 5,order_by = total) %>% 
  as.data.frame() %>% 
  group_by(anio) %>% 
  mutate(rank = row_number(),
         data=paste(iso_3,"|",percent,sep = "")) %>% 
  select(anio,data,rank) %>% as.data.frame() -> insumo_reporte
## `summarise()` has regrouped the output.
## ℹ Summaries were computed grouped by anio and iso_3.
## ℹ Output is grouped by anio.
## ℹ Use `summarise(.groups = "drop_last")` to silence this message.
## ℹ Use `summarise(.by = c(anio, iso_3))` for per-operation grouping
##   (`?dplyr::dplyr_by`) instead.
print(insumo_reporte)
##    anio      data rank
## 1  2022 USA|38.78    1
## 2  2022 GTM|17.05    2
## 3  2022 HND|16.65    3
## 4  2022  NIC|6.98    4
## 5  2022  CRI|4.27    5
## 6  2023  USA|35.6    1
## 7  2023 GTM|18.45    2
## 8  2023  HND|16.3    3
## 9  2023  NIC|7.85    4
## 10 2023  CRI|4.79    5
## 11 2024 USA|33.84    1
## 12 2024 GTM|19.53    2
## 13 2024 HND|16.14    3
## 14 2024  NIC|8.94    4
## 15 2024  CRI|4.71    5
## 16 2025 USA|32.45    1
## 17 2025 GTM|19.64    2
## 18 2025 HND|16.74    3
## 19 2025  NIC|9.12    4
## 20 2025  CRI|4.45    5
## 21 2026 USA|32.53    1
## 22 2026 GTM|19.46    2
## 23 2026 HND|15.44    3
## 24 2026   NIC|8.7    4
## 25 2026  CRI|4.51    5
library(tidyr)
insumo_reporte %>% 
pivot_wider(names_from = rank,values_from = data)->mi_tabla
print(mi_tabla)
## # A tibble: 5 × 6
##    anio `1`       `2`       `3`       `4`      `5`     
##   <dbl> <chr>     <chr>     <chr>     <chr>    <chr>   
## 1  2022 USA|38.78 GTM|17.05 HND|16.65 NIC|6.98 CRI|4.27
## 2  2023 USA|35.6  GTM|18.45 HND|16.3  NIC|7.85 CRI|4.79
## 3  2024 USA|33.84 GTM|19.53 HND|16.14 NIC|8.94 CRI|4.71
## 4  2025 USA|32.45 GTM|19.64 HND|16.74 NIC|9.12 CRI|4.45
## 5  2026 USA|32.53 GTM|19.46 HND|15.44 NIC|8.7  CRI|4.51
mi_tabla %>%
  kable(caption = paste("Top",5,"de Exportaciones periodo",
    min(anios_ranking),"-",max(anios_ranking))) %>%
  add_footnote(label = "Elaboración propia con base en datos del BCR") 
Top 5 de Exportaciones periodo 2022 - 2026
anio 1 2 3 4 5
2022 USA|38.78 GTM|17.05 HND|16.65 NIC|6.98 CRI|4.27
2023 USA|35.6 GTM|18.45 HND|16.3 NIC|7.85 CRI|4.79
2024 USA|33.84 GTM|19.53 HND|16.14 NIC|8.94 CRI|4.71
2025 USA|32.45 GTM|19.64 HND|16.74 NIC|9.12 CRI|4.45
2026 USA|32.53 GTM|19.46 HND|15.44 NIC|8.7 CRI|4.51

Note: aElaboración propia con base en datos del BCR