user5433002
user5433002

Reputation:

Getting name of variable in r

a<-1:10

anyfunction<-function(data)

{

*********some function***********

}

>anyfunction(data=a)

output i want to get something like this

a

***value processed by function***

i want to print whatever the function does under the name of 'a' (here 'a' can be any variable name). any way to get this in R ?

one more thing i want to add to make this post clear is that i want to extract the name of variable as the row names of the output data

somethings like

anyfunction(data=a)
a ****output generate by function****        #here 'a' represents row names of the output

sample example

myfunction<-function(data)
{
data=data+1
return(data)
}


a<-1:4
>myfunction(data=a)

Expected output

a  2 3 4 5 

Upvotes: 2

Views: 2352

Answers (1)

Roland
Roland

Reputation: 132576

Last try:

Create an S3-class with a print method.

anyfunction<-function(data){
  res <- data + 1
  attr(res, "var") <- deparse(substitute(data))
  class(res) <- c("strange", class(res))
  res
}

print.strange <- function(x) {
  cat(attr(x, "var"), x, "\n")
}

anyfunction(a)
# a 2 3 4 5 6 7 8 9 10 11 

Upvotes: 2

Related Questions