rknitrkabler-colnames

Flexible Column names in knitr::kable table


Problem:

Have a large df with many variables. Trying to create a summary kable table with different column names than the original variables.

library(knitr)
library(dplyr)

knitr::kable(iris %>% head(),
             col.names = c("Sepal length", "Sepal width", "Petal length",
                           "Petal width", "Species"))

| Sepal length| Sepal width| Petal length| Petal width|Species |
|------------:|-----------:|------------:|-----------:|:-------|
|          5.1|         3.5|          1.4|         0.2|setosa  |
|          4.9|         3.0|          1.4|         0.2|setosa  |
|          4.7|         3.2|          1.3|         0.2|setosa  |
|          4.6|         3.1|          1.5|         0.2|setosa  |
|          5.0|         3.6|          1.4|         0.2|setosa  |
|          5.4|         3.9|          1.7|         0.4|setosa  |

I want to be able to select specific columns without having to manually rename them via:

knitr::kable(iris %>% filter(Species == "setosa") %>% 
           select(Sepal.Length, Sepal.Width, Species) %>% head(),
         col.names = c("Sepal length", "Sepal width", "Species"))

| Sepal length| Sepal width|Species |
|------------:|-----------:|:-------|
|          5.1|         3.5|setosa  |
|          4.9|         3.0|setosa  |
|          4.7|         3.2|setosa  |
|          4.6|         3.1|setosa  |
|          5.0|         3.6|setosa  |
|          5.4|         3.9|setosa  |

Is there a way to specify only once that I want "Sepal.Length" to be "Sepal Length." That way if I include a certain variable, my code will recognize the variable and rename it to a new column name automatically without me having to go back and do it manually?


Solution

  • Why not rename the columns up front, and use the formatted column names in subsequent calls to filter and select?

    library(tidyverse)
    
    rename(iris, `Sepal length` = Sepal.Length, `Sepal width` = Sepal.Width) %>% 
      filter(Species == "setosa") %>% 
      select(contains("Sepal"), Species) %>% 
      head() %>% 
      knitr::kable()
    
    
    | Sepal length| Sepal width|Species |
    |------------:|-----------:|:-------|
    |          5.1|         3.5|setosa  |
    |          4.9|         3.0|setosa  |
    |          4.7|         3.2|setosa  |
    |          4.6|         3.1|setosa  |
    |          5.0|         3.6|setosa  |
    |          5.4|         3.9|setosa  |