The function provides a way to get column keys associated with the flextable corresponding to a tabulator() object. It helps in customizing or programing with tabulator.

The function is using column names from the original dataset, eventually filters and returns the names corresponding to the selection.

tabulator_colnames(x, columns, type = NULL, ...)

Arguments

x

a tabulator() object

columns

column names to look for

type

the type of column to look for, it can be:

  • 'columns': visible columns, corresponding to names provided in the '...' arguments of your call to 'tabulator()'.

  • 'hidden': unvisible columns, corresponding to names of the original dataset columns.

  • 'rows': visible columns used as 'row' content

  • 'rows_supp': visible columns used as 'rows_supp' content

  • NULL: any type of column

...

any filter conditions that use variables names, the same than the argument columns of function tabulator() (tabulator(columns = c("col1", "col2"))).

Examples

library(flextable)

cancer_dat <- data.frame(
  count = c(
    9L, 5L, 1L, 2L, 2L, 1L, 9L, 3L, 1L, 10L, 2L, 1L, 1L, 2L, 0L, 3L,
    2L, 1L, 1L, 2L, 0L, 12L, 4L, 1L, 7L, 3L, 1L, 5L, 5L, 3L, 10L,
    4L, 1L, 4L, 2L, 0L, 3L, 1L, 0L, 4L, 4L, 2L, 42L, 28L, 19L, 26L,
    19L, 11L, 12L, 10L, 7L, 10L, 5L, 6L, 5L, 0L, 3L, 4L, 3L, 3L,
    1L, 2L, 3L
  ),
  risktime = c(
    157L, 77L, 21L, 139L, 68L, 17L, 126L, 63L, 14L, 102L, 55L,
    12L, 88L, 50L, 10L, 82L, 45L, 8L, 76L, 42L, 6L, 134L, 71L,
    22L, 110L, 63L, 18L, 96L, 58L, 14L, 86L, 42L, 10L, 66L,
    35L, 8L, 59L, 32L, 8L, 51L, 28L, 6L, 212L, 130L, 101L,
    136L, 72L, 63L, 90L, 42L, 43L, 64L, 21L, 32L, 47L, 14L,
    21L, 39L, 13L, 14L, 29L, 7L, 10L
  ),
  time = rep(as.character(1:7), 3),
  histology = rep(as.character(1:3), 21),
  stage = rep(as.character(1:3), each = 21)
)

datasup_first <- data.frame(
  time = factor(1:7, levels = 1:7),
  zzz = runif(7)
)

z <- tabulator(cancer_dat,
  rows = "time",
  columns = c("histology", "stage"),
  datasup_first = datasup_first,
  n = as_paragraph(as_chunk(count))
)

j <- tabulator_colnames(
  x = z, type = "columns",
  columns = c("n"),
  stage %in% 1
)

src <- tabulator_colnames(
  x = z, type = "hidden",
  columns = c("count"),
  stage %in% 1
)

if (require("scales")) {
  colourer <- col_numeric(
    palette = c("wheat", "red"),
    domain = c(0, 45)
  )
  ft_1 <- as_flextable(z)
  ft_1 <- bg(
    ft_1,
    bg = colourer, part = "body",
    j = j, source = src
  )
  ft_1
}
#> a flextable object.
#> col_keys: `time`, `dummy1`, `zzz`, `dummy2`, `1@1@n`, `dummy3`, `1@2@n`, `dummy4`, `1@3@n`, `dummy5`, `2@1@n`, `dummy6`, `2@2@n`, `dummy7`, `2@3@n`, `dummy8`, `3@1@n`, `dummy9`, `3@2@n`, `dummy10`, `3@3@n` 
#> header has 2 row(s) 
#> body has 7 row(s) 
#> original dataset sample: 
#>   time count@1@1 count@1@2 count@1@3 count@2@1 count@2@2 count@2@3 count@3@1
#> 1    1         9        12        42         3         5        10         0
#> 2    2         3         3         4         5         4        28         1
#> 3    3        10        10        10         2         1         3         1
#> 4    4         2         7        26         2         4         5         1
#> 5    5         1         4         1         2         3        19         1
#>   count@3@2 count@3@3 risktime@1@1 risktime@1@2 risktime@1@3 risktime@2@1
#> 1         0         3          157          134          212           63
#> 2         3         7           82           59           39           77
#> 3         1        19          102           86           64           45
#> 4         0         3          139          110          136           55
#> 5         1         6           76           51           29           68
#>   risktime@2@2 risktime@2@3 risktime@3@1 risktime@3@2 risktime@3@3 n@1@1 n@1@2
#> 1           58           42           10            8           21            
#> 2           71          130           14           14           43            
#> 3           32           13           21           22          101            
#> 4           42           21            8            8           14            
#> 5           63           72           12           10           32            
#>   n@1@3 n@2@1 n@2@2 n@2@3 n@3@1 n@3@2 n@3@3       zzz dummy1 dummy2 1@1@n
#> 1                                           0.5435622                    
#> 2                                           0.5460695                    
#> 3                                           0.7413802                    
#> 4                                           0.5206874                    
#> 5                                           0.2230991                    
#>   dummy3 1@2@n dummy4 1@3@n dummy5 2@1@n dummy6 2@2@n dummy7 2@3@n dummy8 3@1@n
#> 1                                                                              
#> 2                                                                              
#> 3                                                                              
#> 4                                                                              
#> 5                                                                              
#>   dummy9 3@2@n dummy10 3@3@n
#> 1                           
#> 2                           
#> 3                           
#> 4                           
#> 5