Using the DiagrammeR package to produce a process flow diagram in RStudio

Process flow diagrams are powerful tools for process analysis, and having created a way to produce them in R is one among the greatest credits to be given to Rich Iannone and his DiagrammeR package.

Generally speaking, this package leverages HTML widgets to let you build an R diagram of nearly every kind.

Even if more advanced and customizable tools are available as standalone software, DiagrammeR lets you easily integrate different parts of your analysis without leaving R.

Moreover, DiagrammeR is perfectly integrated in RStudio and the Shiny framework, which is one among the hottest tools in R community.

Getting ready

As usual, we first have to install and load the necessary package, that is to say the DiagrammeR package:

install.packages("DiagrammeR")
library(DiagrammeR)

We are now ready to create a data frame that stores nodes and hedges of our process workflow.

Particularly, we will build an example from the healthcare environment regarding drugs administration.

How to do it...

  1. We will start by creating a nodes data frame running create_nodes().

    First of all, we need to create a data frame storing all process flow nodes, that is to say all steps composing the process we are looking at. This is done using create_nodes() and by passing ID, label, shape, and colors:

     nodes <- create_nodes(nodes = seq(1:7),
      label = c("select drug", "is it in stock?", "administer it",
      "adquire it","has it solved disease?",
      "look for another drug","end of treatment"),
      distortion = c(0,2,3,30),
      sides = 4,
      shape =  "rectangle",
      style = "filled",
      fillcolor = c("yellow", "lightgreen","azure",
    
      "azure","lightgreen","azure", "yellow")
    )
  2. Next, we create a list of all connections. The second element of every kind of network, and process flows can be considered pertaining to this family as well, is a list of all connection linking nodes.

    To obtain this list, we will create the following list object composed of pairs of nodes IDs:

    edges_couples <- list(c(1,2),c(2,3),c(2,4),c(4,3),c(3,5),c(5,6),c(5,7),c(6,1))
  3. We will now map the list to a from and to vector. In order to create diagram edges, we will call the create_edges() function that requires you to pass a from and a to vector. We will therefore export all first elements of our edge couple to the from vector and all second elements to the to vector:
    from_vect <- c()
    to_vect    <- c()
    for(i in 1:length(edges_couples)) {
      from_vect <- c(from_vect,edges_couples[[i]][1])
      to_vect   <- c(to_vect,edges_couples[[i]][2])
    }
  4. We can now call the create_edges() function, passing the previously created from and to vectors, adding labels that we want to be shown on the edges:
    edges <- create_edges(from = from_vect,
      to   = to_vect,
      label = c("","yes","no","","","no","yes"))
  5. In this step, we will display the process flow diagram in RStudio Viewer.

    This step requires you to actually create a graph object through the create_graph() function and render it through render_graph():

    process_flow <- create_graph(nodes,edges,
      node_attrs = c("fontname = Helvetica
      color = grey80"),
      edge_attrs = c("color = lightblue",
      "arrowsize = 0.5"))
    render_graph(process_flow)

    This will result in the following diagram appearing in your RStudio Viewer pane:

    How to do it...
  6. We can now export our diagram by leveraging the export control in the Viewer pane:
    How to do it...

    After clicking on Copy to Clipboard, the following window will appear, giving you the possibility to resize your picture before actually copying it to your clipboard:

    How to do it...

    Once you are done with the resizing, you will just have to hit the Copy Plot button and paste your diagram where it is needed.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset