getName: Get the name of the object being used in a function call

getNameR Documentation

Get the name of the object being used in a function call

Description

This is a utility to be used inside a function.

Usage

getName(x, object = FALSE)

Arguments

x

String, expression to be evaluated

object

Logical, return the object's name

Details

Within a function, the argument x can be anything and it is usually evaluated as an object.

This function should be used in conjunction with the base match.call(), to obtain the original name of the object being served as an input, regardless of how it is being served.

A particular use case of this function relates to the cases when a variable within a data.frame is used. The overall name of the object (the data frame) is irrelevant, as the real object of interest is the variable.

Value

A character vector of length 1.

Author(s)

Adrian Dusa

Examples

foo <- function(x) {
    funargs <- sapply(match.call(), deparse)[-1]
    return(getName(funargs[1]))
}

dd <- data.frame(X = 1:5, Y = 1:5, Z = 1:5)

foo(dd)
# dd

foo(dd$X)
# X

foo(dd[["X"]])
# X

foo(dd[[c("X", "Y")]])
# X Y

foo(dd[, 1])
# X

foo(dd[, 2:3])
# Y Z

admisc documentation built on Sept. 12, 2024, 6:27 a.m.

Related to getName in admisc...