[R] R_eval in tcltk library

Peter Dalgaard BSA p.dalgaard at biostat.ku.dk
Tue Oct 24 22:55:37 CEST 2000


"Samak, Vele" <vele.samak at ssmb.com> writes:

> Is there a method with the current tcltk library in R to call back an R
> function within the .Tcl("...") interpreter? 
> 
> I can't find anything documented, and I don't understand the usefulness of
> the .Tcl.callback() function. Browsing through the tcltk source code I found
> two functions R_eval and R_call that seem to take input from the .Tcl
> interpreter and parse them through the R evaluator. Unfortunately I get
> Segmentation Fault (core dump) whenever I execute .Tcl("R_eval") or
> .Tcl("R_call"). Also, the results from these functions appears to be lost
> somewher. Both functions in the c code capture the result of eval in a
> variable called ans, but that variable doesn't go anywhere apparently. 
> 
> If someone has examples of calling R functions from .Tcl I would greatly
> appreciate it. Thanks,

None of this stuff is really cast in stone at this point, but here's a
brief sketch:

R_call and .Tcl.callback belong together and it hardly makes sense to
use R_call from raw Tcl code. Their main function is to make R
callbacks from widgets work, i.e.

tkbutton(buttonframe, text = "Quit", command = function() tkdestroy(base))

will make the Tcl command to create the button widget contain
something like "-command { R_call 0x408546a0 }" where the hex code is
the memory address of the R function. Some further trickery allows the
use of %x-style arguments as some types of callbacks want.

R_eval actually does something useful, although it is really a remnant
from some earlier experiments and not currently used for anything. It
does require an argument though and it has no protection against missing
arguments, which explains your segfault. What you can do is e.g.

> .Tcl("R_eval {print(2+2)}")
[1] 4

You cannot at present get the result returned (and in many cases the
complexity of the R object would probably not allow it to be
represented in the string form required by Tcl), but you could
communicate via a global Tcl variable, like in

> (.Tcl("R_eval {tclvar$four <- 2+2} ; format $four"))
[1] "4"

(The extra () there has to do with the assignment setting the
invisible flag.)

If you take a look at the development sources, you'll find that some
new stuff has been added by Thomas Lumley, showing some incorporation
of larger-scale Tcl codes, which you might want to take a closer look
at. [Unless you are on a very short deadline with your project, you
might want to shift over to the development version entirely. It is
expected to become the next stable version (1.2.0) around December
15.] 

As I said, this is very much experimental code, and very likely quirks
will pop up as soon as people try using it for anything substantial.
Suggestions and bug reports are welcome. 

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