as.numeric with tclvalue redux

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

as.numeric with tclvalue redux

Erin Hodgess-2
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


--
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: as.numeric with tclvalue redux

Sundar Dorai-Raj


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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: as.numeric with tclvalue redux

Peter Dalgaard
In reply to this post by Erin Hodgess-2
Erin Hodgess wrote:

> 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?
>
>  
Brian already told you: Parse and eval.

 > eval(parse(text=as.character(tclvalue(tclVar("pi")))))
[1] 3.141593

(Beware: Depending on the environment in which you eval(), strange
things can happen if you use the name of an internal variable in your
function)

What happened with the Inf issue? I can't reproduce that:

 > as.numeric(as.tclObj("Inf"))
[1] Inf

--
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.
Reply | Threaded
Open this post in threaded view
|

Re: as.numeric with tclvalue redux

Erin Hodgess-2
Thank you!


On 3/24/08, Peter Dalgaard <[hidden email]> wrote:

> Erin Hodgess wrote:
> > 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?
> >
> >
> Brian already told you: Parse and eval.
>
>  > eval(parse(text=as.character(tclvalue(tclVar("pi")))))
> [1] 3.141593
>
> (Beware: Depending on the environment in which you eval(), strange
> things can happen if you use the name of an internal variable in your
> function)
>
> What happened with the Inf issue? I can't reproduce that:
>
>  > as.numeric(as.tclObj("Inf"))
> [1] Inf
>
> --
>   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
>  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
>  (*) \(*) -- University of Copenhagen   Denmark      Ph:  (+45) 35327918
> ~~~~~~~~~~ - ([hidden email])              FAX: (+45) 35327907
>
>
>


--
Erin Hodgess
Associate Professor
Department of Computer and Mathematical Sciences
University of Houston - Downtown
mailto: [hidden email]

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.