moodymudskipper / flow

View and Browse Code Using Flow Diagrams
https://moodymudskipper.github.io/flow/
Other
395 stars 26 forks source link

ideas #1

Closed moodymudskipper closed 4 years ago

moodymudskipper commented 4 years ago

Code is one dimensional, with flow charts we see what processes are parallel.

Code can also be summarized by using special comments.

1st step

Iterate through the call, spot control-flow constructs and draw the diagram using (probably) DiagrammeR::grViz(), we can check alternatives in DiagrammeR

Validate by testing on :

2nd step

take the srcref attribute of the function if it exists, spot the instance of funflow comments, and replace by a special call the comment AND everything that follows until the next funflow comment OR the next unmatched closing bracket.

In practice :

if(this > that){
  #-> replace NA with 0
  x[is.na(x)] <- 0
  #-> print x times 3 
  print(3*x)
}

will be transformed into :

if(this > that){
  `*box*`("replace NA with 0")
   `*box*`("print x times 3 ")
}

And then the *box* symbol will be recognized when iterating, so the description of the code is displayed instead of the code.

We can get fancy and add formatting but it's also good to have these comments not look too different from regular comments, so we can code using them without confusing the code.

Using these funflow comments can be switched on/off through a boolean argument to the funflow() function.

We can get a fancy chart header, displaying arguments, their default values, and maybe their description from the help file if available (would be displayed below arg in smaller letters). The title of the chart is a parameter of funflow() but can also be taken automatically from the help file.

moodymudskipper commented 4 years ago

#' @param fun function 
#' @param code wether the code should be displayed, if NA, code will be displayed only if no special comment is shown
#' @param comment the prefix of special comments, should be NULL or a character string starting with `"#"`
funflow <- function(fun, code = NA, comment = NULL){
  ...
}
moodymudskipper commented 4 years ago

I think we could do debugging/browsing and error tracing.

It's a more advanced feature but would be quite amazing.

moodymudskipper commented 4 years ago

A funflow (flowfun ? something else ?) object is a function that displays a diagram of the function with the path that was taken through the control-flow constructs.

as_funflow converts a standard function to a funflow, view_flow just shows the diagram.

If the function is debugged, it will update the diagram at every step. See https://github.com/moodymudskipper/mmmisc/issues/33 .

as_funflow(fun) creates a function such as this one :

function(x,y) { # x and y are fun's parameters
  #> z <- 42 # <- the code from block 1
  block(1)
  if({
    #> x > foo # <- the code from block 2 
    block(2)
  }) {
    #> ... # <- the code from block 3
    block(3)
  } else {
    #> ... # <- the code from block 4
    block(4)
  }
}

Where we could replace the code in comments by the code labels , with an argument to as_funflow

block() will run the appropriate block in the relevant instructions in the relevant environment and display the updated diagram only if we're debugging.

In any the final diagram is shown in the end, and the output is returned as if we had called fun(). In case of error we still show the full diagram, but show where the error happened and what way we took.

moodymudskipper commented 4 years ago

This works at the moment (funs should be renamed) :

area <- function(radius, angle, check = TRUE){
  radius_squared <- r^2
  if(check){
    if(angle < 0 || angle > 2*pi){
      stop("incorrect angle value")
    } else {
      print("good angle value")
    }
  }
  area <- angle * radius_squared
  area
}

funflow(area)

image

moodymudskipper commented 4 years ago
moodymudskipper commented 4 years ago

obsolete

github-actions[bot] commented 2 years ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.