library(dplyr)

\clearpage

Data

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

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

data <- pmt_summarized
head(data)

\clearpage

Simple table

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

\clearpage

Annotate

data %>% 
  st_data() %>%
  st_files(r = "foo.R", output = "foo.tex") %>%
  st_notes(
    "Data were analyzed in quadruplicate.", 
    "The results are very clear."
  ) %>% stable() %>% st_asis()

\clearpage

Notes in minipage

data %>% 
  st_new() %>% 
  st_files("foo.R", "foo.tex") %>% 
  st_noteconf(type = "minipage", width = 0.85) %>%
  st_notes(
    "Data were analyzed in quadruplicate.", 
    "The results are very clear."
  ) %>% stable() %>% st_asis()

\clearpage

Align

data %>% 
  st_new() %>% 
  st_align("c", STUDY = 'l', .r = "DOSE,SCR") %>% 
  stable() %>% st_asis()

\clearpage

Units

data %>% 
  st_new() %>% 
  st_center(STUDY = 'l', .r = "DOSE,SCR") %>% 
  st_units(WT = "kg", SCR = "mg/dL", DOSE = "mg") %>%
  stable() %>% st_asis()

Alternatively

units <- ys_get_unit(spec, parens = TRUE)

data[1:3,] %>% 
  st_new() %>% 
  st_center(STUDY = 'l', .r = "DOSE,SCR") %>% 
  st_units(units) %>%
  stable() %>% st_asis()

\clearpage

Expand header rows

data %>% 
  st_new() %>% 
  st_rename("Study...Number" = STUDY, "Serum...Albumin" = ALB) %>%
  st_units(units) %>%
  stable() %>% st_asis()

\clearpage

Math

ptab <- readRDS("datasets/ptab.RDS")
ptab
ptab %>% 
  st_data() %>% 
  st_center(Parameter = col_ragged(3), .l = "Symbol") %>%
  st_panel(".type") %>% 
  stable() %>% st_asis()

\clearpage

hline from column

st_new(data) %>% 
  st_hline(from = "STUDY") %>% 
  stable() %>% st_asis()

\clearpage

hline anywhere

st_new(data) %>% 
  st_hline(at = c(3,nrow(data))-1) %>%
  stable() %>% st_asis()

\clearpage

hline pattern

tmp <- readRDS("datasets/with-total.RDS")

st_new(tmp) %>% 
  st_hline(pattern = "all", cols = "STUDY", n = 2) %>% 
  stable() %>% st_asis()

\clearpage

hline multiple

tmp <- readRDS("datasets/with-total.RDS")

st_new(tmp) %>% 
  st_hline(at = 3, n = 2) %>% 
  stable() %>% st_asis()

\clearpage

Mark a summary row (cf sumrows)

tmp <- readRDS("datasets/with-total.RDS")

st_new(tmp) %>% 
  st_hline(at = 3, n = 2) %>% 
  st_bold(cols = "STUDY", pattern = "all") %>%
  st_edit(pattern = "all", replacement = "All studies") %>%
  stable() %>% st_asis()

\clearpage

Remove duplicate values

data %>% 
  st_new() %>% 
  st_hline(from = "STUDY") %>% 
  st_clear_reps("STUDY") %>% 
  stable() %>% st_asis()

\clearpage

Add styling to data frame

tmp <- ptdata() 
tmp <- mutate(tmp, STUDY = tex_bold(as.character(STUDY)))
tmp %>% 
  st_new() %>% 
  st_clear_reps(STUDY) %>%
  st_hline(from = "STUDY") %>%
  stable() %>% st_asis()

\clearpage

Add styling in the pipeline

tmp <- ptdata()
tmp %>%
  st_new() %>%
  st_clear_reps(STUDY, .now = TRUE) %>%
  st_hline(pattern = "\\S+", cols = "STUDY") %>%
  st_bold(cols = "STUDY") %>%
  st_it("DOSE") %>%
  st_edit("\\bmale", "dude") %>%
  st_edit("female", "gal") %>%
  stable() %>% st_asis()

\clearpage

Panel

data %>% 
  st_new() %>% 
  st_center(DOSE = 'l', SCR = 'r') %>% 
  st_panel("STUDY", prefix = "Study number: ") %>% 
  stable() %>% st_asis()

\clearpage

Colspan

data %>% 
  st_new() %>% 
  st_left(.c = "WT,AGE,CRCL") %>% 
  st_span("In final model", WT:CRCL) %>% 
  stable() %>% st_asis()

\clearpage

Colspan - multiple

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

\clearpage

Colspan - from cols

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

\clearpage

Handle wide columns

descr is taking over the table

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

ptable %>% st_new() %>% stable() %>% st_asis()

Limit descr to 5 cm

ptable %>% 
  st_new() %>% 
  st_align(descr = col_ragged(5)) %>% 
  stable() %>% st_asis()

\clearpage

Identify a summary row

We can point to one or more rows and style it up as a "summary row"

df.total <- readRDS(file = "datasets/with-total.RDS")
df.total
st_new(df.total) %>% 
  st_sumrow(pattern = "all", label = "All studies", bold = TRUE) %>% 
  stable() %>% st_asis()

\clearpage

Fontsize

data %>% st_new() %>% st_sizes(font = "tiny") %>% stable() %>% st_asis()

\clearpage

Row space

sl <- data %>% slice(1:3)
sl %>% st_new() %>%                         stable() %>% st_asis()
sl %>% st_new() %>% st_sizes(row = 0.9) %>% stable() %>% st_asis()
sl %>% st_new() %>% st_sizes(row = 2  ) %>% stable() %>% st_asis()

\clearpage

Col space

data %>% st_new() %>% st_sizes(col = 20) %>% stable() %>% st_asis()

\clearpage

Render to long table

longd <- bind_rows(data, data, data, data)

longd %>% 
  st_new() %>% 
  st_files(r = "demo-pipe.Rmd") %>%
  st_notes("This example demonstrates how to make long table") %>%
  st_args(lt_cap_text = "Adding a caption to the table") %>% 
  st_args(lt_cap_label = "tab:demographics") %>%
  stable_long() %>% 
  st_asis()

\clearpage

Pass any argument to stable

Rather than passing dots to stable() or stable_long(), you can use st_args(). But note that this is destructive to previous work; it assumes you are completely specifying the contents for a given argument. Note that you can pass arguments through st_args() or directly to stable() (or stable_long()), but you shouldn't need to do both. See this example:

data %>%
  st_new() %>% 
  st_notes("xyz") %>%
  st_args(r_file = "demo-pipe.Rmd", notes = "abc") %>% 
  st_args(output_file = "demo-pipe.tex") %>%
  stable() %>% 
  st_asis()


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