Department of Biostatistics
Vanderbilt University School of Medicine

May 26, 2024

  • System setup
  • To compile report:
    • From the command line: quarto render reportTemplate.qmd --to html
    • To only render html use quarto render reportTemplate.qmd
    • Inside RStudio: press Render from a recent RStudio
  • Commenting/annotation/highlighting
  • Note on markdown tables
    • To keep markdown tables from devoting more space to a column than needed, make the second line of the table look like -|-|-|
    • Use require(qreport) to get the kabl function which makes it easy to combine multiple tables into one block

Thanks to Max Rohde for starting this template.

Code
require(Hmisc)
# Make base graphics look better:
spar <- function(bot=0, left=0, top=0, rt=0)
  par(mar=c(3.25 + bot, 5 + left, 0.5 + top, 0.5 * rt),
      lwd=1.75, mgp=c(2.0, 0.475, 0), tcl=-0.4, xpd=FALSE, ps=14,
      cex.lab=1.15, cex.axis=0.8)
# Set abbreviations for chunk option names
knitr::set_alias(w = 'fig.width', h = 'fig.height')

options(prType='html')   # for certain Hmisc and rms functions
# Get dataset from Dept of Biostatistics dataset repository
getHdata(pbc)
pbc[1:20, 1:8]
   bili albumin stage protime    sex fu.days      age spiders
1  14.5    2.60     4    12.2 female     400 58.76523 present
2   1.1    4.14     3    10.6 female    4500 56.44627 present
3   1.4    3.48     4    12.0   male    1012 70.07255  absent
4   1.8    2.54     4    10.3 female    1925 54.74059 present
5   3.4    3.53     3    10.9 female    1504 38.10541 present
6   0.8    3.98     3    11.0 female    2503 66.25873  absent
7   1.0    4.09     3     9.7 female    1832 55.53457  absent
8   0.3    4.00     3    11.0 female    2466 53.05681  absent
9   3.2    3.08     2    11.0 female    2400 42.50787 present
10 12.6    2.74     4    11.5 female      51 70.55989 present
11  1.4    4.16     4    12.0 female    3762 53.71389 present
12  3.6    3.52     4    13.6 female     304 59.13758 present
13  0.7    3.85     3    10.6 female    3577 45.68925  absent
14  0.8    2.27     4    11.0   male    1217 56.22177  absent
15  0.8    3.87     3    11.0 female    3584 64.64613  absent
16  0.7    3.66     3    10.8 female    3672 40.44353  absent
17  2.7    3.15     4    10.5 female     769 52.18344  absent
18 11.4    2.80     4    12.4 female     131 53.93018 present
19  0.7    3.56     3    11.0 female    4232 49.56057  absent
20  5.1    3.51     4    13.0 female    1356 59.95346  absent
Code
# Take two tables and produce one html section
require(qreport)  # makes kables easy to use and provides various Quarto helpers
kabl(pbc[1:20, 1:3], pbc[21:40, 1:4], caption='Two parts of `pbc` data frame')
bili albumin stage
14.5 2.60 4
1.1 4.14 3
1.4 3.48 4
1.8 2.54 4
3.4 3.53 3
0.8 3.98 3
1.0 4.09 3
0.3 4.00 3
3.2 3.08 2
12.6 2.74 4
1.4 4.16 4
3.6 3.52 4
0.7 3.85 3
0.8 2.27 4
0.8 3.87 3
0.7 3.66 3
2.7 3.15 4
11.4 2.80 4
0.7 3.56 3
5.1 3.51 4
bili albumin stage protime
21 0.6 3.83 4 11.4
22 3.4 3.63 4 11.6
23 17.4 2.94 4 11.7
24 2.1 4.00 2 9.9
25 0.7 4.10 2 11.3
26 5.2 3.68 3 9.9
27 21.6 3.31 4 12.0
28 17.2 3.23 4 13.0
29 0.7 3.78 2 10.6
30 3.6 2.54 4 11.0
31 4.7 3.44 2 10.3
32 1.8 3.34 4 10.6
33 0.8 3.19 3 12.0
34 0.8 3.70 2 10.5
35 1.2 3.20 3 10.6
36 0.3 3.39 2 10.6
37 7.1 3.01 4 12.0
38 3.3 3.53 4 11.0
39 0.7 3.00 4 10.6
40 1.3 3.34 4 11.0
Two parts of pbc data frame

Descriptive Statistics

Code
describe(pbc)
pbc Descriptives
pbc

19 Variables   418 Observations

bili: Serum Bilirubin (mg/dl)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
4180980.9983.2213.742 0.50 0.60 0.80 1.40 3.40 8.0314.00
lowest : 0.3 0.4 0.5 0.6 0.7 , highest: 21.6 22.5 24.5 25.5 28
albumin: Albumin (gm/dl)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
418015413.4970.4732.7502.9673.2433.5303.7704.0104.141
lowest : 1.96 2.1 2.23 2.27 2.31 , highest: 4.3 4.38 4.4 4.52 4.64
stage: Histologic Stage, Ludwig Criteria
image
nmissingdistinctInfoMeanGmd
412640.8933.0240.9519
 Value          1     2     3     4
 Frequency     21    92   155   144
 Proportion 0.051 0.223 0.376 0.350 
For the frequency table, variable is rounded to the nearest 0
protime: Prothrombin Time (sec.)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
4162480.99810.731.029 9.60 9.8010.0010.6011.1012.0012.45
lowest : 9 9.1 9.2 9.3 9.4 , highest: 13.8 14.1 15.2 17.1 18
sex
nmissingdistinct
41802
 Value        male female
 Frequency      44    374
 Proportion  0.105  0.895 

fu.days: Time to Death or Liver Transplantation
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
4180399119181253 245.1 606.81092.81730.02613.53524.24040.6
lowest : 41 43 51 71 77 , highest: 4500 4509 4523 4556 4795
age: Age
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
4180345150.7411.9633.8436.3742.8351.0058.2464.3067.92
lowest : 26.2779 28.8843 29.5551 30.2752 30.5736 , highest: 74.5243 75 75.0116 76.7091 78.4394
spiders
nmissingdistinct
3121062
 Value       absent present
 Frequency      222      90
 Proportion   0.712   0.288 

hepatom
nmissingdistinct
3121062
 Value       absent present
 Frequency      152     160
 Proportion   0.487   0.513 

ascites
nmissingdistinct
3121062
 Value       absent present
 Frequency      288      24
 Proportion   0.923   0.077 

alk.phos: Alkaline Phosphatase (U/liter)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
312106295119831760 599.6 663.0 871.51259.01980.03826.46669.9
lowest : 289 310 369 377 414 , highest: 11046.6 11320.2 11552 12258.8 13862.4
sgot: SGOT (U/ml)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
3121061791122.660.45 54.25 60.45 80.60114.70151.90196.47219.25
lowest : 26.35 28.38 41.85 43.4 45 , highest: 288 299.15 328.6 338 457.25
chol: Cholesterol (mg/dl)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
2841342011369.5194.5188.4213.6249.5309.5400.0560.8674.0
lowest : 120 127 132 149 151 , highest: 1336 1480 1600 1712 1775
trig: Triglycerides (mg/dl)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
2821361461124.764.07 56.00 63.10 84.25108.00151.00195.00230.95
lowest : 33 44 46 49 50 , highest: 319 322 382 432 598
platelet: Platelets (per cm3/1000)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
3081102101261.9107.8117.7139.7199.8257.0322.5386.5430.6
lowest : 62 70 71 79 80 , highest: 493 514 518 539 563
drug
image
nmissingdistinct
41803
 Value      D-penicillamine         placebo  not randomized
 Frequency              154             158             106
 Proportion           0.368           0.378           0.254 

status: Follow-up Status
nmissingdistinctInfoSumMeanGmd
418020.711610.38520.4748

edema
image
nmissingdistinct
41803
 Value                            no edema     edema, no diuretic therapy
 Frequency                             354                             44
 Proportion                          0.847                          0.105
                                          
 Value      edema despite diuretic therapy
 Frequency                              20
 Proportion                          0.048 

copper: Urine Copper (ug/day)
image
nmissingdistinctInfoMeanGmd.05.10.25.50.75.90.95
310108158197.6583.16 17.45 24.00 41.25 73.00123.00208.10249.20
lowest : 4 9 10 11 12 , highest: 412 444 464 558 588

A marginal note goes here. It can be as long as you want and can include raw R output.

Code
spar(top=1)   # allow for title (title produced by default by hist)
with(pbc, hist(albumin, nclass=30))

Histogram of serum albumin
Code
# To just use the caption in the margin, use spar() and hist(..., main='')

Quarto has a nice way of letting you lay out plots. In the following example layout: [[1,1], [1]] in the chunk header1 specifies that there are two rows of plots, with the first row containing two plots and the second only one, with the one made wider to fill the whole space.

1 See this for more information

Code
with(pbc, {
     spar()
     plot(bili, albumin)
     plot(albumin, protime)
     hist(albumin, nclass=25, main='') } )

Running quarto

The yaml header in this template will cause both html and pdf files (throught LaTeX) to be produced. To only produce one output format, run e.g. quarto render my.qmd --to html.

To make your script dual-purpose you can’t always reliably use marginal notes in LaTeX using this quarto syntax:

some text … some text …

So automatically sense which output format is being created and create a character string that will work for both html and LaTeX. For LaTeX, a boxed note in the body of the document will appear.

ishtml <- knitr::is_html_output()
mNote  <- if(ishtml) '.column-margin' else '.callout-note appearance="minimal"'

Then use

Code
# Make a temporary mNote string for illustration
mNote <- '.callout-note appearance="minimal"'

some text … some text …

Computing Environment

Code
# ggplot2 was not actually used, only imported by qreport
grateful::cite_packages(pkgs='Session', output='paragraph', out.dir='.',
    cite.tidyverse=FALSE, omit=c('grateful', 'ggplot2'))

We used R version 4.4.0 (R Core Team 2024) and the following R packages: data.table v. 1.15.4 (Barrett et al. 2024), Hmisc v. 5.1.3 (Harrell Jr 2024), qreport v. 1.0.1 (Harrell 2024).

The code was run on macOS Sonoma 14.4.1.

References

Barrett, Tyson, Matt Dowle, Arun Srinivasan, Jan Gorecki, Michael Chirico, and Toby Hocking. 2024. data.table: Extension of data.frame. https://CRAN.R-project.org/package=data.table.
Harrell, Frank. 2024. qreport: Statistical Reporting with Quarto. https://hbiostat.org/R/qreport/.
Harrell Jr, Frank E. 2024. Hmisc: Harrell Miscellaneous. https://hbiostat.org/R/Hmisc/.
R Core Team. 2024. R: A Language and Environment for Statistical Computing. Vienna, Austria: R Foundation for Statistical Computing. https://www.R-project.org/.