Search code examples
rcallstackcoercion

coerce a function call into a string


I am trying to understand what is a call object in R and to coerce it to characters. However my efforts have been vain so far.

myFun=function(a=1) {   x=sys.call()   return(as.character(x)) }

x=myFun(a=2) # here I would like to get the string "myFun(a = 2)"

I have also been looking for the function that prints a function call (something like print.call). But I couldn't find it.

Does anybody here knows how call objects are printed?


Solution

  • We can use match.call() with deparse

    myFun <- function(a=1) { 
                deparse(match.call())       
      }
    
    myFun(a=2)
    #[1] "myFun(a = 2)"
    

    Or replace match.call() with sys.call() in the function