0

I would like to ask support in handling a table in R Markdown. Let me specify that this document will be then knitted to pdf through the engine pdflatex.

Here my code:

library(knitr)
library(kableExtra)
library(xtable)
sast_vuln <- last_scan_vuln[last_scan_vuln$Analysis_type == 'Sast', ]

CWE_Table <- data.frame(
  "CWE_ID" = names(sort(table(sast_vuln$cweID), decreasing = T))[1:5],
  "Count" = as.vector(sort(table(sast_vuln$cweID), decreasing = T))[1:5]
)

CWE_Table$Description <- sast_vuln$title[match(CWE_Table$CWE_ID, sast_vuln$cweID)]

colnames(CWE_Table)[1] <- "CWE ID"

# Stampa la tabella

xtable2kable <- function(x) {
  out <- capture.output(print(x, table.placement = NULL))[-(1:2)]
  out <- paste(out, collapse = "\n")
  structure(out, format = "latex", class = "knitr_kable")
}

cat(xtable(
  CWE_Table,
  caption = "Top 5 CWE IDs",
  align = "|cccl|",
  booktabs = T
) %>% xtable2kable %>%
  kable_styling(bootstrap_options = "striped", 
                full_width = FALSE, 
                position = "center",
                latex_options = "hold_position",
                font_size = 14
  ) %>%
  row_spec(0, bold = TRUE, background = funGreen),
"\n"
)

Results of my code

What I would like to achieve is:

  1. Remove the row names (1, 2, 3, 4, 5). I have tried "include.rownames = F" in the arguments of xtable but it is not working. Moreover, also if I try to change the column names through xtable it is not working. It seems that it is forced to take row and column names from the original dataframe CWE_Table, and I'm not able to remove the row names from a standard dataframe;
  2. I would like to highlight in light grey alternate rows. I have looked at many materials but none of them was working.

Let me clarify that I needed to create the function "xtable2kable" (found online) because kable does not allow directly to move the caption of the table at the bottom of it.

Thanks in advance to those which will spend their time to try to help me.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.