gpf
gpf

Reputation: 51

get the name of the output variable in R

sorry for my poor english

Is there a way in R to get the name used to the returning values of a function within the function, the same way you can catch the name of the input variables with "substitute"??. I mean something like this "outputname" function:

myFun=function(x){
  nameIN=substitute(x)
  nameOUT=outputname()
  out=x*2
  cat("The name of the input is ", nameIN,"   and this is the value:\n")
  print(x)
  cat("The name of the output is ", nameOUT, "and this is the value:\n")
  print(out)
  return(out)
}

This is what I wish:

> myINPUT=12;
> myOUTPUT=myFun(myINPUT)
The name of the input is  myINPUT and this is the value:
[1] 12
The name of the output is  myOUTPUT and this is the value:
[1] 24


> myOUTPUT
[1] 24

I've been looking for an answer and I am going crazy. It seems something so simple but I can't find anything.

Thanks

Upvotes: 5

Views: 2035

Answers (2)

gpf
gpf

Reputation: 51

That's not exactly what I was looking for but those are good solutions. I had another idea.. give the name of the output as an argument and then, assign the value to it ussing "assign(outPUT_name,out,envir=parent.frame())".

myFun=function(x,outPUT_name){
  nameIN=substitute(x)
  out=x*2
  cat("The name of the input is ", nameIN,"   and this is the value:\n")
  print(x)
  cat("The name of the output is ", outPUT_name, "and this is the value:\n")
  print(out)
  assign(outPUT_name,out,envir=parent.frame())
}

Then you could use it like this:

myFun(myINPUT,'myOUTPUT')

May be I am a little bit capricious but I wanted not to have to add the output name as an argument... it's a shame there's no way to acomplish that

Thank you very much

Upvotes: 0

Matthew Plourde
Matthew Plourde

Reputation: 44624

Here are two workarounds from the comments. This first uses environments to pass by reference. The output variable is supplied as an argument to myFun1. The second uses assign to assign the return value of myFun2 to the output variable and retrieves the name of the ouput variable by examining the call stack.

myINPUT <- 12

Workaround 1

myFun1 <- function(x, output){
  nameIN=substitute(x)
  nameOUT=substitute(output)
  output$value=x*2
  cat("The name of the input is ", nameIN,"   and this is the value:\n")
  print(x)
  cat("The name of the output is ", nameOUT, "and this is the value:\n")
  print(output$value)
}

myOUTPUT <- new.env()
myOUTPUT$value <- 1
myFun1(myINPUT, myOUTPUT)
# The name of the input is  myINPUT    and this is the value:
# [1] 12
# The name of the output is  myOUTPUT and this is the value:
# [1] 24
myOUTPUT$value
# [1] 24

Workaround 2

Suggested by @Roland (my interpretation of his comment, at least):

myFun2=function(x){
  nameIN=substitute(x)
  nameOUT=as.list(sys.calls()[[1]])[[2]]
  out=x*2
  cat("The name of the input is ", nameIN,"   and this is the value:\n")
  print(x)
  cat("The name of the output is ", nameOUT, "and this is the value:\n")
  print(out)
  return(out)
}

assign('myOUTPUT', myFun2(myINPUT))
# The name of the input is  myINPUT    and this is the value:
# [1] 12
# The name of the output is  myOUTPUT and this is the value:
# [1] 24
myOUTPUT
# [1] 24

Upvotes: 2

Related Questions