-
Notifications
You must be signed in to change notification settings - Fork 23
/
reptools.r
1430 lines (1281 loc) · 50.6 KB
/
reptools.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
##' Print Named List of Vectors
##'
##' Function to print a simple named list of vectors in html
##' Creates a column `name` from the names of the list
##' If a vector element of the list is numeric, it is rounded
##' to `dec` digits to the right of the decimal place.
##' @title htmlList
##' @param x a named list
##' @param dec round to this decimal place
##' @return a `kable`
##' @author Frank Harrell
htmlList <- function(x, dec=4) {
g <- function(z)
if(is.character(z)) paste(z, collapse=', ') else
paste(round(z, dec), collapse=', ')
w <- sapply(x, g)
d <- data.frame(name=names(w), value=w)
rownames(d) <- NULL
kabl(d, col.names=NULL)
}
##' Front-end to `kable` and `kables`
##' Calls `kable()` if only one table is to be printed.
##' Calls `kable()` for each table and passes it to `kables` if more than one.
##' Accounts for results of `tapply` not being a vector (is an array)
##'
##' @title kabl
##' @param ... one or more objects to pass to `kable`
##' @param caption overall single caption
##' @param digits passed to `kable` and applies to all tables
##' @param col.names passed to `kable`
##' @param row.names passed to `kable`
##' @return result of `kable` or `kables`
##' @author Frank Harrell
##' @md
kabl <- function(..., caption=NULL, digits=4, col.names=NA, row.names=NA) {
w <- list(...)
tr <- function(x)
if(is.vector(x) || (is.array(x) && length(dim(x)) == 1)) t(x) else x
format <- if(knitr::is_latex_output()) 'latex' else 'pipe'
if(length(w) == 1) {
w <- w[[1]]
return( knitr::kable(tr(w), digits=digits, caption=caption,
col.names=col.names, row.names=row.names,
format=format))
}
w <- lapply(w, function(x) knitr::kable(tr(x), digits=digits, format=format))
knitr::kables(w, caption=caption, format=format)
}
##' Create Text for Running Code Chunk
##'
##' Creates text strings suitable for running through `knitr`. The chunk is given a random name because certain operations are not allowed by `knitr` without it.
##' @title makecodechunk
##' @param cmd character string vector of commands to run inside chunk
##' @param opts optional list of chunk options, e.g. `list(fig.width=6, fig.cap="This is a caption")`. See <https://yihui.org/knitr/options> for a complete list of options.
##' @param results format of results, default is `'asis'`. May specify `results='markup'`.
##' @param callout an optional Quarto callout to include after `#|` after the chunk header that affects how the result appears, e.g. `callout='column: margin'`
##' @param h, w optional height and width to place after the chunk header after `#|`
##' @return character vector
##' @author Frank Harrell
makecodechunk <- function(cmd, opts=NULL, results='asis', lang='r',
callout=NULL, h=NULL, w=NULL) {
if(! length(cmd) || (is.character(cmd) && length(cmd) == 1 &&
cmd %in% c('', ' ', "` `"))) return('')
r <- paste0('results="', results, '"')
if(length(opts))
for(oname in names(opts)) {
op <- opts[[oname]]
if(is.character(op)) op <- paste0('"', op, '"')
r <- paste0(r, ',', oname, '=', op)
}
if(! exists('.chunknumber.')) .chunknumber. <<- 0
.chunknumber. <<- .chunknumber. + 1
cname <- paste0('chnk', .chunknumber.)
if(length(callout)) callout <- paste('#|', callout)
if(length(h)) h <- paste('#| fig.height:', h)
if(length(w)) w <- paste('#| fig.width:', w)
c('',
if(lang == 'r') paste0('```{r ', cname, ',', r, ',echo=FALSE}')
else paste0('```{', lang, ' ', cname, '}'),
callout, cmd, h, w, '```', '')
}
##' General Case Handling of Quarto Callouts
##'
##' This function generates and optionally runs markdown/R code that runs Quarto callouts such as collapsible notes or marginal notes.
##' @title makecallout
##' @param x object to print (if `type='print'`), or one or more formulas whose right hand sides are to be run. Left side provides labels if needed by the particular callout, and if `raw` is included on the right side any R code chunk run will have `results='asis'` in the chunk header.
##' @param callout character string giving the Quarto callout
##' @param label character string label if needed and if not obtained from the left side of a formula
##' @param type defaults to `'print'` to print an object. Set to `'run'` to run a chunk or `'cat'` to use `cat()` to render.
##' @param now set to `FALSE` to return code instead of running it
##' @param results if not using formulas, specifies the formatting option to code in the code header, either `'asis'` (the default) or `'markup'`
##' @param close specifies whether to close the callout or to leave it open for future calls
##' @param ... parameters passed to `print`
##' @return if code is not executed, returns a character vector with the code to run
##' @md
##' @author Frank Harrell
makecallout <- function(...) {
## Define internal function to keep new variables from clashing
## with knitted code
build <- function(x, callout=NULL, label=NULL,
type=NULL, now=TRUE, results='asis',
close=length(callout), ...) {
if(! length(type)) type <- 'print'
k <- character(0)
if('formula' %in% class(x)) {
v <- as.character(attr(terms(x), 'variables'))[-1]
if(! attr(terms(x), 'response')) { # no left side variable
label <- NULL
x <- v
} else {
## Get left hand side and remove leading/training backticks
left <- sub('`$', '', sub('^`', '', v[1]))
label <- paste('##', left) # left-hand side
x <- v[-1] # right-hand side
}
raw <- 'raw' %in% x
if(raw) x <- setdiff(x, 'raw')
type <- 'run'
now <- TRUE
results <- if(raw) 'markup' else 'asis'
}
if(length(callout))
k <- c('', paste0('::: {', callout, '}'),
if(! length(label)) '', label)
res <- if(is.character(x) && length(x) == 1 &&
all(x %in% c('', ' ', "` `"))) ' '
else
if(type == 'print') capture.output(print(x, ...))
else
if(type == 'cat') x
else
makecodechunk(x, results=results)
k <- c(k, res, if(close) c(':::', ''))
list(k=k, now=now, type=type)
}
.z. <- build(...)
.k. <- .z.$k
if(.z.$now) {
switch(.z.$type,
print = cat(.k., sep='\n'), # already print()'d
cat = cat(.k., sep='\n'),
run = cat(knitr::knit(text=knitr::knit_expand(text=.k.),
quiet=TRUE)))
return(invisible())
}
.k.
}
##' Print an Object in a Collapsible Note
##'
##' Prints an object in a Quarto collapsible note.
##' @title makecnote
##' @param x an object having a suitable `print` method
##' @param label a character string providing a title for the tab. Default is the name of the argument passed to `makecnote`.
##' @param ... an optional list of arguments to be passed to `print`
##' @return
##' @author Frank Harrell
##' @md
makecnote <- function(x,
label=paste0('`', deparse(substitute(x)), '`'),
wide=FALSE,
type=c('print', 'run', 'cat'),
...) {
type <- match.arg(type)
co <- paste('.callout-note', if(wide) '.column-page', 'collapse="true"')
makecallout(x, callout=co,
label=paste('#', label), type=type, ...)
invisible()
}
##' Put an Object in the Margin
##'
##' Prints an object in a Quarto column margin.
##' @title makecolmarg
##' @param x an object having a suitable `print` method
##' @param ... an optional list of arguments to be passed to `print`
##' @return
##' @author Frank Harrell
##' @md
makecolmarg <- function(x, type=c('print', 'run', 'cat'), ...) {
type <- match.arg(type)
makecallout(x, callout='.column-margin', type=type, ...)
invisible()
}
##' Make Quarto Tabs
##'
##' Loops through a series of formulas or elements of a named list and outputs each element into
##' a separate `Quarto` tab. A `wide` argument is used to expand the width
##' of the output outside the usual margins. An `initblank` argument
##' creates a first tab that is empty, or you can specify a formula `` `` ~ `` ``. This allows one to show nothing
##' until one of the other tabs is clicked. Multiple commands can be run in one chunk by including multiple right hand terms in a formula. A chunk can be marked for producing raw output by including a term `raw` somewhere in the formula's right side. If can be marked for constructing a label and caption by including `+ caption(caption string, label string)`. The tab number is appended to the label string, and if the label is not provided `baselabel` will be used.
##' @title maketabs
##' @param ... a series of formulas or a single named list. For formulas the left side is the tab label (if multiple words or other illegal R expressions enclose in backticks) and the right hand side has expressions to evaluate during chunk execution, plus optional `raw`, `caption`, and `fig.size` options.
##' @param wide
##' @param initblank
##' @param baselabel a one-word character string that provides the base name of `label`s for tabs with figure captions. The sequential tab number is appended to `baselabel` to obtain the full figure label. If using formulas the figure label may instead come from `caption(.., label)`. If not specified it is taken to be the name of the current chunk with `fig-` prepended.
##' @param cap applies to the non-formula use of `maketabs` and is an integer vector specifying which tabs are to be given figure labels and captions.
##' @param basecap a single character string providing the base text for captions if `cap` is specified.
##' @return
##' @author Frank Harrell
# See https://stackoverflow.com/questions/42631642
maketabs <- function(..., wide=FALSE, initblank=FALSE,
baselabel=NULL, cap=NULL, basecap=NULL, debug=FALSE) {
## Put caption() and fig.size() in parent environment so they can
## be executed in that environment so that their argument values
## may be found there
en <- parent.frame()
assign(envir = en, 'caption',
function(cap, label=NULL) list(label=label, cap=cap) )
assign(envir = en, 'fig.size',
function(width=NULL, height=NULL, column=NULL)
list(width=width, height=height, column=column) )
fs <- list(...)
if(length(fs) == 1 && 'formula' %nin% class(fs[[1]])) {
fs <- fs[[1]] # undo list(...) and get to 1st arg to maketabs
.fs. <<- fs
}
if(! length(baselabel))
baselabel <- knitr::opts_current$get('label')
else if(baselabel == 'none') baselabel <- NULL
if(length(baselabel) && ! grepl('^fig-', baselabel))
baselabel <- paste0('fig-', baselabel)
yaml <- paste0('.panel-tabset', if(wide) ' .column-page')
k <- c('', paste0('::: {', yaml, '}'), '')
if(initblank) k <- c(k, '', '## ', '')
for(i in 1 : length(fs)) {
label <- baselabel
capt <- NULL
size <- NULL
f <- fs[[i]]
isform <- FALSE
if('formula' %in% class(f)) {
isform <- TRUE
capt <- NULL
v <- as.character(attr(terms(f), 'variables'))[-1]
y <- v[1] # left-hand side
y <- gsub('`', '', y)
x <- v[-1] # right-hand side
raw <- 'raw' %in% x
if(raw) x <- setdiff(x, 'raw')
## process caption(..., ...)
jc <- grep('caption\\(', x)
if(length(jc)) {
capt <- eval(parse(text=x[jc]), en)
if(length(capt$label)) label <- capt$label
capt <- capt$cap
x <- x[- jc]
}
## process fig.size(...)
sz <- grep('fig.size\\(', x)
if(length(sz)) {
siz <- eval(parse(text=x[sz]), en)
if(length(siz$width))
size <- paste('fig-width:', siz$width)
if(length(siz$height))
size <- c(size, paste('fig-height:', siz$height))
if(length(siz$column))
size <- c(size, paste('column:', siz$column))
x <- x[- sz]
}
} else {
raw <- FALSE
y <- names(fs)[i]
x <- paste0('.fs.[[', i, ']]')
if(i %in% cap) capt <- basecap
}
r <- paste0('results="',
if(raw) 'markup' else 'asis',
'"')
callout <- NULL
if(length(label) && length(capt)) {
lab <- paste0(label, i)
callout <- c(paste0('label: ', lab),
paste0('fig-cap: "', capt, '"'))
addCap(lab, capt)
}
if(length(size)) callout <- c(callout, size)
k <- c(k, '', paste('##', y), '',
makecodechunk(x, callout=callout,
results=if(raw) 'markup' else 'asis'))
}
k <- c(k, ':::', '')
if(debug) cat(k, sep='\n', file='/tmp/z', append=TRUE)
cat(knitr::knit(text=k, quiet=TRUE))
return(invisible())
}
##' Convert Objects to HTML and View
##'
##' Converts a series of objects created to html.
##' Displays these in the RStudio View pane.
##' If RStudio is not running displays in an external browser.
##' Assumes there is an `html` method for the objects (e.g., objects
##' are result of `Hmisc::describe` or `Hmisc::contents`.
##' User can page through the different outputs with the arrow keys
##' in the RStudio View pane
##' @title htmlView
##' @param ... any number of objects for which an `html` method exists
##' @return
##' @author Frank Harrell
##' @md
##' @examples
##' \dontrun{
##' htmlView(contents(d1), contents(d2))
##' htmlView(describe(d1), describe(d2, descript='Second Dataset'))
##' htmlView(contents(d), describe(d))
##' }
htmlView <- function(...) {
viewer <- getOption('viewer', default=utils::browseURL)
w <- list(...)
td <- tempdir()
if(! dir.exists(td)) dir.create(td)
for(u in w) {
fi <- paste0(tempfile(), '.html')
h <- html(u)
writeLines(as.character(h), fi)
viewer(fi)
}
}
##' Convert to HTML and Eternally View Objects
##'
##' `htmlViewx` is similar to `htmlView` except that an external viewer is
##' launched, and the first object is opened in a new window.
##' Subsequent objects are opened in a new tab in the last created
##' window. Set `options(vbrowser='command line to run browser')`
##' to use a browser other than `Vivaldi`.
##' Defaults to opening a new window for only the first object, and adding
##' tabs after that.
##' @title htmlViewx
##' @param ... a series of objects for which an `html` method exists
##' @param tab set to `'all'` to add even the first object to an existing window.
##' @return
##' @author Frank Harrell
htmlViewx <- function(..., tab=c('notfirst', 'all', 'none')) {
tab <- match.arg(tab)
view <- getOption('vbrowser')
if(! length(view)) view <- 'vivaldi'
w <- list(...)
td <- tempdir()
if(! dir.exists(td)) dir.create(td)
i <- 0
for(u in w) {
i <- i + 1
fi <- paste0(tempfile(), '.html')
h <- html(u)
writeLines(as.character(h), fi)
cmd <- paste0(view,
switch(tab,
all = ' -new-tab',
none = ' -new-window',
notfirst = if(i == 1) ' -new-window' else ' -new-tab' ))
browseURL(fi, browser=cmd)
}
}
##' View Rscripts Help Files in Viewer
##'
##' Displays html help files for <https://github.com/harrelfe/rscripts> functions in the RStudio View pane. These help files are found in <https://hbiostat.org/R/rscripts>.
##' If RStudio is not running, displays in an external browser.
##' @title rsHelp
##' @param fun unquoted name of function
##' @return
##' @author Frank Harrell
##' @md
##' @examples
##' \dontrun{
##' rsView(dataChk)
##' }
rsHelp <- function(fun) {
fun <- as.character(substitute(fun))
viewer <- getOption('viewer', default=utils::browseURL)
infile <- paste0('https://hbiostat.org/R/rscripts/', fun, '.html')
td <- tempdir()
if(! dir.exists(td)) dir.create(td)
fi <- paste0(tempfile(), '.html')
download.file(infile, fi, quiet=TRUE)
viewer(fi)
}
##' Run a Series of Data Checks and Report
##'
##' Function to run various data checks on a data table.
##'
##' Checks are run separately for each part of the `expression` vector `checks`. For each single expression, the variables listed in the output are all the variables mentioned in the expression plus optional variables whose names are in the character vector `id`. `%between% c(a,b)` in expressions is printed as `[a,b]`. The output format is plain text unless `html=TRUE` which also puts each table in a separate Quarto tab. See [here](https://www.fharrell.com/post/rflow) for examples.
##' @title dataChk
##' @param d a data table
##' @param checks a vector of expressions that if satisfied causes records to be listed
##' @param id option vector of variable names to serve as IDs
##' @param html set to `TRUE` to create HTML output and put each check in a separate tab, also creating summary tabs
##' @param omit0 set to `TRUE` to ignore checks finding no observations
##' @param byid if `id` is given set `byid=TRUE` to also list a data frame with all flagged conditions, sorted by `id`
##' @param nrows maximum number of rows to allow to be printed
##' @return an invisible data frame containing variables `check` (the expression checked) and `n` (the number of records satisfying the expression)
##' @author Frank Harrell
##' @md
dataChk <- function(d, checks, id=character(0),
html=FALSE, omit0=FALSE, byid=FALSE, nrows=500) {
if(byid && length(id) < 1) stop('must specify id when byid=TRUE')
s <- NULL
X <- Dat <- list()
dashes <- paste0(rep('-', getOption('width')), collapse='')
fmt <- if(html)
function(name, data)
htmltools::HTML(c('<pre>', paste(data, '\n'),
'</pre>'))
else function(name, data) c(dashes, name, dashes, data)
for(i in 1 : length(checks)) {
x <- checks[i]
cx <- as.character(x)
cx <- gsub('%between% c\\((.*?)\\)', '[\\1]', cx)
form <- as.formula(paste('~', cx))
## Find all variables mentioned in expression
vars.involved <- all.vars(form)
z <- d[eval(x), c(id, vars.involved), with=FALSE]
no <- nrow(z)
if(byid && no > 0) {
Da <- z[, id, with=FALSE]
Da[, Check := cx]
Da[, Values := do.call(paste, z[, vars.involved, with=FALSE])]
Dat[[cx]] <- Da
}
z <- if(no == 0) 'n=0' else capture.output(print(z, nrows=nrows))
z <- fmt(cx, z)
if(no > 0 || ! omit0) X[[cx]] <- z
s <- rbind(s, data.frame(Check=cx, n=no))
}
if(byid) {
Dat <- rbindlist(Dat, fill=TRUE)
# setcolorder(Dat, c(id, 'Check', setdiff(names(Dat), c(id, 'Check'))))
setkeyv(Dat, id)
u <- paste('By', paste(id, collapse=', '))
X[[u]] <- fmt(u, capture.output(print(Dat, nrows=nrows)))
}
X$Summary <- fmt('Summary', capture.output(print(s, nrows=nrows)))
if(html) maketabs(X, initblank=TRUE)
else for(z in X) cat(z, sep='\n')
if(byid) invisible(s) else invisible(Dat)
}
##' Separate Chunk Plot
##'
##' Runs a plot on its own `Rmarkdown/Quarto` `knitr` Chunk. The plot will have its own caption and size, and short captions are placed in the markdown TOC
##'
##' Expressions cannot be re-used, i.e., each expression must evaluate to the right quantity after the chunk in which the `scplot` calls are made is finished, and the new constructed chunk is input. To input and run the constructed chunk:
##' `{r child='scplot.Rmd'}` preceeded and following by 3 back ticks.
##' Hmisc::putHcap is used to markup regular and short captions `cap, scap`. Short caption appears in TOC. If no `scap`, then `cap` is used for this. To change the `putHcap` `subsub` argument set `options(scplot.subsub='## ')` for example.
##' @title scplot
##' @param command an command that causes a plot to be rendered
##' @param cap long caption
##' @param scap short caption
##' @param w width of plot
##' @param h height of plot
##' @param id a string ID for the plot. Defaults to the current chunk label if `knitr` is running
##' @return
##' @author Frank Harrell
##' @md
##' @examples
##' \dontrun{
##' scplot(id='chunkid') # initialize output file scplot.Rmd
##' # or use scplot() to use the current chunk name as the id
##' # scplot(plotting expression, caption, optional short caption, w, h)
##' # scplot(plotting expression ...)
##' }
scplot <- function(command, cap=NULL, scap=NULL, w=5, h=4, id=NULL) {
command <- as.character(sys.call())
if(length(command) == 1) id <- knitr::opts_current$get('label')
if(length(id)) {
cat('', sep='', file='scplot.Rmd')
.iscplot. <<- 0
.idscplot. <<- id
return(invisible())
}
.iscplot. <<- .iscplot. + 1
cname <- paste0(.idscplot., .iscplot.)
subsub <- if(length(.Options$scplot.subsub)) .Options$scplot.subsub else TRUE
label <- Hmisc::putHcap(cap, scap=scap, subsub=subsub, file=FALSE)
k <- c(paste0('\n\n```{r ', cname, ',results="asis",echo=FALSE,fig.width=',
w, ',fig.height=', h, '}\n'), paste0(command[2], '\n```\n\n'))
cat(label, k, sep='', file='scplot.Rmd', append=TRUE)
invisible()
}
missChk <- function(data, use=NULL, exclude=NULL,
type=c('report', 'seq'),
maxpat=15, maxcomb=25, excl1pat=TRUE,
sortpatterns=TRUE,
prednmiss=FALSE, omitpred=NULL,
baselabel=NULL, ...) {
type <- match.arg(type)
cargs <- list(...)
namedata <- deparse(substitute(data))
prtype <- .Options$prType
d <- copy(data)
setDT(d)
if(length(use)) {
if(inherits(use, 'formula')) use <- all.vars(use)
d <- d[, ..use]
}
if(length(exclude)) {
if(inherits(exclude, 'formula')) exclude <- all.vars(exclude)
use <- setdiff(names(d), exclude)
d <- d[, ..use]
}
p <- ncol(d)
ismiss <- function(x)
if(is.character(x) | is.factor(x))
is.na(x) | trimws(x) == '' else is.na(x)
## Replace each variable with missingness indicator
di <- d[, lapply(.SD, ismiss)]
## Hierarchical exclusions
exc <- do.call('seqFreq', c(di, list(noneNA=TRUE)))
if(type == 'seq') return(exc)
na.per.var <- apply(di, 2, sum)
na.per.obs <- apply(di, 1, sum)
if(all(na.per.var == 0))
return(asisOut('No NAs on any of the',
p, ' variables examined.'))
surrq <- function(x) paste0('`', x, '`')
vmiss <- names(na.per.var)[na.per.var > 0]
dm <- d[, ..vmiss]
pm <- length(vmiss)
cat('\n', p - pm, 'variables have no NAs and', pm,
'variables have NAs\n\n')
cat(namedata, ' has ', nrow(d), ' observations (', sum(na.per.obs == 0),
' complete) and ', ncol(d), ' variables (', sum(na.per.var == 0),
' complete)\n', sep='')
if(sum(na.per.var) > 0) {
z <- data.frame(Minimum=c(min(na.per.var), min(na.per.obs)),
Maximum=c(max(na.per.var), max(na.per.obs)),
Mean =round(c(mean(na.per.var), mean(na.per.obs)), 1),
row.names=c('Per variable', 'Per observation'))
print(kabl(z, caption='Number of NAs'))
tab <- table(na.per.var)
print(kabl(tab,
caption='Frequency distribution of number of NAs per variable'))
tab <- table(na.per.obs)
print(kabl(tab,
caption='Frequency distribution of number of incomplete variables per observation'))
}
if(pm < max(20, maxpat)) {
nap <- na.pattern(dm)
nap <- matrix(nap, ncol=1, dimnames=list(names(nap), 'Count'))
n1 <- sum(nap[,1] == 1)
patex <- ''
if(excl1pat && n1 > 0) {
patex <- paste(n1,
'patterns with frequency of 1 not listed\n')
nap <- nap[nap[,1] > 1, 1, drop=FALSE]
}
if(nrow(nap) <= maxpat) {
cat('Frequency counts of all combinations of NAs\n\n',
'Variables in column order are:',
paste(surrq(vmiss), collapse=', '), '\n\n', patex,
sep='')
if(sortpatterns) {
i <- order(- nap[, 1])
nap <- nap[i, , drop=FALSE]
}
print(kabl(nap))
return(invisible())
}
}
.naclus. <<- naclus(dm)
abb <- pm > 40
ptypes <- c('na per var' = 'NAs/var',
'na per obs' = 'NAs/obs',
'mean na' = 'Mean # var NA when other var NA',
'na per var vs mean na' =
'NAs/var vs mean # other variables NA',
'clus' = 'Clustering of missingness')
tabs <- vector('list', length(ptypes))
for(i in seq(ptypes)) {
cap <- if(i == 1) paste0('+ caption("Missing data patterns in `',
namedata, '`"',
if(length(baselabel))
paste0(', "', baselabel, '"'),
')')
lab <- surrq(ptypes[i])
f <- if(i < 5) paste0(lab, ' ~ ',
'naplot(.naclus., which="', names(ptypes[i]),
'")', cap)
else
paste0(lab, ' ~ ', 'plot(.naclus., abbrev=', abb, ')')
tabs[[i]] <- as.formula(f)
}
## Add tab for sequential NA exclusions
.seqmisstab. <<- table(exc)
tabs <- c(tabs, Sequential ~
kabl(.seqmisstab.,
caption='Sequential frequency-ordered exclusions due to NAs'))
dm <- dm[, lapply(.SD, is.na)]
## Produce combination plot for the right number of variables with NAs
if(pm <= maxcomb) {
.combplotp. <<- do.call('combplotp', c(list(data=dm, maxcomb=maxcomb), cargs))
tabs <- c(tabs,
`NA combinations` ~ .combplotp.)
}
if(prednmiss && (pm < p) && any(na.per.var == 0)) {
## Predict using ordinal regression the number of missing variables
## from all the non-missing variables
## Compute the number of missing variables per observation
preds <- names(na.per.var)[na.per.var == 0]
if(length(omitpred)) {
omitv <- if(is.character(omitpred)) omitpred
else
all.vars(omitpred)
preds <- setdiff(preds, omitv)
}
form <- paste('na.per.obs ~', paste(preds, collapse=' + '))
f <- rms::lrm(as.formula(form), data=d)
if(f$fail)
cat('prednmiss=TRUE led to failure of ordinal model to fit\n\n')
else {
.misschkanova. <<- anova(f)
.misschkfit. <<- f
options(prType='html')
tabs <- c(tabs,
`Predicting # NAs per obs` ~ print(.misschkfit., coefs=FALSE) +
plot(.misschkanova.))
}
}
do.call(maketabs, c(list(initblank=TRUE, baselabel=baselabel),
list(tabs)))
options(prType=prtype)
}
varType <- function(data, include=NULL, exclude=NULL,
ndistinct=10, nnonnum=20) {
# include: vector of variables in data to attend to
# exclude: attend to all variables in data except those in exclude
# include and exclude can be vectors or formulas with only right sides
g <- function(x) {
nnum <- is.character(x) || is.factor(x)
lu <- length(unique(x))
fcase(! nnum && lu >= ndistinct, 'continuous',
nnum && lu > nnonnum, 'nonnumeric',
default = 'discrete')
}
if(! is.data.frame(data)) return(g(data))
if(length(include) && ! is.character(include))
include <- all.vars(include)
if(length(exclude) && ! is.character(exclude))
exclude <- all.vars(exclude)
v <- if(length(include)) include else setdiff(names(data), exclude)
s <- sapply(if(is.data.table(data)) data[, ..v] else data[v], g)
split(names(s), s)
}
conVars <- function(...) varType(...)$continuous
disVars <- function(...) varType(...)$discrete
asForm <- function(x) as.formula(paste('~', paste(x, collapse=' + ')))
makemermaid <- function(.object., ..., file) {
code <- strsplit(.object., '\n')[[1]]
ki <- knitr::knit_expand
etext <- do.call('ki', c(list(text=code), list(...)))
cat(etext, sep='\n', file=file)
invisible()
}
makegraphviz <- function(.object., ..., file) {
x <- strsplit(.object., '\n')[[1]]
# Translate `foo` to <font color='darkblue' face='courier'>foo</font>
# face=Lucida Console resulted in overwriting of text
code <- gsub('`(.*?)`', "<font color='darkblue' face='courier'>\\1 </font>", x)
ki <- knitr::knit_expand
dotlist <- list(...)
L <- length(dotlist)
# Function to strip off style info done by html.data.frame
mtab <- function(d) {
k <- ncol(d)
w <- matrix('', nrow=nrow(d) + 1, ncol=k)
w[1, ] <- paste0('<td><font color="darkblue"><b>', names(d),
'</b></font></td>')
for(i in 1 : k) {
di <- d[[i]]
f <- trimws(format(di))
w[-1, i] <- f
align <- if(length(unique(nchar(f))) == 1) 'CENTER'
else if(is.numeric(di)) 'RIGHT' else 'LEFT'
w[-1, i] <- paste0('<td ALIGN="', align, '">',
w[-1, i], '</td>')
}
w <- apply(w, 1, function(x) paste0('<tr>',
paste(x, collapse=''), '</tr>'))
c('<table border="0" cellborder="0" cellspacing="0">',
w[1], '<HR/>', w[-1], '</table>')
}
if(L) for(i in 1 : L)
if(is.data.frame(dotlist[[i]]))
dotlist[[i]] <- mtab(dotlist[[i]])
etext <- do.call('ki', c(list(text=code), dotlist))
cat(etext, sep='\n', file=file)
invisible()
}
vClus <- function(d, exclude=NULL, corrmatrix=FALSE,
fracmiss=0.2, maxlevels=10, minprev=0.05,
horiz=FALSE, label='fig-varclus', print=TRUE) {
w <- as.data.frame(d) # needed by dataframeReduce
if(length(exclude)) w <- w[setdiff(names(w), exclude)]
w <- dataframeReduce(w, fracmiss=fracmiss, maxlevels=maxlevels,
minprev=minprev, print=FALSE)
if(print) print(kabl(attr(w, 'info'),
caption='Variables removed or modified'))
form <- as.formula(paste('~', paste(names(w), collapse=' + ')))
v <- varclus(form, data=w)
if(! corrmatrix) {
if(horiz) plot(as.dendrogram(v$hclust), horiz=TRUE)
else plot(v)
return()
}
.varclus. <<- v
rho <- varclus(form, data=w, trans='none')$sim
.varclus.gg. <<- plotCorrM(rho, xangle=90)[[1]]
cap <- 'Spearman rank correlation matrix. Positive correlations are blue and negative are red.'
form1 <- `Correlation Matrix` ~ .varclus.gg. + caption(cap, label=label) +
fig.size(width=9.25, height=8.5)
form2 <- `Variable Clustering` ~
plot(as.dendrogram(.varclus.$hclus), horiz=TRUE) +
fig.size(height=4.5, width=7.5)
form3 <- `Variable Clustering` ~ plot(.varclus.) +
fig.size(height=5.5, width=7.5)
if(horiz) maketabs(form1, form2, initblank=TRUE)
else
maketabs(form1, form3, initblank=TRUE)
}
dataOverview <- function(d, d2=NULL, id=NULL,
plot=c('scatter', 'dot', 'none'),
pr=nvar <= 50, which=1, dec=3, baselabel=NULL) {
nam1 <- deparse(substitute(d ))
nam2 <- if(length(d2)) deparse(substitute(d2))
plot <- match.arg(plot)
if(which == 2 && ! length(d2))
stop('which=2 only applies when second dataset is provided')
d <- copy(d)
setDT(d)
if(length(d2)) {
d2 <- copy(d2)
setDT(d2)
}
## From rmsb package, augmented to handle dates/times:
distSym <- function(x, prob=0.9, na.rm=FALSE) {
if(na.rm) x <- x[! is.na(x)]
x <- unclass(x)
a <- (1. - prob) / 2.
w <- quantile(x, probs=c(a / 2., 1. - a / 2.))
xbar <- mean(x)
as.vector((w[2] - xbar) / (xbar - w[1]))
}
lun <- function(x) length(unique(x))
id1 <- id2 <- FALSE
ids1 <- ids2 <- NULL
idv <- if(length(id)) all.vars(id)
nid <- if(length(idv) == 1) paste('ID variable', idv)
else
paste('ID variables', paste(idv, collapse=' '))
if(length(id)) {
id1 <- all(idv %in% names(d))
if(id1)
ids1 <- unique(d[, do.call(paste0, .SD), .SDcols=idv])
if(length(d2)) {
id2 <- all(idv %in% names(d2))
if(id2) ids2 <- unique(d2[, do.call(paste0, .SD), .SDcols=idv])
}
}
ismiss <- function(x)
if(is.character(x) | is.factor(x))
is.na(x) | trimws(x) == '' else is.na(x)
na <- sapply(d, ismiss) * 1
na.per.var <- apply(na, 2, sum)
na.per.obs <- apply(na, 1, sum)
cat(nam1, ' has ', nrow(d), ' observations (', sum(na.per.obs == 0),
' complete) and ', ncol(d), ' variables (', sum(na.per.var == 0),
' complete)', sep='')
if(length(d2)) {
vcommon <- sum(names(d) %in% names(d2))
cat(' of which', vcommon, 'variables are in', nam2)
} else cat('\n')
if(id1) {
cat('There are', length(ids1), 'unique values of', nid, 'in', nam1)
if(length(d2) && id2) {
ncommon <- sum(ids1 %in% ids2)
cat(' with', ncommon, 'unique IDs also found in', nam2)
}
cat('\n')
}
if(length(d2)) {
cat(nam2, 'has', nrow(d2), 'observations and', ncol(d2), 'variables')
vcommon <- sum(names(d2) %in% names(d))
cat(' of which', vcommon, 'are in', nam1, '\n')
if(id2) {
cat('There are', length(ids2), 'unique values of', nid, 'in', nam2, '\n')
if(id1) {
ncommon <- sum(ids2 %in% ids1)
cat(ncommon, 'unique IDs are found in', nam1, '\n\n')
}
}
}
## Get variables types
w <- switch(which, d, d2)
nvar <- ncol(w)
g <- function(x) {
type <- varType(x)
info <- NA
distinct <- lun(x)
if(type == 'continuous') {
sym <- distSym(x, na.rm=TRUE)
x <- round(x, dec)
}
tab <- table(x)
n <- sum(tab)
fp <- tab / n
if(type != 'continuous') sym <- 1. - mean(abs(fp - 1. / length(tab)))
info <- if(distinct < 2) 0 else (1 - sum(fp ^ 3)) / (1 - 1 / n / n)
low <- which.min(tab)
hi <- which.max(tab)
list(type = upFirst(type),
distinct = distinct,
info = info,
symmetry = sym,
NAs = sum(is.na(x)),
mincat = names(tab)[low],
mincatfreq = unname(tab[low]),
maxcat = names(tab)[hi],
maxcatfreq = unname(tab[hi]))
}
z <- lapply(w, g)
r <- rbindlist(z, idcol='variable')
if(pr) {
s <- copy(r)
setnames(s, .q(variable, type, distinct, info, symmetry, NAs, mincat,
mincatfreq, maxcat, maxcatfreq),
.q(Variable, Type, Distinct, Info, Symmetry, NAs, "Rarest Value",
"Frequency of Rarest Value", Mode, "Frequency of Mode"))
print(kabl(s, digits=3))
}
if(plot == 'none') return(invisible())
breaks <- function(mf) {
br <- pretty(c(0, mf), 10)
mbr <- c(0, 10, 20, 30, 40, 50, 100, if(mf >= 200) seq(200, mf, by=100))
mbr <- mbr[mbr < mf]
mbr <- setdiff(mbr, br)
list(br=br, mbr=mbr)
}
if(plot == 'dot') {
r <- r[, .(variable, type, distinct, NAs, mincatfreq, maxcatfreq)]
m <- melt(r, id.vars=c('variable', 'type'),
variable.name='what', value.name='Freq')
s <- split(m, m$type)
b <- breaks(max(m$Freq))
br <- b$br; mbr <- b$mbr
gg <- function(data)
ggplot(data, aes(y=variable, x=Freq, col=what)) + geom_point() +
scale_x_continuous(trans='sqrt', breaks=br,
minor_breaks=mbr) +
xlab('') + ylab('Frequency') +
guides(color=guide_legend(title='')) +
theme(legend.position='bottom')
g <- lapply(s, gg)
} else if(plot == 'scatter') {
r[, txt := paste(variable,
paste('distinct values:', distinct),
paste('NAs:', NAs),
paste('Info:', round(info, 3)),
paste('Symmetry:', round(symmetry, 3)),
paste0('lowest frequency (', mincatfreq, ') value:',
mincat),
paste0('highest frequency (', maxcatfreq, ') value:',
maxcat), sep='<br>')]
b <- breaks(max(r$distinct))
br <- b$br; mbr <- b$mbr
gg <- function(data) {
cap <- paste(nrow(data), 'variables and', nrow(w),
'observations\nNumber of NAs is color coded')
lnna <- levels(data$.nna.)
ggplotlyr(
if(any(trimws(as.character(data$.nna.)) != '0'))
ggplot(data, aes(x=distinct, y=symmetry,
color=as.integer(.nna.), label=txt)) +
scale_x_continuous(trans='sqrt', breaks=br,
minor_breaks=mbr) +
scale_color_gradientn(colors=viridis::viridis(min(10, length(lnna))),
breaks=1 : length(lnna),
labels=lnna) +
geom_point() + xlab('Number of Distinct Values') +
ylab('Symmetry') +
labs(caption=cap) +
guides(color=guide_legend(title='NAs'))
else
ggplot(data, aes(x=distinct, y=symmetry,
label=txt)) +
scale_x_continuous(trans='sqrt', breaks=br,
minor_breaks=mbr) +
geom_point() + xlab('Number of Distinct Values') +
ylab('Symmetry') +
labs(caption=cap)
)
# theme(legend.position='bottom')
}
r[, .nna. := cut2(NAs, g=12)]
s <- split(r, r$type)
g <- lapply(s, gg)
}
print(kabl(r[, paste(levels(.nna.), collapse=' ')],
'Intervals of frequencies of NAs used for color-coding plots'))
if(plot == 'scatter') {
cap <- paste('Plot of the degree of symmetry of the distribution of a variable (value of 1.0 is most symmetric) vs. the number of distinct values of the variable. Hover over a point to see the variable name and detailed characteristics.')
maketabs(g, initblank=TRUE, basecap=cap, cap=1)
}
else maketabs(g, initblank=TRUE)