--- title: "flowchart" output: rmarkdown::html_vignette: toc: true toc_depth: 5 number_sections: true package: "`flowchart`" vignette: > %\VignetteIndexEntry{flowchart} %\VignetteEncoding{UTF-8} %\VignetteEngine{knitr::rmarkdown} editor_options: chunk_output_type: console --- ```{r message=FALSE, warning=FALSE, include=FALSE} library(flowchart) library(dplyr) library(tidyr) library(purrr) library(stringr) library(tibble) ``` # Overview `flowchart` is a package for drawing participant flow diagrams directly from a `data.frame` using tidyverse. It provides a set of functions that can be combined with `|>` to create all kinds of flowcharts from a `data.frame` in an easy way: - `as_fc()` transforms a `data.frame` into a `fc` object that can be manipulated by the package - `fc_split()` splits a flowchart according to the different values of a column in the `data.frame` - `fc_filter()` creates a filtered box from the flowchart, based on the evaluation of an expression in the `data.frame` - `fc_merge()` combines horizontally two different flowcharts - `fc_stack()` combines vertically two different flowcharts - `fc_modify()` allows to modify the parameters of the flowchart which are stored in `.$fc` - `fc_draw()` draws the flowchart created by the previous functions - `fc_export()` allows to export the flowchart drawn to the desired format # Installation We can install the stable version in CRAN: ```{r eval=FALSE} install.packages("flowchart") ``` Or the development version from GitHub: ```{r eval=FALSE} # install.packages("remotes") remotes::install_github('bruigtp/flowchart') ``` # `safo` dataset We will use the built-in dataset `safo`, which is a randomly generated dataset from the SAFO trial[^1]. SAFO is an open-label, multicentre, phase III–IV superiority randomised clinical trial designed to assess whether cloxacillin plus fosfomycin administered during the first 7 days of therapy achieves better treatment outcomes than cloxacillin alone in hospitalised patients with meticillin-sensitive Staphylococcus aureus bacteraemia. [^1]: Grillo, S., Pujol, M., Miró, J.M. et al. Cloxacillin plus fosfomycin versus cloxacillin alone for methicillin-susceptible Staphylococcus aureus bacteremia: a randomized trial. Nat Med 29, 2518–2525 (2023). https://doi.org/10.1038/s41591-023-02569-0 ```{r} library(flowchart) data(safo) head(safo) ``` # Basic operations The first step is to initialise the flowchart with `as_fc`. The last step, if we want to visualise the created flowchart, is to draw the flowchart with `fc_draw`. In between we can combine the functions `fc_split`., `fc_filter`, `fc_merge`, `fc_stack` with the operator pipe (`|>` or `%>$`) to create complex flowchart structures. ## Initialize To initialize a flowchart from a dataset we have to use the `as_fc()` function: ```{r} safo_fc <- safo |> as_fc() str(safo_fc, max.level = 1) ``` The `safo_fc` object created is a `fc` object, which consists of a list containing the tibble of the `data.frame` associated with the flowchart and the tibble that stores the flowchart parameters. In this example, `safo_fc$data` corresponds to the `safo` dataset while `safo_fc$fc` contains the parameters of the initial flowchart: ```{r} safo_fc$fc ``` Alternatively, if a `data.frame` is not available, we can initialize a flowchart using the `N =` argument manually specifying the number of rows: ```{r include=FALSE} as_fc(N = 230) ``` ## Draw The function `fc_draw()` allows to draw the flowchart associated to any `fc` object. Following the last example, we can draw the initial flowchart that has been previously created: ```{r} safo_fc |> fc_draw() ``` ## Filter We can filter the flowchart using `fc_filter()` specifying the logic in which the filter is to be applied. For example, we can show the number of patients that were randomized in the study: ```{r fig.width = 6, fig.height = 5} safo |> as_fc(label = "Patients assessed for eligibility") |> fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) |> fc_draw() ``` Percentages are calculated from the box in the previous level. See 'Modify function arguments' for more information on the `label=` and `show_exc=` arguments. Alternatively, if the column to filter is not available, we can use the `N =` argument to manually specify the number of rows of the resulting filter: ```{r fig.width = 6, fig.height = 5} safo |> as_fc(label = "Patients assessed for eligibility") |> fc_filter(N = 215, label = "Randomized", show_exc = TRUE) |> fc_draw() ``` ## Split We can split the flowchart into groups using `fc_split()` specifying the grouping variable. The function will split the flowchart into as many categories as the specified variable has. For example, we can split the previous flowchart showing the patients allocated in the two study treatments: ```{r fig.width = 6, fig.height = 5} safo |> dplyr::filter(!is.na(group)) |> as_fc(label = "Randomized patients") |> fc_split(group) |> fc_draw() ``` Percentages are calculated from the box in the previous level. Alternatively, if the column to split is not available, we can use the `N =` argument to manually specify the number of rows in each group of the resulting split: ```{r fig.width = 6, fig.height = 5} safo |> dplyr::filter(!is.na(group)) |> as_fc(label = "Randomized patients") |> fc_split(N = c(105, 110), label = c("cloxacillin plus fosfomycin", "cloxacillin alone")) |> fc_draw() ``` The idea is to combine the `fc_filter()` and `fc_split()` functions in the way we want to create different flowchart structures, however complex the may be. ## Export Once the flowchart has been drawn we can export it to the most popular image formats, including both bitmap (png, jpeg, tiff, bmp) and vector (svg, pdf) formats, using `fc_export()`: ```{r eval = FALSE} safo |> as_fc(label = "Patients assessed for eligibility") |> fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) |> fc_draw() |> fc_export("flowchart.png") ``` We can change the size and resolution of the stored image. ```{r eval = FALSE} safo |> as_fc(label = "Patients assessed for eligibility") |> fc_filter(!is.na(group), label = "Randomized", show_exc = TRUE) |> fc_draw() |> fc_export("flowchart.png", width = 3000, height = 4000, res = 700) ```