futureverse / futureverse/globals

Add custom codetools usage handlers for non-standard evaluation (NSE)?

Open
#12 2 comments 0 reactions 0 assignees View on GitHub
enhancement question
Dominant language
R
Stars
29
Forks
3
PR merge metrics
No merged PRs in 30d

Description

The [codetools](https://github.com/cran/codetools/) package does indeed handle some non-standard evaluation (NSE) expressions, e.g. `library()` without quotes:

``` r
> codetools::findGlobals(function() library(tools))
[1] "library"
```

Note how `tools` is _not_ identified as a global/unknown; this is because codetools knows how `library()` works.

This is done by (internal) usage-handler functions, e.g. for [`base::library()`](https://github.com/cran/codetools/blob/master/R/codetools.R#L697-L701) it sets up:

``` r
addCollectUsageHandler("library", "base", function(e, w) {
w$enterGlobal("function", "library", e, w)
if (length(e) > 2)
for(a in dropMissings(e[-(1:2)])) walkCode(a, w)
})
```

This can be retrieved as:

``` r
> codetools:::collectUsageHandlers$library
function (e, w)
{
w$enterGlobal("function", "library", e, w)
if (length(e) > 2)
for (a in dropMissings(e[-(1:2)])) walkCode(a, w)
}

```

There are several usage handlers set up this way:

``` r
> ls(codetools:::collectUsageHandlers)
[1] "$" "$<-" "::" ":::"
[5] "@" "@<-" "{" "~"
[9] "<-" "<<-" "=" "assign"
[13] "binomial" "bquote" "data" "detach"
[17] "expression" "for" "function" "Gamma"
[21] "gaussian" "if" "library" "local"
[25] "poisson" "quasi" "quasibinomial" "quasipoisson"
[29] "quote" "Quote" "require" "substitute"
[33] "with"
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.