Skip to content

Commit 6e68d73

Browse files
committed
Better number print with big marks #18
1 parent 1c3f4c5 commit 6e68d73

File tree

5 files changed

+108
-8
lines changed

5 files changed

+108
-8
lines changed

CRAN-SUBMISSION

Lines changed: 0 additions & 3 deletions
This file was deleted.

NEWS.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
# consort 1.2.1
2+
3+
- Better numeric format in `gen_text`
4+
- Bug in connection with `build_grviz`
5+
16
# consort 1.2.0
27

38
- Able to have multiple split with `grViz`

R/gen_text.R

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ gen_text <- function(x, label = NULL, bullet = FALSE) {
5959
if(is.data.frame(val)){
6060
r <- box_data.frame(val)
6161
if(!is.null(label)){
62-
lab_lst <- sprintf("%s (n=%i)", label[indx], sum(!is.na(val[[1]])))
62+
lab_lst <- sprintf("%s (n=%s)",
63+
label[indx],
64+
pret_num(sum(!is.na(val[[1]]))))
6365
r <- paste(lab_lst, r, sep = "\n")
6466
}
6567

@@ -85,7 +87,7 @@ box_data.frame <- function(x, label = NULL){
8587
stop("only two columns are supported")
8688

8789
if(!is.null(label))
88-
label <- sprintf("%s (n=%i)", label, sum(!is.na(x[[1]])))
90+
label <- sprintf("%s (n=%s)", label, pret_num(sum(!is.na(x[[1]]))))
8991

9092
r <- sapply(na.omit(unique(x[[1]])), function(i){
9193
box_label(x[[2]][x[[1]] == i], label = i, bullet = TRUE)
@@ -115,15 +117,37 @@ box_label <- function(x, label, bullet = TRUE) {
115117
}
116118

117119
if (is.null(label)) {
118-
return(paste0(names(table(x)), " (n=", table(x), ")"))
120+
if(is.factor(x))
121+
tab <- table(droplevels(x))
122+
else
123+
tab <- table(x)
124+
125+
tp <- paste0(names(tab), " (n=", pret_num(tab), ")")
126+
if(!bullet){
127+
return(tp)
128+
}else{
129+
return(paste0("\u2022 ", paste(tp, collapse = "\n\u2022 ")))
130+
}
131+
119132
}
120133

121-
tp <- paste0(label, " (n=", sum(!is.na(x)), ")")
134+
tp <- paste0(label, " (n=", pret_num(sum(!is.na(x))), ")")
122135

123136
if (bullet) {
124-
txt_sub <- paste0("\u2022 ", names(table(x)), " (n=", table(x), ")")
137+
if(is.factor(x))
138+
tab <- table(droplevels(x))
139+
else
140+
tab <- table(x)
141+
txt_sub <- paste0("\u2022 ", names(tab), " (n=", pret_num(tab), ")")
125142
tp <- paste0(tp, ":\n", paste(txt_sub, collapse = "\n"))
126143
}
127144

128145
return(tp)
129146
}
147+
148+
149+
# Format numbers
150+
#' @keywords internal
151+
pret_num <- function(x){
152+
prettyNum(x, big.mark = ",", preserve.width = "none", scientific = FALSE)
153+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
digraph consort_diagram {
2+
graph [layout = dot]
3+
4+
5+
6+
7+
8+
# node definitions with substituted label text
9+
node [shape = rectangle, fillcolor = Biege, style="", fillcolor = "", color = ""]
10+
11+
node1 [label = "Cohort 1 (n=6)"]
12+
node2 [label = "Cohort 2 (n=6)"]
13+
node3 [label = "Cohort 3 (n=6)"]
14+
node4 [label = "Excluded (n=1)\l"]
15+
node5 [label = "Excluded (n=3)\l"]
16+
node7 [label = "Cohort 1 (n=5)"]
17+
node8 [label = "Cohort 2 (n=3)"]
18+
node10 [label = "Total (n=14)"]
19+
20+
21+
## Invisible point node for joints
22+
23+
node [shape = point, width = 0]
24+
25+
P1 P2 P3 P4 P5
26+
27+
subgraph {
28+
rank = same; rankdir = LR; node1; node2; node3;
29+
}
30+
subgraph {
31+
rank = same; rankdir = LR; P1; node4;
32+
}
33+
subgraph {
34+
rank = same; rankdir = LR; node7; node8; node9;
35+
}
36+
subgraph {
37+
rank = same; rankdir = LR; node4; node5; node6;
38+
}
39+
subgraph {
40+
rank = same; rankdir = LR; P2; node5;
41+
}
42+
subgraph {
43+
rank = same; rankdir = LR; node4; node5;
44+
}
45+
subgraph {
46+
rank = same; rankdir = LR; P3; P4; P5;
47+
}
48+
49+
edge[style=""];
50+
51+
node1 -> P1 [arrowhead = none];
52+
P1 -> node4;
53+
P1 -> node7;
54+
node2 -> P2 [arrowhead = none];
55+
P2 -> node5;
56+
P2 -> node8;
57+
P4 -> node10;
58+
node7 -> P3 [arrowhead = none];
59+
node8 -> P4 [arrowhead = none];
60+
node9 -> P5 [arrowhead = none];
61+
P3 -> P4 -> P5 [arrowhead = none, minlen = 10];
62+
63+
node3 -> node9 [arrowhead = none];
64+
65+
66+
}

tests/testthat/test-gen_text.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,13 @@ test_that("Generate text for the consort", {
1313
label = "Cars in the data",
1414
bullet = TRUE
1515
)
16+
17+
tx7 <- gen_text(split(val$car, val$am),
18+
bullet = TRUE
19+
)
20+
21+
expect_equal(tx7, sub(".*?\\n", "", tx2))
22+
1623
tx3 <- gen_text(val$am)
1724
tx4 <- gen_text(rep("", 4), bullet = TRUE)
1825

@@ -54,4 +61,5 @@ test_that("Generate text for the consort", {
5461
sum(tab6["V-shaped",]), "):")
5562
)
5663

64+
5765
})

0 commit comments

Comments
 (0)