Search code examples
rr-markdownpurrrkable

Problems applying a map2 function with kable. Kable does not read the names of the output tables properly


I have this dataset

dput(per_stack[1:4, ])

which gives this output

structure(list(compliment_perc_index_bloc_org_plan_migpunts = c("Menys del 25%", 
"Menys del 25%", "Menys del 25%", "Menys del 25%"), compliment_perc_index_bloc_pressupostari_migpunts = c("Menys del 25%", 
"Menys del 25%", "Entre un 50% i un 75%", "Menys del 25%"), compliment_perc_index_bloc_contractacio_migpunts = c("Menys del 25%", 
"Menys del 25%", "Entre un 25% i un 50%", "Entre un 50% i un 75%"
), compliment_perc_index_bloc_serveis_migpunts = c("Menys del 25%", 
"Entre un 25% i un 50%", "Entre un 25% i un 50%", "Menys del 25%"
), compliment_perc_index_bloc_ajudes_subven_migpunts = c("Entre un 25% i un 50%", 
"Entre un 25% i un 50%", "Entre un 25% i un 50%", "Menys del 25%"
), compliment_perc_index_bloc_info_electes_migpunts = c("Menys del 25%", 
"Menys del 25%", "Menys del 25%", "Menys del 25%"), compliment_perc_index_bloc_patrimoni_migpunts = c("Menys del 25%", 
"Més del 75%", "Més del 75%", "Menys del 25%"), compliment_perc_index_bloc_consultes_migpunts = c("Menys del 25%", 
"Menys del 25%", "Menys del 25%", "Menys del 25%"), compliment_perc_index_bloc_info_rell_jurid_migpunts = c("Entre un 50% i un 75%", 
"Entre un 25% i un 50%", "Entre un 50% i un 75%", "Entre un 25% i un 50%"
), Tramspoblacio = structure(c(5L, 4L, 4L, 4L), .Label = c("1 - 999", 
"1000 - 4999", "5000 - 9999", "10000 - 19999", "20000 - 49999", 
"50000 - 99999", "100000 - 200000", "Mayor de 200000"), class = "factor")), row.names = c(NA, 
4L), class = "data.frame") 

I have done a map2 function to avoid repeating an operation.

So, I created a vector with the variables I want to avoid writing and the variable I want to group by

target_var <- c("compliment_perc_index_bloc_org_plan_migpunts", "compliment_perc_index_bloc_pressupostari_migpunts", "compliment_perc_index_bloc_contractacio_migpunts",
                "compliment_perc_index_bloc_serveis_migpunts", "compliment_perc_index_bloc_ajudes_subven_migpunts", "compliment_perc_index_bloc_info_electes_migpunts",
                "compliment_perc_index_bloc_patrimoni_migpunts", "compliment_perc_index_bloc_consultes_migpunts", "compliment_perc_index_bloc_info_rell_jurid_migpunts" ) 

I want to group by this variable

group_var <- c("Tramspoblacio")

I have built an alternative dataframe

df1 = expand.grid(target_var, group_var, stringsAsFactors = F)

And I have done the map operation, which works

index_blocs_map_grups_compliment <- map2(df1$Var2, df1$Var1, ~ round(prop.table(table(per_stack[, .x], per_stack[, .y]), margin=1)*100,1)) %>% purrr::set_names(df1$Var1)

When I open it with markdown, I apply the operation

##I call the function
index_blocs_map_grups_compliment <- map2(df1$Var2, df1$Var1, ~ round(prop.table(table(per_stack[, .x], per_stack[, .y]), margin=1)*100,1)) %>% purrr::set_names(df1$Var1)
#I apply it with kable
kable(index_blocs_map_grups_compliment, caption = "<center><strong>Percentatge segons grup de població </strong></center>") %>% 
  kable_styling(bootstrap_options = c("striped", "hover"))

When I apply the function without kable, It creates the 9 tables with the $, as it does in RStudio. However, when I want to format it with kable, the name of each table is lost and I lose the original format... My question is how I can change the name of each of the tables using kable. If I try to change it with "Caption" I can only change the title of the whole result (the title of the 9 tables altogether), but not of each one of the 9 tables.

Thank you very much!!!


Solution

  • As far as I get it the issue is that you are applying kable on the list of tables. I you want nine tables then use purrr::map to loop over your list of tables and to format each separately via kable. If you want to add a caption to each of your plots then you could use purrr::imap or purrr::map2. Finally, as you mentioned that you want to add the tables to an Rmd I would suggest to use purrr::walk which requires to add a print() statement and to set results="asis" in the code chunk options:

    ---
    title: "Untitled"
    output: html_document
    date: "2023-03-07"
    ---
    
    ```{r setup, include=FALSE}
    knitr::opts_chunk$set(echo = TRUE)
    ```
    
    ```{r echo=FALSE}
    per_stack <- structure(list(compliment_perc_index_bloc_org_plan_migpunts = c(
      "Menys del 25%",
      "Menys del 25%", "Menys del 25%", "Menys del 25%"
    ), compliment_perc_index_bloc_pressupostari_migpunts = c(
      "Menys del 25%",
      "Menys del 25%", "Entre un 50% i un 75%", "Menys del 25%"
    ), compliment_perc_index_bloc_contractacio_migpunts = c(
      "Menys del 25%",
      "Menys del 25%", "Entre un 25% i un 50%", "Entre un 50% i un 75%"
    ), compliment_perc_index_bloc_serveis_migpunts = c(
      "Menys del 25%",
      "Entre un 25% i un 50%", "Entre un 25% i un 50%", "Menys del 25%"
    ), compliment_perc_index_bloc_ajudes_subven_migpunts = c(
      "Entre un 25% i un 50%",
      "Entre un 25% i un 50%", "Entre un 25% i un 50%", "Menys del 25%"
    ), compliment_perc_index_bloc_info_electes_migpunts = c(
      "Menys del 25%",
      "Menys del 25%", "Menys del 25%", "Menys del 25%"
    ), compliment_perc_index_bloc_patrimoni_migpunts = c(
      "Menys del 25%",
      "Més del 75%", "Més del 75%", "Menys del 25%"
    ), compliment_perc_index_bloc_consultes_migpunts = c(
      "Menys del 25%",
      "Menys del 25%", "Menys del 25%", "Menys del 25%"
    ), compliment_perc_index_bloc_info_rell_jurid_migpunts = c(
      "Entre un 50% i un 75%",
      "Entre un 25% i un 50%", "Entre un 50% i un 75%", "Entre un 25% i un 50%"
    ), Tramspoblacio = structure(c(5L, 4L, 4L, 4L), .Label = c(
      "1 - 999",
      "1000 - 4999", "5000 - 9999", "10000 - 19999", "20000 - 49999",
      "50000 - 99999", "100000 - 200000", "Mayor de 200000"
    ), class = "factor")), row.names = c(
      NA,
      4L
    ), class = "data.frame")
    
    
    target_var <- c(
      "compliment_perc_index_bloc_org_plan_migpunts", "compliment_perc_index_bloc_pressupostari_migpunts", "compliment_perc_index_bloc_contractacio_migpunts",
      "compliment_perc_index_bloc_serveis_migpunts", "compliment_perc_index_bloc_ajudes_subven_migpunts", "compliment_perc_index_bloc_info_electes_migpunts",
      "compliment_perc_index_bloc_patrimoni_migpunts", "compliment_perc_index_bloc_consultes_migpunts", "compliment_perc_index_bloc_info_rell_jurid_migpunts"
    )
    
    group_var <- c("Tramspoblacio")
    
    df1 <- expand.grid(target_var, group_var, stringsAsFactors = F)
    ```
    
    ```{r}
    library(purrr)
    library(kableExtra)
    library(knitr)
    
    index_blocs_map_grups_compliment <- map2(
      df1$Var2, df1$Var1, ~ round(prop.table(table(per_stack[, .x], per_stack[, .y]), margin = 1) * 100, 1)
    ) %>%
      purrr::set_names(df1$Var1)
    
    index_blocs_map_grups_compliment <- map2(df1$Var2, df1$Var1, ~ round(prop.table(table(per_stack[, .x], per_stack[, .y]), margin = 1) * 100, 1)) %>% purrr::set_names(df1$Var1)
    ```
    
    ```{r, results='asis'}
    iwalk(index_blocs_map_grups_compliment, function(x, y) {
      kable(x, caption = paste0("<center><strong>", y, "</strong></center>")) %>%
        kable_styling(bootstrap_options = c("striped", "hover")) |> 
        print()
    })
    ```
    

    enter image description here