Trying out the Flow package

| July 6, 2022

I have been meaning to try out the flow package as it appears to help people visualize loops and conditional statements.

Here is some code that I made to help someone over in the R4DS Slack channel. The task was to check if the change in temperature was over 0.5, and then to set the next 30 days of data to NA.

temp_data <- rnorm(100, 20, 1) #fake data about temperatures
N <- length(temp_data)

#initialization
buffer <- 0
day <- 2
previous_temp <- temp_data[1]

# loop
while(day <= N){
  current_temp <- temp_data[day]
  
  if(buffer == 0){
    if(!is.na(current_temp) & !is.na(previous_temp)){
      #perform check
      if(abs(current_temp - previous_temp) > 0.5){
      buffer <- 30
    }}
  } else {
    temp_data[day] <- NA
    buffer <- buffer - 1
  }
  
  # iterate
  previous_temp <- current_temp
  day <- day + 1
}

For the flow package capabilities, I think I need to wrap this code chunk into a function.

prune_data <- function(temp_data){
  N <- length(temp_data)
  
  #initialization
  buffer <- 0
  day <- 2
  previous_temp <- temp_data[1]
  
  # loop
  while(day <= N){
    current_temp <- temp_data[day]
    
    if(buffer == 0){
      if(!is.na(current_temp) & !is.na(previous_temp)){
        #perform check
        if(abs(current_temp - previous_temp) > 0.5){
          buffer <- 30
        }}
    } else {
      temp_data[day] <- NA
      buffer <- buffer - 1
    }
    
    # iterate
    previous_temp <- current_temp
    day <- day + 1
  }
}

Now let us see if the flow package can visualize the prune_data function.

flow::flow_view(prune_data)
filename #.if: visual=rhomb fill=#e2efda align=center #.for: visual=rhomb fill=#ddebf7 align=center #.repeat: visual=rhomb fill=#fce4d6 align=center #.while: visual=rhomb fill=#fff2cc align=center #.standard: visual=class fill=#ededed align=left #.commented: visual=class fill=#ededed #.header: visual=roundrect fill=#d9e1f2 align=left #.return: visual=end fill=#70ad47 empty #.stop: visual=end fill=#ed7d31 empty #.break: visual=receiver fill=#ffc000 empty #.next: visual=transceiver fill=#5b9bd5 empty #arrowSize: 1 #bendSize: 0.3 #direction: down #gutter: 5 #edgeMargin: 0 #edges: hard #fill: #eee8d5 #fillArrows: false #font: Courier #fontSize: 12 #leading: 1.25 #lineWidth: 3 #padding: 16 #spacing: 40 #stroke: #33322E #title: filename #zoom: 1 #acyclicer: greedy #ranker: network-simplex [<header>prune_data(temp_data)] -> [<standard> 1: ;N <- length(temp_data) buffer <- 0 day <- 2 previous_temp <- temp_data\[1\]] [<standard> 1: ;N <- length(temp_data) buffer <- 0 day <- 2 previous_temp <- temp_data\[1\]] -> [<while> 2: ;⠀ while (day <= N) ⠀ ⠀ ⠀ ⠀] [<while> 2: ;⠀ while (day <= N) ⠀ ⠀ ⠀ ⠀] -> [<standard> 3: ;current_temp <- temp_data\[day\]] [<standard> 3: ;current_temp <- temp_data\[day\]] -> [<if> 4: ;⠀ if (buffer == 0) ⠀ ⠀ ⠀ ⠀] [<if> 4: ;⠀ if (buffer == 0) ⠀ ⠀ ⠀ ⠀] y -> [<if> 5: ;⠀ if (!is.na(current_temp) & !is.na(previous_temp)) ⠀ ⠀ ⠀ ⠀] [<if> 5: ;⠀ if (!is.na(current_temp) & !is.na(previous_temp)) ⠀ ⠀ ⠀ ⠀] y -> [<if> 6: ;⠀ if (abs(current_temp - previous_temp) > 0.5) ⠀ ⠀ ⠀ ⠀] [<if> 6: ;⠀ if (abs(current_temp - previous_temp) > 0.5) ⠀ ⠀ ⠀ ⠀] y -> [<standard> 7: ;buffer <- 30] [<standard> 7: ;buffer <- 30] -> [<end> -6:] [<if> 6: ;⠀ if (abs(current_temp - previous_temp) > 0.5) ⠀ ⠀ ⠀ ⠀] n -> [<end> -6:] [<end> -6:] -> [<end> -5:] [<if> 5: ;⠀ if (!is.na(current_temp) & !is.na(previous_temp)) ⠀ ⠀ ⠀ ⠀] n -> [<end> -5:] [<end> -5:] -> [<end> -4:] [<if> 4: ;⠀ if (buffer == 0) ⠀ ⠀ ⠀ ⠀] n -> [<standard> 8: ;temp_data\[day\] <- NA buffer <- buffer - 1] [<standard> 8: ;temp_data\[day\] <- NA buffer <- buffer - 1] -> [<end> -4:] [<end> -4:] -> [<standard> 9: ;previous_temp <- current_temp day <- day + 1] [<standard> 9: ;previous_temp <- current_temp day <- day + 1] -> [<start> -2:] [<while> 2: ;⠀ while (day <= N) ⠀ ⠀ ⠀ ⠀] next <- [<start> -2:] [<start> -2:] -> [<return> 10:] y y y n n n next prune_data(temp_data) 1: N <- length(temp_data) buffer <- 0 day <- 2 previous_temp <- temp_data[1] 2: ⠀ while (day <= N) ⠀ ⠀ ⠀ ⠀ 3: current_temp <- temp_data[day] 4: ⠀ if (buffer == 0) ⠀ ⠀ ⠀ ⠀ 5: ⠀ if (!is.na(current_temp) & !is.na(previous_temp)) ⠀ ⠀ ⠀ ⠀ 6: ⠀ if (abs(current_temp - previous_temp) > 0.5) ⠀ ⠀ ⠀ ⠀ 7: buffer <- 30 8: temp_data[day] <- NA buffer <- buffer - 1 9: previous_temp <- current_temp day <- day + 1