Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 532
RockScience Avatar asked Sep 14 '16 05:09

RockScience


People also ask

How do you call a function in a string?

There are two methods to call a function from string stored in a variable. The first one is by using the window object method and the second one is by using eval() method. The eval() method is older and it is deprecated.

How do I convert a function to a string in Python?

How to Convert Python Int to String: To convert an integer to string in Python, use the str() function. This function takes any data type and converts it into a string, including integers. Use the syntax print(str(INT)) to return the int as a str , or string.

How do you call a JavaScript function name?

The JavaScript call() Method The call() method is a predefined JavaScript method. It can be used to invoke (call) a method with an owner object as an argument (parameter). With call() , an object can use a method belonging to another object.

How do you convert a string to a variable in Python?

Instead of using the locals() and the globals() function to convert a string to a variable name in python, we can also use the vars() function. The vars() function, when executed in the global scope, behaves just like the globals() function.


1 Answers

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

like image 88
akrun Avatar answered Sep 30 '22 08:09

akrun