Rtcl

Difference between version 9 and 10 - Previous - Next
Rtcl is a [Tcl] extension for executing code in the [R] [http://www.r-project.org]
programming language. R is a free version of Bell Labs' S language for
statistical computing. Rtcl embeds an R interpreter into Tcl, and allows
access to it via Tcl commands. The latest version of Rtcl is available from

https://github.com/mattadams/Rtcl

Rtcl was originally written by [Neil McKay]. His last published version is available from

http://www.eecs.umich.edu/~mckay/computer/Rtcl0.3.tar.gz

----


***Installation on macOS***
[TR] I have successfully compiled and used the package on macOS Catalina (10.15.7), using Rtcl version 1.2.1. R version 4.0.4 is installed and lives under /Library/Frameworks/R.framework/Resources. This is the directory returned by

    R RHOME
Compiling Rtcl using just with `./configure; make; make install` works. However, I am getting the error message `cannot load libR` from the Rtcl package when I do `package require Rtcl`. This is because Rtcl cannot find the installed R. First, the `R_HOME` environment variable is not set by R, so the initialization script in Rtcl cannot use it. Second, the paths to R hard-coded into Rtcl do not match my installation, so Rtcl cannot find R at that place either. It doesn't help much to add the correct path in Rtcl's init code. This will help loading the library in Tcl but then R fails with saying `cannot find system Renviron`. It turns out that the easy solution is to just define the `R_HOME` environment variable and set it to the output of `R RHOME`. Depending on your setup, you can just put it into your profile:

    export R_HOME=`R RHOME`

You could also include it into the pkgIndex.tcl file of Rtcl, before the actual libRtcl1.2.1.dylib library is loaded, e.g.:

    set env(R_HOME) [exec /usr/local/bin/R RHOME]
    package ifneeded Rtcl 1.2.1 \
        [list load [file join $dir libRtcl1.2.1.dylib] Rtcl]


----

***Usage***
The package provides four new commands in the rtcl [namespace]:

    ::rtcl::eval ?-verbose? R Expression(s)
    ::rtcl::source ?-verbose? foo.R
    ::rtcl::gettype ?-verbose? R Expression(s)
    ::rtcl::getvalue ?-verbose? R Expression(s)

Here's an example output from the console:

    % package require Rtcl
    1.2.1
    % # the following will just execute some R code without returning anything:
    % ::rtcl::eval "a <- c(1:10)"
    % # if you want to see the content of 'a', you need:
    % ::rtcl::getvalue a
    1 2 3 4 5 6 7 8 9 10
    % # getvalues does also execute the command, so you can do both in one step:
    % ::rtcl::getvalue "a <- c(1:10)"
    1 2 3 4 5 6 7 8 9 10
    % # loading R libraries works as usual:
    % ::rtcl::eval library(sf)
    % we can transfer data to R and get results back like this:
    % set x 5
    % set result [::rtcl::getvalue "$x * $x"]
    % 25.0
    % puts $result
    25.0
    % note, that Rtcl returned a 'real' value:
    % ::rtcl::gettype "$x * $x"
    real
    % ... when doing this in plain R, you will get "25" and 'class(5*5)' will return only 'numeric', so 'real' is the safe choice ...




<<categories>> Package | Mathematics | Statistics