Help with KableExtra / Hide columns

Hello,
I am a real R Markdown beginner ( I began 10 days ago) and I am stuck on removing the first two columns of the attached table screenshot.
I use the first two columns of the dtb variable (DES_LIV04 and DES_LIV05) to order/group my report .
The print statement is working fine, but their values are still showing for each detail line.
I did try remove.column , col_names and hundreds of other attempts with no success.
Any help is really appreciated.

knitr::opts_chunk$set(echo = TRUE)
library(knitr)
library(xtable)
library(pander)
library(htmltools)
library(DT)
library(data.table)
library(magrittr)
library(kableExtra)

tabolato <- read.csv("/Users/gm/Developments/myApps/myshiny/tabolato.csv", header = TRUE)

\newpage


liv04 <-unique(tabolato[,12])
elements <- length(liv04)
dtb <- data.table(tabolato)
dtb <- dtb[, .(DES_LIV04,DES_LIV05,DESCRIZIONE,CDARTICOLO,CDARTFORN,RAGSOC,
               COSTONETTOAC,COSTONETTONETTOAC,PREZZO,PREZZOIVATOAC,ALIQIVA,UM,
               QTAMOVIMENTATAAC,QTAVENDUTAAC,PCTMARGINENETTO,PCTMARGINENETTONETTO)]

z = 0
cat("\n") 
for (i in liv04){
  
  cat("\n") 
  cat("\\newpage") 
  cat("## ", i, "\n") # Create headings
  ans <- dtb[DES_LIV04 == i]

  print(
    kable(ans, "latex", booktabs = TRUE, longtable = TRUE, caption = i) %>%
    kable_paper("striped", full_width = F) %>%
    kable_styling(latex_options = c("hold_position", "repeat_header"), font_size = 5) %>%
  pack_rows(index = table(ans$DES_LIV05)) 
  )

  cat("\n")
  z = z + 1
  if (z == elements-200){
    break 
  } 
  }

.

I don't think this is a Rmarkdown question specifically.

You need to try to make a working R code to get the correct result in the R Markdown output.

To remove column shown by kable, you should remove them from the data.frame

To know more about how to wrangle data I would advice to read

Hi,
thank you very much for your reply and for the link, I will read "Welcome I R for Data Science" for sure.
To be honest I tried to remove "ans$DES_LIV05" column, but If I do it I cannot use
" pack_rows(index = table(ans$DES_LIV05)) ".
If the column is present in the data.frame it's printed twice :

  1. Grouped column (that's correct)
  2. Detail level (this is duplicate).

I did print the detail level DES_LIV05 value in white color and put it as last column as a workaround
column_spec(17, width = "5cm", color="white") .
I know it's very bad coding but I googled all day without finding a proper solution.

print(
kable(ans, "latex", booktabs = TRUE, longtable = TRUE, caption = i,
col.names = c("Descrizione",
"Articolo Nostro",
"Articolo Fornit",
"Ragione Sociale",
"Costo Netto",
"Fine Anno",
"Costo Netto Netto",
"Prezzo AC",
"Prezzo Ivato AC",
"Iva",
"UM",
"Qta Acq.",
"Qta Vend",
"Qta Rim.",
"% Marg. Netto",
"% Marg. Netto Netto",
".",
"ArtRaggr",
"."
) ) %>%
kable_paper("striped", full_width = F) %>%
kable_styling(latex_options = c("repeat_header"), font_size = 6) %>%
pack_rows(index = table(ans$DES_LIV05)) %>%
column_spec(1,width = "5.5cm") %>%
column_spec(2:2, width = "1cm",
background = ifelse(ans$CDARTRAGGR == ans$CDARTICOLO, "white", "green")) %>%
column_spec(3:3, width = "1cm") %>%
column_spec(4,width = "3.1cm") %>%
column_spec(5:14, width = "0.8cm") %>%
column_spec(15:16, width = "1.0cm") %>%
column_spec(17, width = "5cm", color="white")
)

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.