isParameter: Determine if a variable name is a function parameter or a...

isParameterR Documentation

Determine if a variable name is a function parameter or a local variable

Description

These functions allow one to query whether a variable name corresponds to either a formal argument/parameter of a given function or to a variable that is created locally in the body of the function, or neither.

Usage

isParameter(p, fun)
isLocalVar(p, fun, notParam = TRUE)

Arguments

p

the name of the variable to query

fun

the R function

notParam

a logical value related to how we treat parameters/formal arguments that are also local variables. A parameter which has no local assignments to its name will not be considered a local variable. However, if there is one or more assignments to that parameter name in the body of the function, notParam determines whether isLocalVar returns TRUE for a local variable or not in this case. If this is TRUE, then the parameter is never considered a local variable. If this is FALSE, then a variable name that corresponds to a parameter and has local assignments within the body is considered a local variable so the result will be TRUE.

Value

A logical value

Author(s)

Duncan Temple Lang

See Also

findAssignsTo formals

Examples

  f = function(x, y) {
     len = length(x)
     w = !is.na(x)
     (x[w] + y[w])/len
  }
  isLocalVar("x", f)
  isLocalVar("x", f, FALSE)
  isLocalVar("w", f)
  isLocalVar("len", f)

  isParameter("x", f)
  isParameter("len", f)
  isParameter("other", f)

duncantl/CodeAnalysis documentation built on Feb. 21, 2024, 10:49 p.m.