Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
HenrikBengtsson opened this issue May 7, 2016 · 2 comments

Comments

@HenrikBengtsson
Copy link
Collaborator

The codetools package does indeed handle some non-standard evaluation (NSE) expressions, e.g. library() without quotes:

> 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() it sets up:

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:

> 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)
}
<bytecode: 0x000000000b844fd0>
<environment: namespace:codetools>

There are several usage handlers set up this way:

> 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"
@HenrikBengtsson
Copy link
Collaborator Author

Looking at the usage handler for base::with() we see that it is conditioned on a setting skipWith, which cannot be set via codetools::findGlobals():

addCollectUsageHandler("with", "base", function(e, w) {
    w$enterGlobal("function", "with", e, w)
    if (identical(w$skipWith, TRUE))
        walkCode(e[[2]], w)
    else collectUsageArgs(e, w)
})

This is why we get:

> codetools::findGlobals(function() with(df, sum(a)))
[1] "a"    "df"   "sum"  "with"

However, we can override this as:

codetools:::addCollectUsageHandler("with", "base", function(e, w) {
    w$enterGlobal("function", "with", e, w)
    walkCode(e[[2]], w)
})

such that only the first argument is checked:

> codetools::findGlobals(function() with(df, sum(a)))
[1] "df"   "with"

@HenrikBengtsson
Copy link
Collaborator Author

Importantly, addCollectUsageHandler() completely ignores the 2nd argument (where) right now:

# 'where' is ignored for now
addCollectUsageHandler <- function(v, where, fun)
    assign(v, fun, envir = collectUsageHandlers)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant