29

Is there any way to programmatically tell if a given function in has standard evaluation, and if not, which component of function evaluation –

  • parsing,
  • matching,
  • scoping,
  • promise formation,
  • promise fulfillment,
  • return,

etc. – is non-standard? I understand that closures are likely to be standard, and primitives are likely to be non-standard, but there are exceptions both ways. I’m asking about determining whether the function semantics are standard with respect to each of these things, not whether the function mechanics are standard.

I assume these things ought to be derivable from a close and careful reading of the help page, and failing that the code, and failing that any referenced source code. But it would save me a great deal of grief if I had a mechanical way of quickly identifying non-standard features in the evaluation of a given function.

If there is not a way to programmatically identify all the ways in which a function is nonstandard, are there ways to test for any aspect of standardness?

Artem
  • 3,304
  • 3
  • 18
  • 41
andrewH
  • 2,281
  • 2
  • 22
  • 32
  • 5
    Can you give some examples of existing functions and tell us how you think they should be classified? And why is this important? I doubt that short of an AI that can read the help pages and figure it out there is any way of doing this for certain from code introspection, especially in an OO system where foo(bar) could go just about anywhere. – Spacedman Feb 17 '18 at 10:37
  • 5
    Since the `$` function uses nonstandard eval, it's going to be difficult to find very many functions that are totally free of NSE. – IRTFM Mar 31 '20 at 18:33
  • 1
    "_I assume these things ought to be derivable from a close and careful reading of the help page_". [Good luck with that](https://stackoverflow.com/q/66907140/10319707). I can't even find where the cursed things are documented. – J. Mini Apr 07 '21 at 13:44

1 Answers1

1

Quick way to check for non-standard evaluation (NSE) is to verify if certain keywords are used, e.g. substitute, eval, deparse etc. Please see the code below. It looks into the body of function and count how many times NSE-related keywords are used.

is_nse <- function(x) {
  nse_criteria <- c("substitute", "deparse", "eval", "parent.frame", "quote")
  code <- as.character(body(x))
  print(x)
  cat("-------------------------------------------------------------------\n")
  nse_count <- sapply(nse_criteria, function(x) sum(grepl(x, code)))
  if(sum(nse_count) > 0) 
    warning("Possible non-standard evaluation")
  nse_count
}

is_nse(as.Date.default)

Output:

function (x, ...) 
{
    if (inherits(x, "Date")) 
        x
    else if (is.null(x)) 
        .Date(numeric())
    else if (is.logical(x) && all(is.na(x))) 
        .Date(as.numeric(x))
    else stop(gettextf("do not know how to convert '%s' to class %s", 
        deparse1(substitute(x)), dQuote("Date")), domain = NA)
}
<bytecode: 0x0000021be90e8f18>
<environment: namespace:base>
-------------------------------------------------------------------
  substitute      deparse         eval parent.frame        quote 
           1            1            0            0            0 
Warning message:
In is_nse(as.Date.default) : Possible non-standard evaluation
Artem
  • 3,304
  • 3
  • 18
  • 41