moodymudskipper / flow

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

Focus on particular variable #79

Closed jpkrooney closed 2 years ago

jpkrooney commented 2 years ago

Hi. Great package, thanks for making it.

Is it possible to name a variable and make a flow diagram of where this variable is sent as arguments in sub-functions etc? I'm asking because I'm looking at someone else complex code, and I wish to change something such that a variable is not created (to use a faster alternative method). But this means changes everywhere downstream that the variable is used. It would be really useful to be able to do something like flow_view(function, varname, out = "file.png"), and get a flow diagram of where this variable is passed. Many thanks!

moodymudskipper commented 2 years ago

Interestingly I'm also working on refactoring horrible code and I've been building a new feature this afternoon that you might like :

flow_view_vars(unglue:::unglue_data0)

flow2

It doesn't say anything about what calls a variable was fed to, but it does show which variables depend on other variables.

Direct dependencies are shown with full lines, dashed lines means it depends through control flow.

So basically if you have if(foo == 1) x <- 2 * y there'll be a full line from y to x and a dashed line from foo to x.

Arguments are blue and only outputs should be seen at the end of a branch, unless it's dead code. This assumes you don't use functions like assign and don't use variables from parent environments (so everything starts from arguments)

I've pushed it so you can use it too. Unfortunately for my use case it seems to crash for big diagrams at the moment, let me know if it works for you!

jpkrooney commented 2 years ago

That looks awesome! I'd love to try it out, but I'm not seeing your push - for me it shows latest changes 2 days ago - did it definitely go through ? Nevermind, its updated now! Thanks I'll try this out!

moodymudskipper commented 2 years ago

These diagrams become ugly when variables are overwritten. The most frequent case I can think of are :

The former are plainly overwritten by tmp <- new_val statements

The latter I believe are mostly grown through fun(x,...) <- new_val statements where fun will often be [, [[, $ or `names.

When x is overwritten we can show it in the diagram as x* ( then x** and so on) . In the latter case x* should depend on x. In the former not, unless x is also in the rhs.

This might actually simplify the diagrams a lot of times, and will reflect the chronology of changes, which we don't have now.

This should be optional but might be the default. The behavior for the two cases above could be switched independently.

moodymudskipper commented 2 years ago

I hope by handling this temp variable issue we'll be also able to see clusters of interacting variables in the diagram. An isolated cluster shows code with logic independent of the rest of the function, i.e. code that can be refactored into a separate function.

jpkrooney commented 2 years ago

Oh that would be a neat feature!

moodymudskipper commented 2 years ago

This can be closed now since flow_view_vars() is included in latest CRAN version

github-actions[bot] commented 1 year 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.