[R] as.numeric with tclvalue redux

Sundar Dorai-Raj sundar.dorai-raj at pdf.com
Mon Mar 24 19:05:23 CET 2008



Erin Hodgess said the following on 3/24/2008 10:39 AM:
> Hi again R People:
> 
> This works fine:
>> library(tcltk)
>> a <- tclVar("4.5")
>> as.numeric(tclvalue(a))
> [1] 4.5
>> #But if you have:
>> b <- tclVar("pi")
>> as.numeric(tclvalue(b))
> [1] NA
> Warning message:
> NAs introduced by coercion
> 
> Is anyone aware of a way around this, please?
> 
> thanks,
> Erin
> 
> 

Does this help?

eval.tclvalue <- function(x, ...) {
   x <- type.convert(tclvalue(x), as.is = TRUE)
   if(is.character(x) && exists(x, ...)) {
     get(x)
   } else {
     x
   }
}

a <- tclVar("4.5")
b <- tclVar("pi")
c <- tclVar("abcd")

eval.tclvalue(a)
eval.tclvalue(b)
eval.tclvalue(c)

HTH,

--sundar



More information about the R-help mailing list