[R] problem in deparse(substitute())

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Wed Sep 18 12:33:47 CEST 2002


"vito muggeo" <vito.muggeo at giustizia.it> writes:

> 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,

That's because you changed y before the substitute(). Once you have changed
it, it has no connection with the formal argument and is just a vector
of numbers, which when deparsed gives a label like
"c(34.159,45.6789,....)". 

You need something like

yl<-deparse(substitute(y))
y<-y[(v+1):(n-v)]
plot(y,type="l",lty=3,xlab="Time",ylab=yl)


-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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