Predictive Hacks

How to Draw Flow Diagrams in R

The “flow” package visualizes as flow diagrams the logic of functions, expressions, or scripts. Let’s take an example of a function that checks if a number is a prime or not.

is_prime <- function(num) {
   if (num == 2) {
      TRUE
   } else if (any(num %% 2:(num-1) == 0)) {
      FALSE
   } else { 
      TRUE
   }
}
 
is_prime(997)
 
[1] TRUE

We can get the flow diagram by running:

library(flow)
flow_view(is_prime)
 
How to Draw Flow Diagrams in R 1

Share This Post

Share on facebook
Share on linkedin
Share on twitter
Share on email

Subscribe To Our Newsletter

Get updates and learn from the best

More To Explore