#install.packages("easypackages")
library("easypackages")
paq <- c("tidyverse", "data.table", "scales", "ggthemes", "showtext",
         "readxl", 'stats', "cluster", "factoextra", 'cowplot', 'tibble',
         'writexl', "kableExtra", "reactable")
libraries(paq)

Introducción

El presente manual tiene como objetivo presentar los principales formatos para la elaboración de tablas en formato html.

Tabla Base

Esta base esta compuesta por 75 filas y cuatro variables, una categórica y tres númericas.

##   Letra Bote Barco  Año
## 1     E    9    95 2021
## 2     E   54    48 2022
## 3     E   35    11 2022
## 4     B   30   113 2020
## 5     D   65    40 2021

Tabla Básica I

align = “cccc”: Indica que las cuatro columnas deben estar centradas, existe además la opción l (left), r(right).

Re_I <- kable(Re,align = "cccc")%>%
        kable_styling(bootstrap_options = "striped", font_size = 12, full_width = F)%>%
        kable_classic(full_width = F, html_font = "Arial Narrow")%>%
        row_spec(0, color = "white", background = "navy", align = "center", bold= T) 
Re_I
Letra Bote Barco Año
A 39 105 2020
B 67 137 2020
D 4 50 2021
B 7 104 2020
C 95 107 2021

Tabla Básica II: Título y fuente

Re_II <- kable(Re,align = "cccc",caption ="<center><strong>Ejemplo 2</strong></center>", format= "html",
               html_font = "Arial Narrow")%>%
         kable_styling(bootstrap_options = "striped", font_size = 12, full_width = F)%>%
         kable_classic(full_width = F, html_font = "Arial Narrow")%>%
         footnote(general="Friends",
                       general_title   = "Fuente:",
                       footnote_as_chunk = T, title_format = c("bold"))%>%
        row_spec(0, color = "white", background = "navy", align = "center", bold= T) 
Re_II
Ejemplo 2
Letra Bote Barco Año
A 39 105 2020
B 67 137 2020
D 4 50 2021
B 7 104 2020
C 95 107 2021
Fuente: Friends

Tabla Básica III: Decimal mark

Para diferenciar entre separador de decimal y miles, se aplica la siguiente rutina: knitr::kable(x, format.args = list(decimal.mark = ‘,’, big.mark = “’”))

Re_IV <- knitr::kable(Re_III,align = "cc",format.args = list(decimal.mark = ','
                                                             , big.mark = "."))%>%
         kable_styling(bootstrap_options = "striped", font_size = 12, full_width = F)%>%
         kable_classic(full_width = F, html_font = "Arial Narrow")%>%
         row_spec(0, color = "white", background = "navy", align = "center", bold= T) 

Re_IV
Letra Promedio
A 5.806,50
B 6.663,65
C 5.917,10
D 2.765,00

Tabla Básica IV: Scrool Box

Se_I <-  kable(Se,align = "cccc")%>%
         kable_styling(bootstrap_options = "striped", font_size = 12, full_width = F)%>%
         kable_classic(full_width = F, html_font = "Arial Narrow")%>%
         row_spec(0, color = "white", background = "#6897BB", bold= T)%>%
         scroll_box(height = "250px")
Se_I
Letra Bote Barco Año
C 148 22 2020
D 4 50 2021
D 13 76 2022
B 30 113 2020
C 144 65 2020
A 111 79 2020
D 60 47 2020
B 53 40 2020
E 127 134 2020
E 23 118 2022
A 148 73 2020
C 69 99 2020
E 56 46 2020
B 70 19 2022
B 142 114 2021

Tabla Básica V: Scrool Box & scale::percent

Se_II <- Se %>%
        group_by(Letra)%>%
        summarise(to=Barco/sum(Barco),
                  to=scales::percent(to,suffix = "%",decimal.mark = ","))
## Warning: Returning more (or less) than 1 row per `summarise()` group was deprecated in
## dplyr 1.1.0.
## i Please use `reframe()` instead.
## i When switching from `summarise()` to `reframe()`, remember that `reframe()`
##   always returns an ungrouped data frame and adjust accordingly.
## Call `lifecycle::last_lifecycle_warnings()` to see where this warning was
## generated.
Se_III <-  kable(Se_II,align = "cc")%>%
           kable_styling(bootstrap_options = "striped", font_size = 12, full_width = F)%>%
           kable_classic(full_width = F, html_font = "Arial Narrow")%>%
           row_spec(0, color = "white", background = "#6897BB", bold= T)%>%
           scroll_box(height = "250px")
Se_III
Letra to
A 52%
A 48%
B 39,51%
B 13,99%
B 6,64%
B 39,86%
C 12%
C 35%
C 53%
D 28,9%
D 43,9%
D 27,2%
E 45,0%
E 39,6%
E 15,4%

Tabla Base VI: merge rows - EN CONSTRUCCIÓN

En esta sección se aplicará una fórmula, que permitirá combinar celdas repetidas.

GR <-  kbl(Dattos_A,align = "llc", booktabs = TRUE,escape = FALSE)%>%
           kable_styling(bootstrap_options = "striped", font_size = 12, full_width = F)%>%
           kable_classic(full_width = F, html_font = "Arial Narrow")%>%
           row_spec(0, color = "white", background = "navy", bold= T)%>%
           kableExtra::collapse_rows(1,valign = "top")
           
GR
Home Sector Precios
Casa Norte 1364
Casa Sur 1412
Playa Manta 1128
Playa Montañita 1475
Bosque Cruz Loma 1144
Bosque Cotopaxi 1887

Tabla Base VII: Resaltar filas

Para que la rutina funcione, en la sección kable, deberá activarse la función escape = FALSE

Fuego <- Dattos_A %>%
         mutate("Home"= cell_spec(`Home`, color = ifelse(`Home` == "Playa", "white", "white"),
                       background  = ifelse(`Home` == "Playa", "#500000", "#0b5394")))%>%
         kable(escape = FALSE)%>%
         kable_styling()%>%
         kable_classic(full_width = F, html_font = "Arial Narrow")%>%
         row_spec(0, color = "white", background = "navy", bold= T)

Fuego
Home Sector Precios
Casa Norte 1364
Casa Sur 1412
Playa Manta 1128
Playa Montañita 1475
Bosque Cruz Loma 1144
Bosque Cotopaxi 1887

Función

Con la opción add_indent, es posible aplicar la opción de sangría, mientras que con la opción level_of_indent, es factible determinar el nivel de la sangría.

fg <- function(data){
  knitr::kable(data,format.args = list(decimal.mark=",",big.mark="."))%>%
  kable_styling(bootstrap_options = "striped", font_size = 12, full_width = F)%>%
  add_indent(c(1,2,3,5), level_of_indent = 1)%>%  
  add_indent(c(4,6,7), level_of_indent = 2)
}

AD <- fg(data_set)

Library reactable

Para facilitar el análisis de grandes volúmenes de datos presentados en tablas, es altamente recomendable utilizar la librería reactable

La columna LO se mantiene fija.

ad <- reactable(
  Davv,
  columns = list(
    LO = colDef(
      headerStyle = list(position = "sticky", left = 0, zIndex = 1, backgroundColor = "white"),
      style = list(position = "sticky", left = 0, backgroundColor = "white")
    )
  ),
  defaultPageSize = 10,
  style = list(fontSize = "12px"),
  highlight = TRUE,
  searchable = TRUE
)

ad