0

Package DiagrammeR in R has a nice Razor-like string replacement syntax using @@ for node labels. In simple unidirectional flowcharts I made in the past using digraph flowchart, I used to be able to combine it with an additional sprintf() statement to insert numbers from a table.

However, I cannot make it work with a newer complex flowchart with side arms based on dummy nodes that is based on this answer. Here is a minimal reproducible example.

library(DiagrammeR)
inclusion_flowchart <- grViz(
    sprintf("digraph flowchart {
      # node definitions with substituted label text
      node [fontname = Helvetica, shape = rectangle]
      base [label = '@@1']

      exc1 [label = '@@2']
      rem1 [label = '@@3']

      exc2 [label = '@@4']
      rem2 [label = '@@5']

      exc3 [label = '@@6']
      rem3 [label = '@@7']

      exc4 [label = '@@8']
      rem4 [label = '@@9']

      # downward arrows FROM dummy nodes where a side arrow starts
      node [shape=none, width=0, height=0, label = '']
      side1 -> rem1;
      side2 -> rem2;
      side3 -> rem3;
      side4 -> rem4;

      # side arrows from dummy nodes
      {rank = same; side1 -> exc1}
      {rank = same; side2 -> exc2}
      {rank = same; side3 -> exc3}
      {rank = same; side4 -> exc4}

      # downword LINES TO dummy sites where a side arrow starts
      edge [dir = none]
      base -> side1;
      rem1 -> side2;
      rem2 -> side3;
      rem3 -> side4;
      }

      [1]: 'Included patients with baseline data: n=%s participants'

      [2]: 'Excluded for reason 1: n=%s'
      [3]: 'Remain after exclusion 1: n=%s'

      [4]: 'Excluded for reason 2: n=%s'
      [5]: 'Remain after exclusion 2: n=%s'

      [6]: 'Excluded for reason 3: n=%s'
      [7]: 'Remain after reason 3: n=%s'

      [8]: 'Excluded for reason 4: n=%s'
      [9]: 'Remain after reason 4: n=%s'
      ",
      100, #1, could be extracted from a table
      10, #2, could be extracted from a table
      90, #3, could be extracted from a table
      25, #4, could be extracted from a table
      65, #5, could be extracted from a table
      4, #6, could be extracted from a table
      61, #7, could be extracted from a table
      17, #8, could be extracted from a table
      44 #9, could be extracted from a table
      ) # closes sprintf
  ) # closes grViz call

But I obtain the following error message, which obviously refers to the Razor-like replacement:

Error in gsub(paste0("@@", i), eval_expressions[[i]][1], spec_body) : 
  invalid 'replacement' argument

I have tried enclosing the sprintf() statement in replace_in_spec(), which is used explicitly in the documentation of DiagrammeR, but this was not necessary in my previous use of this structure in simpler flowcharts (unfortunately, I cannot retrieve those examples), and leads to the same error.

I suspect there is something wrong with my node specifications, but I cannot identify the problem. Note that if I strip the syntax from any attempt to replace substrings I do obtain the desired structure:

inclusion_flowchart_naked <- grViz("digraph flowchart {
  # node definitions with substituted label text
  node [fontname = Helvetica, shape = rectangle]
  base [label = 'base']

  exc1 [label = 'exc1']
  rem1 [label = 'rem1']

  exc2 [label = 'exc2']
  rem2 [label = 'rem2']

  exc3 [label = 'exc3']
  rem3 [label = 'rem3']

  exc4 [label = 'exc4']
  rem4 [label = 'rem4']

  # downward arrows FROM dummy nodes where a side arrow starts
  node [shape=none, width=0, height=0, label = '']
  side1 -> rem1;
  side2 -> rem2;
  side3 -> rem3;
  side4 -> rem4;

  # side arrows from dummy nodes
  {rank = same; side1 -> exc1}
  {rank = same; side2 -> exc2}
  {rank = same; side3 -> exc3}
  {rank = same; side4 -> exc4}

  # downword LINES TO dummy sites where a side arrow starts
  edge [dir = none]
  base -> side1;
  rem1 -> side2;
  rem2 -> side3;
  rem3 -> side4;
}
") # closes grViz call    

Which is: enter image description here

Thank you in advance if you can share your ideas. I am using R 3.6.2, RStudio 1.2.5033, and DiagrammeR 1.0.5.

torwart
  • 550
  • 1
  • 5
  • 11
  • Using R version 3.6.1. and DiagrammeR 1.0.1 your code runs and the `sprintf` replacement works fine (the dummy nodes however not as they are visible...). So it seems to be a problem/change within the package and/or R. – ek-g Jul 14 '20 at 12:53
  • Thank you; I have been considering updating to R 4.0.2, though it is just out. I will post here if something changes then. – torwart Jul 14 '20 at 16:51

0 Answers0