Skip to content
Prev 37347 / 63424 Next

Adding Tcl source to an R package

On Tue, Aug 10, 2010 at 04:13:18PM -0400, Adrian Waddell wrote:

            
Whether you use Tcl namespaces or not is up to you and your Tcl code,
AFAICT R's Tcl interface has nothing to do with it.
"have to" is much too strong, there are certainly other ways.  E.g.,
help(".Tcl") specifically talks about addTclPath() and tclRequire(),
which are R interfaces to Tcl's "package require" and its "auto_path"
global variable:

  http://tcl.activestate.com/man/tcl8.4/TclCmd/package.htm
  http://tcl.activestate.com/man/tcl8.4/TclCmd/library.htm#M24

Remember that those are just helper functions to make combining R and
Tcl more convenient.  .Tcl() lets you execute any Tcl code you want,
so you could accomplish all the Tcl-side manipulation you need using
just it.

Some of the other R helper functions are doing more than just adding a
little syntactic sugar though.  For example, tclVar() and tclObj() are
very handy:

  input <- tclVar()
  # This nicely sets the Tcl list structure from the R vector: 
  tclObj(input) <- as.tclObj(my.vector ,drop=FALSE) 

Btw, it's easier to write Tcl code using variable names you know at
the time you write the code rather than the run-time determined
variable names that tclVar() gives you.  However, the implementation
of tclVar() is nicely simple, and if you look you'll see how to make
your own version that uses whatever Tcl variable name you want.