[Rd] Adding Tcl source to an R package

Andrew Piskorski atp at piskorski.com
Wed Aug 11 15:23:19 CEST 2010


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

> >> My Question: Where can I put my procedures in a *.tcl file within my
> >> package structure and how do I make my R package to load the Tcl
> >> procedures? And will these functions be within a Tcl namespace?

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.

> I have to load the tcl file within my R Code at some point with
> 
> tcl("source", tclfile)

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

-- 
Andrew Piskorski <atp at piskorski.com>
http://www.piskorski.com/



More information about the R-devel mailing list