library(dplyr)
library(purrr)

\clearpage

Data

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

library(pmtables)
library(yspec)
spec <- ys_help$spec()

data <- pmt_summarized

head(data)

asis

In this document, we are rendering tables inline as we knit. To accomplish this we need to pipe tables to st_asis() and you will see that in every example. If you are knitting a document and want to include a table, you should do this. If you are not knitting a document with included tables, you shouldn't use st_asis()

\clearpage

Simple table

data %>% stable(cols_bold=TRUE) %>% st_asis()

\clearpage

Annotate

stable(
  data, 
  r_file = "foo.R", 
  output_file = "foo.tex", 
  notes = c("Data were analyzed in quadruplicate.", "The results are very clear."),
) %>% st_asis()

\clearpage

Notes in minipage

stable(
  data, 
  note_config = noteconf(type = "minipage", width = 0.85),
  notes = c("Data were analyzed in quadruplicate.", "The results are very clear."),
) %>% st_asis()

\clearpage

Align

data %>% 
  stable(align = cols_center(STUDY = 'l', .r = "DOSE,SCR")) %>%
  st_asis()

\clearpage

Units

units <- ys_get_unit(spec, parens = TRUE)

stable(
  data,
  units = units,
) %>% st_asis()

\clearpage

Expand header rows

stable(
  data, 
  cols_rename = vars("Study...Number" = STUDY, "Serum...Albumin" = ALB), 
  units = units
) %>% st_asis()

\clearpage

Extra column information

tmp <- slice(ptdata(), 1:5)

xtra0 <- slice(tmp,1) %>% mutate(across(everything(), ~""))
xtra0

xtra1 <- mutate(xtra0, DOSE = "(mg)", N = "(number)", WT = "(kg)")

xtra2 <- mutate(xtra0, WT = "[baseline]")

xtra <- bind_rows(xtra2,xtra1)

xtra
stable(tmp, cols_extra = xtra) %>% st_asis()

\clearpage

Math

ptab <- readRDS("datasets/ptab.RDS")
ptab
stable(
  ptab, 
  align = cols_center(Parameter =  col_ragged(3), .l = "Symbol"), 
  panel = ".type"
  ) %>% st_asis()

\clearpage

hline from column

stable(
  data,
  hline_from = "STUDY",
) %>% st_asis()

\clearpage

hline anywhere

stable(
  data,
  hline_at = c(3,nrow(data))-1,
) %>% st_asis()

\clearpage

Remove duplicate values

stable(
  data,
  clear_reps = "STUDY",
) %>% st_asis()

\clearpage

Add styling to data frame

tmp <- ptdata()

tmp <- mutate(tmp, STUDY = tex_bold(as.character(STUDY)))
stable(
  tmp, 
  clear_reps = "STUDY", 
  hline_from = "STUDY", 
) %>% st_asis()

\clearpage

Panel

stable(
  data,
  panel = as.panel("STUDY", prefix = "Study number: "),
) %>% st_asis()

\clearpage

Drop column

stable(data[1:3,], drop = "STUDY,FORM,DOSE") %>% st_asis()
stable(data[1:3,], drop = "SCR,FORM,DOSE", panel = "STUDY") %>% st_asis()

\clearpage

Colspan

Basic

stable(
  data,
  span = colgroup("In final model", WT:CRCL),
) %>% st_asis()

\clearpage

Multiple

stable(
  data,
  span = list(
    colgroup("Meh", DOSE:WT),
    colgroup("Hrm", AGE:CRCL), 
    colgroup("Huh", ALB:SCR), 
    colgroup("Expert opinion", CRCL:SCR, level = 2)
  )
) %>% st_asis()

\clearpage

Split from columns

dotdata <- readRDS("datasets/with-dots.RDS")
head(dotdata, n=2)
stable(
  dotdata,
  span_split = colsplit(sep = '.'),
) %>% st_asis()

\clearpage

With titles breaking across lines

You can include ... in the title it will break in to multiple lines. You can alter the split sequence with the span_title_break argument (default is ...) . See ?tab_spanners.

This feature is in addition to the functionality allowing you to break column names (control the split seqence with cols_break, where default is also ...).

stable(
  stdata(), 
  span = colgroup("Final model ... Covariates", WT:SCR), 
  cols_rename = c("Study Protocol ... Number" = "STUDY")
) %>% st_asis()

\clearpage

Handle wide columns

descr is taking over the table

ptable <- readRDS("datasets/ptable.RDS")

stable(ptable) %>% st_asis()

Limit descr to 5 cm

stable(
  ptable, 
  align = cols_center(descr = col_ragged(5))
) %>% st_asis()

\clearpage

Identify a summary row

df.total <- readRDS(file = "datasets/with-total.RDS")
df.total
stable(
  df.total, 
  sumrows = sumrow(
    df.total$STUDY == "all", 
    label = "All Studies", 
    bold = TRUE
  )
) %>% st_asis()

\clearpage

Save the table to file

tab <- stable(data, output_file = "foo.tex")
str(tab)

This can be used to save

stable_save(tab)

file.exists("foo.tex")

\clearpage

Long table

long <- ptdata()
long <- map_dfr(1:2, ~ long) %>% arrange(STUDY,DOSE,FORM)
long %>%
  stable_long(
    panel = "STUDY", cols_bold = TRUE,
    units = ys_get_unit(spec, parens = TRUE),
    notes = "The results look great!",
    clear_reps = "DOSE",
    cols_rename = c(Formulation  = "FORM"),
    r_file = "foo.R", output_file = "../deliv/table/output.tex",
    note_config = noteconf(type = "minipage", width = 0.8,table_skip = 0.2)
  ) %>% st_asis()

\clearpage

Fontsize

data %>% stable(sizes = tab_size(font = "tiny")) %>% st_asis()

\clearpage

Row space

data %>% slice(1:3) %>% stable() %>% st_asis()
data %>% slice(1:3) %>% stable(sizes = tab_size(row = 0.9)) %>% st_asis()
data %>% slice(1:3) %>% stable(sizes = tab_size(row = 2))   %>% st_asis()

\clearpage

Col space

data %>% stable(sizes = tab_size(col = 20)) %>% st_asis()

\clearpage

Still ok

stable(data) %>% st_asis()


metrumresearchgroup/pmtables documentation built on Oct. 27, 2024, 5:16 p.m.