Search code examples
htmlrr-markdownrenamekable

How can I rename the rows (not columns) of a Kable in R, specifically in RMarkdown?


I run a code that pulls from different places in order to create the test_results variable. This variable I have put into a kable so I can control the output of it better in the HTML file I am creating. The code below creates a kable table exactly how I want it formatted, and I was able to rename the column names how I want those. What can I add to this code to rename the row names? I have tried row.names and other things but I either get an error or that tibble is depreciated with the function I was trying to use.

kable(head(test_results), caption = "**Test Results**",
      col.names = c("Test Result",
                    "N",
                    "Total"))%>%
  
  kable_material(c("striped", "hover"))

Solution

  • Change row names before sending it to kable.

    library(kable)
    library(kableExtra) # kable_material
    mt <- mtcars[1:3, 1:4]
    kable(mt) %>% kable_material(c("striped", "hover"))
    

    kable/html with original row names

    rownames(mt) <- c("hello", "world", "again")
    kable(mt) %>% kable_material(c("striped", "hover"))
    

    kable/html with row names adjusted