Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ VignetteBuilder: knitr
Depends:
R (>= 4.1.0)
RoxygenNote: 7.3.2
Roxygen: list(markdown = TRUE)
URL: https://bruigtp.github.io/flowchart/
Config/testthat/edition: 3
Config/Needs/website: rmarkdown
4 changes: 3 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,6 @@

* Stack flowcharts with `unite=TRUE` if there are more boxes in the last level of the first flowchart than in the first level of the second flowchart.

* Fixed `bug` allowing trailing zeros to be trimmed regardless of `round_digits` argument in `fc_filter()` and `fc_split()` (@kenkomodo)
* Fixed `bug` allowing trailing zeros to be trimmed regardless of `round_digits` argument in `fc_filter()` and `fc_split()` (@kenkomodo)

* Updated `fc_draw()` with arguments to control arrow shape and color: `arrow_color`, `arrow_lwd`, `arrow_lineend`, and `arrow_linejoin`. Updated `fc_export()` to use new arguments for flowchart image export. (#43; @kenkomodo)
22 changes: 11 additions & 11 deletions R/as_fc.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@
#' @description This function allows to initialize a flow chart given any database. It will create a fc object showing the number of rows of the database. If a database is not available, the user can instead directly enter the number of rows in the study.
#'
#' @param .data Data frame to be initialised as a flowchart.
#' @param N Number of rows of the study in case `.data` is NULL.
#' @param N Number of rows of the study in case `.data` is `NULL`.
#' @param label Character or expression with the text that will be shown in the box.
#' @param text_pattern Character or expression defining the structure that will have the text in each of the boxes. It recognizes label, n, N and perc within brackets. For default it is "\{label\}\\n \{n\}". If text_pattern or label is an expression, the label is always placed at the beginning of the pattern, followed by a line break where the structure specified by text_pattern is placed.
#' @param text_pattern Character or expression defining the structure that will have the text in each of the boxes. It recognizes `label`, `n`, `N` and `perc` within brackets. For default it is `"{label}\n {n}"`. If `text_pattern` or `label` is an expression, the `label` is always placed at the beginning of the pattern, followed by a line break where the structure specified by `text_pattern` is placed.
#' @param just Justification for the text: left, center or right. Default is center.
#' @param text_color Color of the text. It is black by default. See the `col` parameter for \code{\link{gpar}}.
#' @param text_fs Font size of the text. It is 8 by default. See the `fontsize` parameter for \code{\link{gpar}}.
#' @param text_fface Font face of the text. It is 1 by default. See the `fontface` parameter for \code{\link{gpar}}.
#' @param text_ffamily Changes the font family of the text. Default is NA. See the `fontfamily` parameter for \code{\link{gpar}}.
#' @param text_color Color of the text. It is `"black"` by default. See the `col` parameter for [gpar].
#' @param text_fs Font size of the text. It is 8 by default. See the `fontsize` parameter for [gpar].
#' @param text_fface Font face of the text. It is 1 by default. See the `fontface` parameter for [gpar].
#' @param text_ffamily Changes the font family of the text. Default is `NA`. See the `fontfamily` parameter for [gpar].
#' @param text_padding Changes the text padding inside the box. Default is 1. This number has to be greater than 0.
#' @param bg_fill Box background color. It is white by default. See the `fill` parameter for \code{\link{gpar}}.
#' @param border_color Box border color. It is black by default. See the `col` parameter for \code{\link{gpar}}.
#' @param width Width of the box. If NA, it automatically adjusts to the content (default). Must be an object of class \code{\link{unit}} or a number between 0 and 1.
#' @param height Height of the box. If NA, it automatically adjusts to the content (default). Must be an object of class \code{\link{unit}} or a number between 0 and 1.
#' @param hide Logical value to hide the initial box or not. Default is FALSE. hide = TRUE can only be combined with fc_split().
#' @param bg_fill Box background color. It is white by default. See the `fill` parameter for [gpar].
#' @param border_color Box border color. It is `"black"` by default. See the `col` parameter for [gpar].
#' @param width Width of the box. If `NA`, it automatically adjusts to the content (default). Must be an object of class [unit] or a number between 0 and 1.
#' @param height Height of the box. If `NA`, it automatically adjusts to the content (default). Must be an object of class [unit] or a number between 0 and 1.
#' @param hide Logical value to hide the initial box or not. Default is FALSE. hide = TRUE can only be combined with [fc_split()].
#'
#' @return List with the dataset and the initialized flowchart parameters.
#'
Expand Down
46 changes: 29 additions & 17 deletions R/fc_draw.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,20 @@
#' @param box_corners Indicator of whether to draw boxes with round (`"round"`) vs non-round (`"sharp"`) corners. Default is `"round"`.
#' @param arrow_angle The angle of the arrow head in degrees, as in `arrow`.
#' @param arrow_length A unit specifying the length of the arrow head (from tip to base), as in `arrow`.
#' @param arrow_ends One of "last", "first", or "both", indicating which ends of the line to draw arrow heads, as in `arrow`.
#' @param arrow_type One of "open" or "closed" indicating whether the arrow head should be a closed triangle, as in `arrow`.
#' @param title The title of the flowchart. Default is NULL (no title).
#' @param arrow_ends One of `"last"`, `"first"`, or `"both"`, indicating which ends of the line to draw arrow heads, as in [arrow].
#' @param arrow_type One of `"open"` or `"closed"` indicating whether the arrow head should be a closed triangle, as in [arrow].
#' @param arrow_color Color of the arrows. Default is `"black"`. See the `col` parameter for [gpar].
#' @param arrow_lwd Line width of the arrows. Default is 1. See the `lwd` parameter for [gpar].
#' @param arrow_lineend Line end style for arrows. One of `"round"`, `"butt"`, or `"square"`. Default is `"round"`. See the `lineend` parameter for [gpar].
#' @param arrow_linejoin Line join style for arrow heads (i.e., shape of arrow head corners). One of `"round"`, `"mitre"`, or `"bevel"`. Default is `"round"`. See the `linejoin` parameter for [gpar].
#' @param title The title of the flowchart. Default is `NULL` (no title).
#' @param title_x x coordinate for the title. Default is 0.5.
#' @param title_y y coordinate for the title. Default is 0.9.
#' @param title_color Color of the title. It is black by default. See the `col` parameter for \code{\link{gpar}}.
#' @param title_fs Font size of the title. It is 15 by default. See the `fontsize` parameter for \code{\link{gpar}}.
#' @param title_fface Font face of the title. It is 2 by default. See the `fontface` parameter for \code{\link{gpar}}.
#' @param title_ffamily Changes the font family of the title. Default is NA. See the `fontfamily` parameter for \code{\link{gpar}}.
#' @param canvas_bg Background color for the entire canvas (the area behind the flowchart boxes). Default is `"white"`. Set to `"transparent"` or `NULL` for a transparent background; `"transparent"` background will only be noticeable when exporting drawn flowcharts via `fc_export()` and is compatible with all `fc_export()` formats except `"jpeg"` and `"bmp"`.
#' @param title_color Color of the title. It is `"black"` by default. See the `col` parameter for [gpar].
#' @param title_fs Font size of the title. It is 15 by default. See the `fontsize` parameter for [gpar].
#' @param title_fface Font face of the title. It is 2 by default. See the `fontface` parameter for [gpar].
#' @param title_ffamily Changes the font family of the title. Default is `NA`. See the `fontfamily` parameter for [gpar].
#' @param canvas_bg Background color for the entire canvas (the area behind the flowchart boxes). Default is `"white"`. Set to `"transparent"` or `NULL` for a transparent background; `"transparent"` background will only be noticeable when exporting drawn flowcharts via [fc_export()] and is compatible with all [fc_export()] formats except `"jpeg"` and `"bmp"`.

#' @return Invisibly returns the same object that has been given to the function, with the given arguments to draw the flowchart stored in the attributes.
#'
Expand All @@ -30,7 +34,7 @@
#'
#' @export

fc_draw <- function(object, big.mark = "", box_corners = "round", arrow_angle = 30, arrow_length = grid::unit(0.1, "inches"), arrow_ends = "last", arrow_type = "closed", title = NULL, title_x = 0.5, title_y = 0.9, title_color = "black", title_fs = 15, title_fface = 2, title_ffamily = NULL, canvas_bg = "white") {
fc_draw <- function(object, big.mark = "", box_corners = "round", arrow_angle = 30, arrow_length = grid::unit(0.1, "inches"), arrow_ends = "last", arrow_type = "closed", arrow_color = "black", arrow_lwd = 1, arrow_lineend = "round", arrow_linejoin = "round", title = NULL, title_x = 0.5, title_y = 0.9, title_color = "black", title_fs = 15, title_fface = 2, title_ffamily = NULL, canvas_bg = "white") {

is_class(object, "fc")
UseMethod("fc_draw")
Expand All @@ -40,7 +44,7 @@ fc_draw <- function(object, big.mark = "", box_corners = "round", arrow_angle =
#' @importFrom rlang .data
#' @export

fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle = 30, arrow_length = grid::unit(0.1, "inches"), arrow_ends = "last", arrow_type = "closed", title = NULL, title_x = 0.5, title_y = 0.9, title_color = "black", title_fs = 15, title_fface = 2, title_ffamily = NULL, canvas_bg = "white") {
fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle = 30, arrow_length = grid::unit(0.1, "inches"), arrow_ends = "last", arrow_type = "closed", arrow_color = "black", arrow_lwd = 1, arrow_lineend = "round", arrow_linejoin = "round", title = NULL, title_x = 0.5, title_y = 0.9, title_color = "black", title_fs = 15, title_fface = 2, title_ffamily = NULL, canvas_bg = "white") {

# Check for valid corners argument
if (!box_corners %in% c("round", "sharp")) {
Expand All @@ -53,6 +57,14 @@ fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle
rect_type <- grid::rectGrob
}

if (!(arrow_lineend %in% c("round", "butt", "square"))) {
cli::cli_abort("The {.arg arrow_lineend} argument must be {.val round}, {.val butt}, or {.val square}")
}

if (!(arrow_linejoin %in% c("round", "mitre", "bevel"))) {
cli::cli_abort("The {.arg arrow_linejoin} argument must be {.val round}, {.val mitre}, or {.val bevel}")
}

#Initialize grid
grid::grid.newpage()

Expand All @@ -64,7 +76,7 @@ fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle
object0 <- object #to return the object unaltered

#We have to return the parameters of the function in the attribute of object$fc
params <- c("big.mark", "box_corners", "arrow_angle", "arrow_length", "arrow_ends", "arrow_type", "title", "title_x", "title_y", "title_color", "title_fs", "title_fface", "title_ffamily", "canvas_bg")
params <- c("big.mark", "box_corners", "arrow_angle", "arrow_length", "arrow_ends", "arrow_type", "arrow_color", "arrow_lwd", "arrow_lineend", "arrow_linejoin", "title", "title_x", "title_y", "title_color", "title_fs", "title_fface", "title_ffamily", "canvas_bg")
attr_draw <- purrr::map(params, ~get(.x))
names(attr_draw) <- params

Expand Down Expand Up @@ -154,7 +166,7 @@ fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle

#If it exists because now the initial box can be hided
if(length(id_par) > 0) {
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[id_par]], plot_fc[[i]]$bg[[k]], type = "N", arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[id_par]], plot_fc[[i]]$bg[[k]], type = "N", lty_gp = getOption("connectGrob", default = grid::gpar(col = arrow_color, fill = arrow_color, lwd = arrow_lwd, lineend = arrow_lineend, linejoin = arrow_linejoin)), arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
}

}
Expand All @@ -171,7 +183,7 @@ fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle

#If it exists because now the initial box can be hided
if(length(id) > 0) {
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[id]], plot_fc[[i]]$bg[[k]], type = "vertical", arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[id]], plot_fc[[i]]$bg[[k]], type = "vertical", lty_gp = getOption("connectGrob", default = grid::gpar(col = arrow_color, fill = arrow_color, lwd = arrow_lwd, lineend = arrow_lineend, linejoin = arrow_linejoin)), arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
}

}
Expand All @@ -180,7 +192,7 @@ fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle

for(k in ids) {

print(Gmisc::connectGrob(plot_fc[[i]]$bg[[k - 1]], plot_fc[[i]]$bg[[k]], type = "-", arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[k - 1]], plot_fc[[i]]$bg[[k]], type = "-", lty_gp = getOption("connectGrob", default = grid::gpar(col = arrow_color, fill = arrow_color, lwd = arrow_lwd, lineend = arrow_lineend, linejoin = arrow_linejoin)), arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))

}

Expand All @@ -197,15 +209,15 @@ fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle

for(k in ids) {

print(Gmisc::connectGrob(plot_fc[[i]]$bg[[id_last]], plot_fc[[i]]$bg[[k]], type = "N", arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[id_last]], plot_fc[[i]]$bg[[k]], type = "N", lty_gp = getOption("connectGrob", default = grid::gpar(col = arrow_color, fill = arrow_color, lwd = arrow_lwd, lineend = arrow_lineend, linejoin = arrow_linejoin)), arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))

}

} else if (length(ids) == 1 & length(id_last) > 1) {

for(k in id_last) {

print(Gmisc::connectGrob(plot_fc[[i]]$bg[[k]], plot_fc[[i]]$bg[[ids]], type = "L", arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[k]], plot_fc[[i]]$bg[[ids]], type = "L", lty_gp = getOption("connectGrob", default = grid::gpar(col = arrow_color, fill = arrow_color, lwd = arrow_lwd, lineend = arrow_lineend, linejoin = arrow_linejoin)), arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))

}

Expand All @@ -214,7 +226,7 @@ fc_draw.fc <- function(object, big.mark = "", box_corners = "round", arrow_angle
#They have the same number of boxes
for(k in 1:length(ids)) {
#vertical connection
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[id_last[k]]], plot_fc[[i]]$bg[[ids[k]]], type = "vertical", arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
print(Gmisc::connectGrob(plot_fc[[i]]$bg[[id_last[k]]], plot_fc[[i]]$bg[[ids[k]]], type = "vertical", lty_gp = getOption("connectGrob", default = grid::gpar(col = arrow_color, fill = arrow_color, lwd = arrow_lwd, lineend = arrow_lineend, linejoin = arrow_linejoin)), arrow_obj = getOption("connectGrobArrow", default = grid::arrow(angle = arrow_angle, length = arrow_length, ends = arrow_ends, type = arrow_type))))
}

} else {
Expand Down
Loading
Loading