Scanning files for side effect functions

I've found and I'm very excited with the Hadley's "conflicted" package that does some magic scanning source code and revealing possible conflicts like stat::filter and dplyr::filter. It enforces you to explicitly specify what function do you need and prevents you from making very unpleasant and non-obvious bugs.

I wonder if there is something similar that scans your files and detects functions with occasionally missed side effects.

Definition

"pure functions have no side effects : they don’t affect the state of the world in any way apart from the value they return." (http://adv-r.had.co.nz/Functions.html)

It can be very handy considering the fact that there is still missing any reliable tool in RStudio like in Intellij IDEA helping you to extract functions properly. I know the existing one but it is still not so good.

If no, is it a good idea to suggest such a package to be considered to Hadley?

1 Like

Thanks for introducing me to conflicted

The scanner will look at the loaded packages (which automates paying attention to the order of loading and the masked by messages). I suppose it wouldn't be too hard to automate script loading, library listing and scanning, but the conflicted engine right now only does functions in packages, not user-defined, if that's what you're looking for. Among the thousands of packages with tens of thousands of function, it would be a tall order to scan for all possible conflicts.

Here's what doesn't happen when you add a user-defined function with a namespace conflict to a loaded library

> lag <- function(x) {x}
> lag(3.14)
[1] 3.14
> conflict_scout(pkgs = NULL)
10 conflicts:
* `combine`  : gridExtra, dplyr
* `filter`   : dplyr, stats
* `intersect`: [dplyr]
* `lag`      : dplyr, stats
* `lift`     : caret, purrr
* `Position` : ggplot2, base
* `setdiff`  : [dplyr]
* `setequal` : [dplyr]
* `summary`  : [RMySQL]
* `union`    : [dplyr]
> 

See? It ignored the duplication of lag.

I think the closest I can come to what you looking for among a directory tree of scripts is a descending parser to create a hash of file:library and grep out library(), write to tmp.R, run
conflict_scout(pkgs = NULL) and write back the output report as a comment to the related script.

1 Like

Thank you for your reply. I understood your explanations on how conflicted works

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.