[R] problem in deparse(substitute())

Uwe Ligges ligges at statistik.uni-dortmund.de
Wed Sep 18 14:31:27 CEST 2002


vito muggeo wrote:
> 
> Hi all,
> I am experiencing the following quite strange (at least in my knowledge)
> problem in a simple function like the following:
> 
> fn<-function(y,v=2){
>     n<-length(y)
>     y<-y[(v+1):(n-v)]
>  plot(y,type="l",lty=3,xlab="Time",ylab=deparse(substitute(y)))
>     }
> 
> fn(rnorm(50)) #look at the plot!!!
> 
> The plot appears with numbers on the left side!
> If I delete the deparse(substitute()) in the ylab argument in the plot()
> function
> or I omit the line "y<-y[(v+1):(n-v)]" everything works well.
> 
> I can not test whether this problem depends on my R system (R 1.5.1 Win Me),
> but I have never had no problem before.
> 
> many thanks for your help,


Read ?substitute completely and you will see that the solution is:

  fn <- function(y, v=2){
      dep.sub.y <- deparse(substitute(y))
      n <- length(y)
      y <- y[(v + 1):(n - v)]
      plot(y, type = "l", lty = 3, xlab = "Time", ylab = dep.sub.y)
  }

Note how wonderful readable the code above is.

Uwe Ligges
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list