Closed moodymudskipper closed 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){
...
}
I think we could do debugging/browsing and error tracing.
It's a more advanced feature but would be quite amazing.
on.exit()
call colors in red the block that failsA 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.
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)
flow_debugonce()
/ flow_debug()
which would override or trace the function for one callobsolete
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.
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 inDiagrammeR
Validate by testing on :
[.data.table
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 :
will be transformed into :
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.