1

If I do this:

parse(text="foo()")

I get:

expression(foo())

But what I really need is:

foo()

So basically "foo()" -> foo()

Also, if I try:

as.symbol("foo()")

I get:

`foo()`

I don't want the tick marks

  • 1
    Try `eval(as.symbol("foo"))`. – Rui Barradas Jan 09 '21 at 08:40
  • You can try 'get' function or 'eval' to work with parse output. I am not sure do u want to print the function without quotes or evaluate something which is under quotes? Use noquote function on strings to print without quotes. Or print with quote= FALSE – PKumar Jan 09 '21 at 08:41
  • 1
    First, you shouldn't use parse like this. Using parse is not proper meta-programming. Then: parse(text="foo()")[[1]] gives you the call. – Roland Jan 09 '21 at 09:20
  • It is not clear what you are trying to achieve. Evaluate function? (I think not looking at your comments) Print name? But where? To file, to console? What problem you want to solve? `cat("foo()")` writes `foo()` to console, but it is just printing things. – Marek Oct 27 '22 at 12:29

2 Answers2

1

Something like this?

foo <- function(x) pi*x
eval(as.symbol("foo"))(1:10)
#[1]  3.141593  6.283185  9.424778 12.566371 15.707963 18.849556 21.991149
#[8] 25.132741 28.274334 31.415927

The return value of eval(.) can be assigned to an object and it will be a function.

f <- eval(as.symbol("foo"))
f
#function(x) pi*x

f(2)
#[1] 6.283185
Rui Barradas
  • 70,273
  • 8
  • 34
  • 66
0
parse(text="foo()") %>% as.character %>% noquote
[1] foo()
Karl Edwards
  • 305
  • 4