1

Is it possible to customize the headings portion printed with the freq() and descr() functions in the summarytools R package?

Ultimately, my aim here is to make headings above my freq() or descr() rmarkdown outputs. (I don't mean the # style markdown headings.) So maybe if I cannot customize these function outputs, is there a way I could add custom text as a heading directly above the output so in rmarkdown for a seemless look?

Looks like I can either keep or discard the output headings with headings=FALSE but not customize it. Data & code example below.

Goals:

  1. Write my own name(s) for the variable(s) expressed in the output.
  2. Remove the default type of function, coded variable name, and type of variable.
  3. Use these changes in rmarkdown.

I've looked through the function help docs and few design vignettes such as Summarytools in R Markdown Documents by Dominic Comtois, but maybe I'm missing something.

toy <- data.frame(x=c("a","b","c","a","b","a"))

freq(toy$x, cumul=FALSE)

# output:
#Frequencies  
#toy$x  
#Type: Character  
#
#              Freq   % Valid   % Total
#----------- ------ --------- ---------
#          a      3     50.00     50.00
#          b      2     33.33     33.33
#          c      1     16.67     16.67
#       <NA>      0                0.00
#      Total      6    100.00    100.00

freq(toy$x, cumul=FALSE, headings=FALSE)

#output:
#              Freq   % Valid   % Total
#----------- ------ --------- ---------
#          a      3     50.00     50.00
#          b      2     33.33     33.33
#          c      1     16.67     16.67
#       <NA>      0                0.00
#      Total      6    100.00    100.00

#EXAMPLE GOAL:
#Letter Groups
#              Freq   % Valid   % Total
#----------- ------ --------- ---------
#          a      3     50.00     50.00
#          b      2     33.33     33.33
#          c      1     16.67     16.67
#       <NA>      0                0.00
#      Total      6    100.00    100.00
Shawn Janzen
  • 369
  • 3
  • 15
  • Look into the `attributes()`of the objects created by *summarytools*; You can tweak many elements if you store the results into an object, for example `ff <- freq(...)`, and then examine `attributes(ff)`... and redefine those attributes. You can also check the docs for the print() / view() function of the package, which lets you override the default values of various heading elements. – Dominic Comtois Aug 20 '23 at 14:04

0 Answers0