[R] Tcl Tk table

Peter Dalgaard p.dalgaard at biostat.ku.dk
Mon Apr 26 15:53:40 CEST 2004


James Wettenhall <wettenhall at wehi.edu.au> writes:

> If I teach myself to avoid .Tk.ID() as much as possible in 
> order to improve my R-Tcl/Tk coding style, should I also try to 
> avoid tclvalue() as much as possible?  (I'm pretty sure the 
> answer is "No".)  They seem similar in that they both convert 
> a Tcl/Tk object into a string, but while .Tk.ID() is 
> unnecessary/wrong for the example in this thread, tclvalue() 
> is still necessary for this:
> foo <- tclVar("one two")
> tkmessageBox(message=foo)           # Doesn't work :(
> tkmessageBox(message=tclvalue(foo)) # Does work :)
> 
> So this analogy is my current excuse for mistakenly 
> using .Tk.ID() when it isn't needed.

OK, let's remove your excuse then... ;-)

Notice that tclVar objects are like pointers: They represent the
variable rather than its content. I.e. tclvalue on the R side
corresponds to a "$" on the Tcl side:

> library(tcltk)
> foo <- tclVar("one two")
> foo
$env
<environment: 0x907109c>

attr(,"class")
[1] "tclVar"
> ls(foo$env)
[1] "::RTcl1"
> tkcmd("list",fee=foo)
<Tcl> -fee ::RTcl1
> tkcmd("list",fee=tclvalue(foo))
<Tcl> -fee {one two}

Notice that you really do need to pass the variable sometimes, e.g. 

b <- tkcheckbutton(tt,variable=foo)

in which case you do not want tclvalue(foo) unless you managed to
create a variable called "one two". 

So you can't do without tclvalue, but you might want to replace it
with tclObj, which removes the detour via the string rep.:

> tkcmd("list",fee=tclvalue(foo))
<Tcl> -fee {one two}
> tkcmd("list",fee=tclObj(foo))
<Tcl> -fee {one two}


Somewhat more confusing is the relation between tclvalue and
as.character, e.g.

> tclvalue(tclObj(foo))
[1] "one two"
> as.character(tclObj(foo))
[1] "one" "two"

This reflects Tcl's distinction between lists and their string
representation. (With hindsight, I suspect that this use of tclvalue
might better have been avoided in favour of as.string.tclObj() or
similar.)
 
(I'll get back to you on the doc issues). 

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




More information about the R-help mailing list