6  Pipe interface

A better way to compose and style your table.

6.1 Basics

Mostly working with this data; but some others come in later to illustrate certain features.

data <- pmt_summarized
head(data)
# A tibble: 6 × 9
  STUDY       DOSE   FORM    N     WT    CRCL  AGE   ALB   SCR  
  <chr>       <chr>  <chr>   <chr> <chr> <chr> <chr> <chr> <chr>
1 12-DEMO-001 100 mg tablet  80    71.4  104   33.7  4.20  1.06 
2 12-DEMO-001 150 mg capsule 16    89.4  122   24.4  4.63  1.12 
3 12-DEMO-001 150 mg tablet  48    81.7  104   34.4  3.83  0.910
4 12-DEMO-001 150 mg troche  16    94.0  93.2  27.4  4.94  1.25 
5 12-DEMO-001 200 mg tablet  64    67.9  100   27.5  4.25  1.10 
6 12-DEMO-001 200 mg troche  16    76.6  99.2  22.8  4.54  1.15 

You start out a pipeline by passing your data frame into st_new()

data %>% st_new() %>% class
[1] "stobject"    "environment"

This creates an object that gets revised by subsequent steps in the pipeline, adding features and styling as you go.

For the final step in the pipeline, we’ll send the object to stable() to create the table

data %>% 
  st_new() %>% 
  stable() %>% 
  head(n=9)
[1] "\\setlength{\\tabcolsep}{5pt} "                                            
[2] "\\begin{threeparttable}"                                                   
[3] "\\renewcommand{\\arraystretch}{1.3}"                                       
[4] "\\begin{tabular}[h]{lllllllll}"                                            
[5] "\\hline"                                                                   
[6] "STUDY & DOSE & FORM & N & WT & CRCL & AGE & ALB & SCR \\\\"                
[7] "\\hline"                                                                   
[8] "12-DEMO-001 & 100 mg & tablet & 80 & 71.4 & 104 & 33.7 & 4.20 & 1.06 \\\\" 
[9] "12-DEMO-001 & 150 mg & capsule & 16 & 89.4 & 122 & 24.4 & 4.63 & 1.12 \\\\"

6.2 Simple table

You can terminate the pipeline and create a tabular table by sending to stable()

data %>% 
  st_new() %>% 
  stable()

Equivalent call

data %>% stable()

6.3 Long table

You can also pipe to stable_long() to make a long table

data %>% 
  st_new() %>% 
  stable_long()

6.4 Annotate with file names

st_new(data) %>% st_files(r = "foo.R", output = "foo.tex")

Equivalent call

stable(
  data, 
  r_file = "foo.R", 
  output_file = "foo.tex"
)

Note that in the pipe version, we already have file in the function name so that gets dropped from the argument name.

6.5 Save a table

First convert with stable() or stable_long() then save with stable_save()

st_new(data) %>% 
  st_files(output = "foo.tex") %>%
  stable() %>% 
  stable_save()

6.6 Align columns

  • st_center(...)
  • st_left(...)
  • st_right(...)
  • st_align(...)
st_new(data) %>% 
  st_center() %>% 
  stable() 

Equivalent call

stable(data, align = cols_center()) 

6.7 Fix a column width

This is an aspect of aligning columns and uses col_ragged() most of the time. For example, if we want columns left-justified but the Title column is to be capped at 5 cm

st_new(data) %>% 
  st_left(Title = col_ragged(5)) %>%
  stable()

6.8 Rename columns

st_new(data) %>% 
  st_rename(Weight = "WT") %>% 
  stable()

Equivalent call

stable(data, cols_rename = c(Weight = "WT")) 

6.9 Blank columns

st_new(data) %>% 
  st_blank(WT, ALB, DOSE) %>% 
  stable()

Equivalent call

stable(data, cols_blank = "WT,ALB,DOSE")

6.10 Drop columns

st_new(data) %>% 
  st_drop(WT, ALB, DOSE) %>% 
  stable()

Equivalent call

stable(data, drop = "WT, ALB, DOSE")

6.11 Notes

  • Multiple calls are allowed; notes will accumulate
st_new(data) %>% 
  st_notes("first note") %>% 
  st_notes("second note") %>% 
  stable()

Equivalent call

stable(
  data, 
  notes = c("first note", "second note")
)

6.12 Units

st_new(data) %>% 
  st_units(WT = "kg", AGE = "years") %>% 
  stable()

Note that st_units() will automatically add parens to your units; this can be suppressed with the parens argument.

Units can also be added as a list

u <- list(WT = "kg", AGE = "years")

st_new(data) %>% 
  st_units(u) %>% 
  stable()

Equivalent call

stable(data, units = u)

6.13 panel

st_new(data) %>% 
  st_panel("STUDY") %>% 
  stable()

Equivalent call

stable(data, panel = "STUDY")

6.14 span

st_new(data) %>% 
  st_span("Covariates", c(WT, ALB, EGFR)) %>% 
  stable()

Equivalent call

stable(
  data, 
  span = as.span("Covariates", c(WT, ALB, EGFR))
)

6.15 hlines - at

To put horizontal lines at specific rows

st_new(data) %>% 
  st_hline(at = c(2,4,6))

Equivalent call

stable(data, hline_at = c(2,4,6))

6.16 hlines - from

To calculate hlines based on data frame column values (for example, to break the table by changing values of STUDYf)

st_new(data) %>% 
  st_hline(from = "STUDYf")

Equivalent call

stable(data, hline_from = "STUDYf")

6.17 hlines - pattern

To search the table for a pattern and add hlines above matches

st_new(data) %>% 
  st_hline(pattern  = "All", cols = "Summary")

The cols argument limits the search to the Summary column; omit this argument to search the whole table.

There is no equivalent call for this using straight stable().

6.18 Clear replicate values

To create groups by “clearing” replicate values in a column

st_new(data) %>% 
  st_clear_reps(STUDY) %>% 
  stable()

Equivalent call

stable(data, clear_reps = "STUDY")

7 Row and column padding

Use st_sizes() to change padding in rows or columns or adjust other aspects of table sizes

st_new(data) %>% 
  st_sizes(row = 1.1, col = 6) %>%
  stable()

Equivalent call

stable(data, sizes = tab_size(row = 1.1, col = 6))
 [1] "\\setlength{\\tabcolsep}{6pt} "                                             
 [2] "\\begin{threeparttable}"                                                    
 [3] "\\renewcommand{\\arraystretch}{1.1}"                                        
 [4] "\\begin{tabular}[h]{lllllllll}"                                             
 [5] "\\hline"                                                                    
 [6] "STUDY & DOSE & FORM & N & WT & CRCL & AGE & ALB & SCR \\\\"                 
 [7] "\\hline"                                                                    
 [8] "12-DEMO-001 & 100 mg & tablet & 80 & 71.4 & 104 & 33.7 & 4.20 & 1.06 \\\\"  
 [9] "12-DEMO-001 & 150 mg & capsule & 16 & 89.4 & 122 & 24.4 & 4.63 & 1.12 \\\\" 
[10] "12-DEMO-001 & 150 mg & tablet & 48 & 81.7 & 104 & 34.4 & 3.83 & 0.910 \\\\" 
[11] "12-DEMO-001 & 150 mg & troche & 16 & 94.0 & 93.2 & 27.4 & 4.94 & 1.25 \\\\" 
[12] "12-DEMO-001 & 200 mg & tablet & 64 & 67.9 & 100 & 27.5 & 4.25 & 1.10 \\\\"  
[13] "12-DEMO-001 & 200 mg & troche & 16 & 76.6 & 99.2 & 22.8 & 4.54 & 1.15 \\\\" 
[14] "12-DEMO-002 & 100 mg & capsule & 36 & 61.3 & 113 & 38.3 & 4.04 & 1.28 \\\\" 
[15] "12-DEMO-002 & 100 mg & tablet & 324 & 77.6 & 106 & 29.9 & 4.31 & 0.981 \\\\"
[16] "12-DEMO-002 & 50 mg & capsule & 36 & 74.1 & 112 & 37.1 & 4.44 & 0.900 \\\\" 
[17] "12-DEMO-002 & 50 mg & tablet & 324 & 71.2 & 106 & 34.1 & 4.63 & 0.868 \\\\" 
[18] "12-DEMO-002 & 75 mg & capsule & 36 & 72.4 & 105 & 38.2 & 3.89 & 0.900 \\\\" 
[19] "12-DEMO-002 & 75 mg & tablet & 288 & 71.6 & 98.9 & 34.2 & 4.49 & 0.991 \\\\"
[20] "12-DEMO-002 & 75 mg & troche & 36 & 73.6 & 103 & 49.2 & 4.52 & 0.930 \\\\"  
[21] "\\hline"                                                                    
[22] "\\end{tabular}"                                                             
[23] "\\end{threeparttable}"                                                      
attr(,"class")
[1] "stable"

The arguments to st_sizes() get passed to tab_size().

8 Pipe cheat table