Updated 2014-05-12 01:04:32 by RLE

See also the alphabet.

Interfacing R and Tcl edit

What: R
Where: http://www.r-project.org/
Description: Statistical computing and visualization language, similar to the S language. R is "a language and environment for statistical computing and graphics" (http://www.r-project.org/about.html).
For more background on R, see the February 2003 installment of "Server Clinic".
Provides a wide variety of techniques. Comes with a tcltk package, providing interface and language bindings for Tcl/Tk. Makes use of plplot.
Note that Mark Myatt mailto:[email protected] is writing a syntax highlighting editor to be used as a front-end of R; contact him for details.
Currently at version 2.6.0.
Updated: 11/2007
Contact: See web site

See also the article series here [1] and here [2].

R has bindings to Tcl, Tk, and Visual Tcl through the R packages 'tcltk' and 'vtcl'. The 'tcltk' package creates and uses tcl/tk interpreters from R. This tcl interpreter inside R has an additional command 'R some R commands' which can execute R commands, e.g, 'R a<-rnorm(100)'. The 'vtcl' package contains only one function 'visual.tcl()' which enables the user to run Visual Tcl from inside R. (See the original announcement here: [3])

Seductive screen-shots appear at http://www.bioconductor.org/Screenshots/index.html (link is 404 on 2012-06-10), for example.

Brodgar has an R component.

[Peter Dalgaard] says in comp.lang.tcl that http://CRAN.R-project.org/doc/Rnews/ should be used as a source for more recent information; particularly in vol 1/3 and 2/3.

He goes on to say:
"What we currently have is a Tcl interpreter embedded in R and interface routines and (some) object reflection and communication mechanisms, which allow you to run Tcl stuff from within R.
What we don't have is the reverse: Mechanisms that let you control R from Tcl. Well, we do have some rudiments of it, but not as elaborate as in the other direction. In principle R is embeddable; you can build R as a shared object and link it into another program, but you probably need to jump through a few hoops to get event loops and such stuff right. (I believe http://developer.r-project.org/embedded.html is the best documentation we have.)"

That was the situation in early 2003. A few hours before the year ended, though, Neil McKay announced availability of Rtcl.

samoc: To access R functions from Tcl I use this simple wrapper:
proc R {script} {
    # Execute "R" script.

    set script "options(warn=-1); $script"
    exec R --vanilla --slave << [uplevel [list subst $script]] 2>@1

proc R.c {args} {
    # Convert Tcl List to "R" vector.

    return c([join $args ,])

proc R.call {r_function args} {
    # Call "r_function" with "args".
    # Result is a Tcl dictionary.

    R {format ($r_function ([join $args ,]))}

set a [R.c 71.1 31.4 15.4 11.3 27 14.8 4.8]
set b [R.c 73.3 46.7 19.5 41 31.4 23.8 7.5]

puts [R {cat(wilcox.test($a, $b)\$p.value)}]

set r [R.call wilcox.test $a $b]

puts "p = [dict get $r p.value]"
puts "W = [dict get $r statistic]"

References, etc.

AM (14 november 2005) I saw this article this morning:

The R commander: A basic-statistics graphical user interface to R

Fox, J


Unlike S-PLUS, R does not incorporate a statistical graphical user interface (GUI), but it does include tools for building GUIs. Based on the tcltk package (which furnishes an interface to the Tcl/Tk GUI toolkit), the Rcmdr package provides a basic-statistics graphical user interface to R called the "R Commander." The design objectives of the R Commander were as follows: to support, through an easy-to-use, extensible, cross-platform GUI, the statistical functionality required for a basic-statistics course (though its current functionality has grown to include support for linear and generalized-linear models, and other more advanced features); to make it relatively difficult to do unreasonable things; and to render visible the relationship between choices made in the GUI and the R commands that they generate. The R Commander uses a simple and familiar menu/dialog-box interface. Top-level menus include File, Edit, Data, Statistics, Graphs, Models, Distributions, Tools, and Help, with the complete menu tree giv

Link: http://socserv.mcmaster.ca/jfox/Misc/Rcmdr/

GS (061016) Slides about a talk: The R-Tcl/Tk interface: Potential usage for graphical models, Peter Dalgaard. [4] (Link is 404 on 2012-06-10)

And an article by the same author: The R-Tcl/Tk interface, Peter Dalgaard. [5]

At the implementation level, R is a very interesting language with an approach which is very close to Tcl, yet implemented in a totally different way. In an expression such as "a = myfunc(1,b+2)" the arguments are passed in unevaluated form, sort of as a parse tree AFAIK. Inside myfunc, each argument is named, but the association is such that the first access to each argument will actually evaluate it (in the caller's context, of course). So in day-to-day use, the behavior of a function call in R is relatively normal.

The point is that it is possible to get at the incoming unevaluated form and that an argument can be force-evaluated more than once, with side effects and all. Therefore - like Tcl - R can use normal functions to implement control structures: "if(a<b,c=1,c=2)", since only the selected argument will be evaluated. Likewise for "while" and so on.

Like Tcl, R uses copy-on-write (COW) semantics, reference counts, and data sharing.

There is a book which describes S (the non-OSS precursor of R) called "Programming with Data", by John. M. Chambers. It lists S's delightfully concise goal: To turn ideas into software, quickly and faithfully.


[6] PBS Modelling provides software to facilitate the design, testing, and operation of computer models. It focuses particularly on tools that make it easy to construct and edit a customized graphical user interface (GUI). Although it depends heavily on the R interface to the Tcl/Tk package, a user does not need to know Tcl/Tk.

See also edit