textdiagrammer

R DiagrammeR package Mermaid text using actual calculation results


I would like to utilize the DiagrammeR package for a simple flow chart in my Rmarkdown. However, I couldn't figure out a way to use actual output from a data table into the text. Suppose I have a simple query of a database with total records, patients count and date in year info for three different cohorts. I wanted to create a diagram using Mermaid. The codes look at this.

Total = paste0('Records:',b1$records,' Patients:',b1$patients,' Year:',b1$year)
# (Records:1000 Patients:822 Year:5)
Sub1 = paste0('Records:',b2$records,' Patients:',b2$patients,' Year:',b2$year)
Sub2 = paste0('Records:',b3$records,' Patients:',b3$patients,' Year:',b3$year)

mermaid("
graph TB
  A[Total] --> B{Sub1} --> C{Sub2}
  ")

Instead of Printing out diagram with: Records:1000 Patients:822 Year:5 in the A, it shows verbatim word "Total".

Any suggestion on how to do it correctly?

Thanks!


Solution

  • You are one step away from what you'd like to achieve. Please try this simple example below to see the logic:

    library(DiagrammeR)
    

    Stracture:

    DiagrammeR(
      "
      graph TB
      
      A[Question] -->B[Answer]
      "
    )
    

    1. Define answer node:

    B <- paste0("There are ", nrow(iris), " records")
    

    2. Combine it with other components, using ; to separate statements:

    results <- paste0("graph TB; A[How many rows does iris have?]-->", "B[", B, "]")
    

    3. Call 'results' in DiagrammeR:

    DiagrammeR(diagram = results)
    

    The final plot should refresh when your calculation updates.

    The plot that calls your calculation