Author
Affilliation

Vanderbilt University Department of Biostatistics

Published

April 27, 2022

  • System setup
  • To compile report:
    • From the command line: 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(Hmisc); getRs('kable.r', put='source') 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
getRs('kable.r', put='source')  # makes kables easy to use
kabl(pbc[1:20, 1:3], pbc[21:40, 1:4], caption='Two parts of `pbc` data frame')
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

Descriptive Statistics

Code
html(describe(pbc))
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.0
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.10 2.23 2.27 2.31 , highest: 4.30 4.38 4.40 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
 

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.0 9.1 9.2 9.3 9.4 , highest: 13.8 14.1 15.2 17.1 18.0
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.27789 28.88433 29.55510 30.27515 30.57358 , highest: 74.52430 75.00000 75.01164 76.70910 78.43943
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.0 310.0 369.0 377.0 414.0 , highest: 11046.6 11320.2 11552.0 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.40 45.00 , highest: 288.00 299.15 328.60 338.00 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))

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

     R version 4.1.3 (2022-03-10)
     Platform: x86_64-pc-linux-gnu (64-bit)
     Running under: Pop!_OS 21.10
     
     Matrix products: default
     BLAS:   /usr/lib/x86_64-linux-gnu/blas/libblas.so.3.9.0
     LAPACK: /usr/lib/x86_64-linux-gnu/lapack/liblapack.so.3.9.0
     
     attached base packages:
     [1] stats     graphics  grDevices utils     datasets  methods   base     
     
     other attached packages:
     [1] Hmisc_4.7-0     ggplot2_3.3.3   Formula_1.2-4   survival_3.2-13
     [5] lattice_0.20-45
     
    To cite R in publications use:

    R Core Team (2022). R: A Language and Environment for Statistical Computing. R Foundation for Statistical Computing, Vienna, Austria. https://www.R-project.org/.

    To cite the Hmisc package in publications use:

    Harrell Jr F (2022). Hmisc: Harrell Miscellaneous. R package version 4.7-0, https://hbiostat.org/R/Hmisc/.

    To cite the ggplot2 package in publications use:

    Wickham H (2016). ggplot2: Elegant Graphics for Data Analysis. Springer-Verlag New York. ISBN 978-3-319-24277-4, https://ggplot2.tidyverse.org.

    To cite the survival package in publications use:

    Therneau T (2021). A Package for Survival Analysis in R. R package version 3.2-13, https://CRAN.R-project.org/package=survival.