Skip to content

Commit 46ab5c4

Browse files
authored
Merge pull request #35 from adayim/revert
Minor bug fix and simple markup support.
2 parents c4d5d16 + d9e758d commit 46ab5c4

23 files changed

Lines changed: 743 additions & 77 deletions

NEWS.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# consort 1.2.3
22

33
- Allow user configuration of arrow graphical parameters and padding with `set_consort_defaults()`.
4-
- Allow custom bullet characters
4+
- Allow custom bullet characters.
5+
- Allow simple markup for bold, italic and superscript.
56
- Figure styles will also be applied to grViz plot.
67
- Improved documentation.
78
- Special thanks to @Ramsas88

R/build_grid.R

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
#' \code{\link{build_grviz}} or \code{plot(g, grViz = TRUE)} for
77
#' multiple split nodes instead.
88
#'
9-
#' @param x A conosrt object.
9+
#' @param x A consort object.
1010
#'
1111
#' @return A \code{gList} object
1212
#' @export
@@ -54,13 +54,16 @@ build_grid <- function(x) {
5454
nodes_coord$y <- (vp_height - nodes_coord$y)/vp_height
5555

5656
if(any(grepl("label", names(x)))){
57-
label_coord <- calc_coords_label(label_plot,
58-
nodes_coord$nd_y,
57+
label_coord <- calc_coords_label(label_plot,
58+
nodes_coord$nd_y,
5959
max_h = nodes_coord$max_height)
6060
vp_width <- sum(label_coord$width[1], vp_width)
6161

6262
nodes_coord$x <- (nodes_coord$x + label_coord$width[1])/vp_width
63-
63+
64+
# Convert label x from char units to NPC so they scale with the viewport
65+
label_coord$x <- label_coord$x / vp_width
66+
6467
}else{
6568
nodes_coord$x <- (nodes_coord$x)/vp_width
6669
}
@@ -72,22 +75,17 @@ build_grid <- function(x) {
7275
y = unit(nodes_coord$y[i], "npc"))
7376
r$name <- i
7477

75-
# Skep empty side box
78+
# Skip empty side box
7679
if(is_empty(consort_plot[[i]]$text))
7780
return(NULL)
7881

7982
return(r)
8083
}, simplify = FALSE)
8184

85+
grobs_list <- gList()
8286
for (i in seq_along(nodes)) {
83-
if(is.null(nodes[[i]]))
84-
next
85-
86-
if (i == 1) {
87-
grobs_list <- gList(gList(), nodes[[i]])
88-
} else {
87+
if(!is.null(nodes[[i]]))
8988
grobs_list <- gList(grobs_list, nodes[[i]])
90-
}
9189
}
9290

9391
# Connections
@@ -115,28 +113,22 @@ build_grid <- function(x) {
115113
for(i in seq_along(label_plot)){
116114
nam <- names(label_plot)[i]
117115
r <- move_box(label_plot[[nam]]$box,
118-
x = unit(label_coord$x[nam], "char"),
116+
x = unit(label_coord$x[nam], "npc"),
119117
y = unit(label_coord$y[nam], "npc"))
120118
r$name <- nam
121119

122-
if (i == 1) {
123-
lab_grobs <- gList(gList(), r)
124-
} else {
125-
lab_grobs <- gList(lab_grobs, r)
126-
}
120+
lab_grobs <- if (i == 1) gList(r) else gList(lab_grobs, r)
127121
}
128122

129123
grobs_list <- gList(grobs_list, lab_grobs)
130124

131125
}
132126

133-
grobTree(grobs_list,
127+
grobTree(grobs_list,
134128
name = "consort",
135129
vp = viewport(width = unit(0.98, "npc"),
136130
height = unit(0.98, "npc")))
137-
138-
# return(grobs_list)
139-
131+
140132
}
141133

142134

R/defaults.R

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ consort_defaults_settings <- list(
1111
arrow_length = 0.1,
1212
arrow_type = "closed",
1313
pad_u = 3,
14-
bullet = "\u2022"
14+
bullet = "\u2022",
15+
parse_markup = FALSE
1516
)
1617

1718
consort_global$defaults <- consort_defaults_settings
@@ -36,6 +37,10 @@ consort_opt <- function(name) {
3637
#' @param arrow_type Character, arrow type: \code{"closed"} or \code{"open"}.
3738
#' @param pad_u Numeric, padding between nodes.
3839
#' @param bullet Character, bullet character for side box items.
40+
#' @param parse_markup Logical, whether to parse lightweight markup syntax
41+
#' (\code{**bold**}, \code{*italic*}, \code{^{superscript}},
42+
#' \code{_{subscript}}, \code{__underline__}) in node labels.
43+
#' Default is \code{FALSE}.
3944
#'
4045
#' @return Invisibly returns the previous defaults (a \code{consort_defaults} object).
4146
#' @export
@@ -63,7 +68,8 @@ set_consort_defaults <- function(
6368
arrow_length = NULL,
6469
arrow_type = NULL,
6570
pad_u = NULL,
66-
bullet = NULL
71+
bullet = NULL,
72+
parse_markup = NULL
6773
) {
6874

6975
old <- get_consort_defaults()
@@ -78,7 +84,8 @@ set_consort_defaults <- function(
7884
arrow_length = arrow_length,
7985
arrow_type = arrow_type,
8086
pad_u = pad_u,
81-
bullet = bullet
87+
bullet = bullet,
88+
parse_markup = parse_markup
8289
)
8390

8491
# Keep only non-NULL arguments
@@ -117,6 +124,11 @@ set_consort_defaults <- function(
117124
stop("`bullet` must be a single character string.")
118125
}
119126

127+
if (!is.null(parse_markup)) {
128+
if (!is.logical(parse_markup) || length(parse_markup) != 1 || is.na(parse_markup))
129+
stop("`parse_markup` must be a single TRUE/FALSE value.")
130+
}
131+
120132
# Update stored defaults
121133
consort_global$defaults <- utils::modifyList(consort_global$defaults, args)
122134

R/grid_util.R

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ calc_y_coords <- function(consort_plot, nodes_layout, pad_u) {
1414
prev_bt <- max(heights)
1515
} else {
1616
# Extra padding when column count changes (split/merge transition)
17-
extra_pad <- if (length(nd_y[[i]]) != length(nd_y[[i - 1]])) 2 * pad_u else pad_u
17+
extra_pad <- if (length(heights) != length(nd_y[[i - 1]])) 2 * pad_u else pad_u
1818
nd_y[[i]] <- prev_bt + extra_pad + heights / 2
1919
prev_bt <- prev_bt + extra_pad + max(heights)
2020
}
@@ -275,10 +275,12 @@ gp_consecutive <- function(x){
275275
int <- 1
276276
gp <- vector("character", length = length(x))
277277
gp[1] <- letters[int]
278-
for(i in 2:length(x)){
279-
if(x[i] != x[i-1])
280-
int <- int + 1
281-
gp[i] <- letters[int]
278+
if(length(x) > 1){
279+
for(i in 2:length(x)){
280+
if(x[i] != x[i-1])
281+
int <- int + 1
282+
gp[i] <- letters[int]
283+
}
282284
}
283285
return(gp)
284286
}

R/grviz_util.R

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -128,22 +128,36 @@ mk_invs_connect <- function(x){
128128
# Make text alignment
129129
#' @keywords internal
130130
mk_text_align <- function(text, just, group = NULL, grviz_style = NULL){
131-
# If empty
132-
# if(is_empty(text))
133-
# return("")
134-
135-
jst <- ifelse(just == "center", "",
136-
ifelse(just == "left", "\\l", "\r"))
137-
138-
if(just %in% c("left", "right")){
139-
text <- unlist(strsplit(text, "\n"))
140-
text <- ifelse(just == "left",
141-
paste(text, collapse = "\\l"),
142-
paste(text, collapse = "\r"))
131+
132+
if (has_markup(text)) {
133+
# Graphviz HTML-like label: <html> instead of "plain"
134+
html <- markup_to_html(text)
135+
136+
if (just == "left") {
137+
html <- gsub("<br/>", '<br align="left"/>', html, fixed = TRUE)
138+
html <- paste0(html, '<br align="left"/>')
139+
} else if (just == "right") {
140+
html <- gsub("<br/>", '<br align="right"/>', html, fixed = TRUE)
141+
html <- paste0(html, '<br align="right"/>')
142+
}
143+
144+
attr_parts <- sprintf("label = <%s>", html)
145+
} else {
146+
# Original plain-text label
147+
jst <- ifelse(just == "center", "",
148+
ifelse(just == "left", "\\l", "\r"))
149+
150+
if(just %in% c("left", "right")){
151+
text <- unlist(strsplit(text, "\n"))
152+
text <- ifelse(just == "left",
153+
paste(text, collapse = "\\l"),
154+
paste(text, collapse = "\r"))
155+
}
156+
157+
attr_parts <- sprintf('label = "%s%s"', text, jst)
143158
}
144159

145160
# Build attributes
146-
attr_parts <- sprintf('label = "%s%s"', text, jst)
147161
if(!is.null(group)) attr_parts <- paste(attr_parts, sprintf("group=%s", group))
148162
if(!is.null(grviz_style)) attr_parts <- paste(attr_parts, grviz_style)
149163

0 commit comments

Comments
 (0)