0

Code below.

  func <- function(x){ 
print( paste(substitute(x)) )
 } ; 
func(x[])

If I run func(x), the code print "x" out, but if I run like func(x[]) or func(x$y), it cannot print "x[]" or "x$y" out correctly. How can I get the correct input string? Thank you.

Similar example is like plot() function, when I use plot(x$y), the ylab is "x$y". When I use plot(x[]), the ylab is "x[]". When I use plot(1:20), the ylab is "1:20".

Lele Shu
  • 63
  • 1
  • 6

1 Answers1

4

Use deparse as well.

func <- function(x){
    deparse(substitute(x))
}

which gives

> func(x[1])
[1] "x[1]"
> func(x[])
[1] "x[]"
Dason
  • 60,663
  • 9
  • 131
  • 148