getName | R Documentation |
This is a utility to be used inside a function.
getName(x, object = FALSE)
x |
String, expression to be evaluated |
object |
Logical, return the object's name |
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.
A character vector of length 1.
Adrian Dusa
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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.