R

See also the alphabet.


Interfacing R and Tcl

What: R
Where: http://www.r-project.org/
http://cran.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 ). Comes with a tcltk package, providing interface and language bindings for Tcl/Tk. Makes use of various plotting outputs.
Updated: 4/2018
Contact: See web site

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: [L1 ])

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 [L2 ] 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.

MattAdams continued Neil's work and made a 1.0 release in April 2018. You can obtain the most recent Rtcl extension at https://github.com/mattadams/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 ,]))}
}

e.g.

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:

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

Authors:
Fox, J

Source:
JOURNAL OF STATISTICAL SOFTWARE, 14 (9): - SEP 2005

Abstract:
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. [L3 ] (Link is 404 on 2012-06-10)

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


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.

-jcw


[L5 ] 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


{tickle} [L6 ] is a package for creating UIs in base R.

This is an opinionated abstraction over the base R package {tcltk}.

This package aims to provide:

  • a simplified UI creation process similar to shiny.
  • a modern-looking visual default.