Rev 78076 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/tcltk/man/TclInterface.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2014 R Core Team% Distributed under GPL 2 or later\name{TclInterface}\title{Low-level Tcl/Tk Interface}%\alias{TclInterface}\alias{.Tcl}\alias{.Tcl.objv}\alias{.Tcl.callback}\alias{.Tcl.args}\alias{.Tcl.args.objv}\alias{tclvar}\alias{tclVar}\alias{as.character.tclVar}\alias{tclArray}\alias{[[.tclArray}\alias{[[<-.tclArray}\alias{$.tclArray}\alias{$<-.tclArray}\alias{names.tclArray}\alias{length.tclArray}\alias{names<-.tclArray}\alias{length<-.tclArray}\alias{tclvalue}\alias{tclvalue<-}\alias{tclvalue.default}\alias{tclvalue<-.default}\alias{tclvalue.tclVar}\alias{tclvalue<-.tclVar}\alias{tclvalue.tclObj}\alias{as.character.tclObj}\alias{as.integer.tclObj}\alias{as.double.tclObj}\alias{as.logical.tclObj}\alias{as.raw.tclObj}\alias{as.tclObj}\alias{is.tclObj}\alias{tclObj}\alias{print.tclObj}\alias{tclObj<-}\alias{tclObj.tclVar}\alias{tclObj<-.tclVar}\alias{.Tk.ID}\alias{.Tk.newwin}\alias{.Tk.subwin}\alias{is.tkwin}\alias{tkdestroy}\alias{.TkRoot}\alias{addTclPath}\alias{tclRequire}\alias{tclVersion}\description{These functions and variables provide the basic glue between \R and theTcl interpreter and Tk GUI toolkit. Tkwindows may be represented via \R objects. Tcl variables can be accessedvia objects of class \code{tclVar} and the C level interface to Tclobjects is accessed via objects of class \code{tclObj}.}\usage{.Tcl(...).Tcl.objv(objv).Tcl.args(...).Tcl.args.objv(...).Tcl.callback(...).Tk.ID(win).Tk.newwin(ID).Tk.subwin(parent).TkRoottkdestroy(win)is.tkwin(x)tclvalue(x)tclvalue(x) <- valuetclVar(init = "")\method{as.character}{tclVar}(x, ...)\method{tclvalue}{tclVar}(x)\method{tclvalue}{tclVar}(x) <- valuetclArray()\method{[[}{tclArray}(x, ...)\method{[[}{tclArray}(x, ...) <- value\method{$}{tclArray}(x, i)\method{$}{tclArray}(x, i) <- value\method{names}{tclArray}(x)\method{length}{tclArray}(x)tclObj(x)tclObj(x) <- value\method{tclObj}{tclVar}(x)\method{tclObj}{tclVar}(x) <- valueas.tclObj(x, drop = FALSE)is.tclObj(x)\method{as.character}{tclObj}(x, ...)\method{as.integer}{tclObj}(x, ...)\method{as.double}{tclObj}(x, ...)\method{as.logical}{tclObj}(x, ...)\method{as.raw}{tclObj}(x, ...)\method{tclvalue}{tclObj}(x)\method{tclvalue}{default}(x)\method{tclvalue}{default}(x) <- valueaddTclPath(path = ".")tclRequire(package, warn = TRUE)tclVersion()}\arguments{\item{objv}{a named vector of Tcl objects}\item{win}{a window structure}\item{x}{an object}\item{i}{character or (unquoted) name}\item{drop}{logical. Indicates whether a single-element vector shouldbe made into a simple Tcl object or a list of length one}\item{value}{For \code{tclvalue} assignments, a character string. For\code{tclObj} assignments, an object of class \code{tclObj}}\item{ID}{a window ID}\item{parent}{a window which becomes the parent of the resulting window}\item{path}{path to a directory containing Tcl packages}\item{package}{a Tcl package name}\item{warn}{logical. Warn if not found?}\item{...}{Additional arguments. See below.}\item{init}{initialization value}}\details{Many of these functions are not intended for general use but are usedinternally by the commands that create and manipulate Tk widgets andTcl objects. At the lowest level \code{.Tcl} sends a command as a textstring to the Tcl interpreter and returns the result as an object ofclass \code{tclObj} (see below). A newer variant \code{.Tcl.objv}accepts arguments in the form of a named list of \code{tclObj}objects.\code{.Tcl.args} converts an R argument list of \code{tag = value} pairsto the Tcl \code{-option value} style, thusenabling a simple translation between the two languages. To send avalue with no preceding option flag to Tcl, just use an untaggedargument. In the rare case one needs an option with no subsequentvalue \code{tag = NULL} can be used. Most values are just converted tocharacter mode and inserted in the command string, but window objectsare passed using their ID string, and callbacks are passed via theresult of \code{.Tcl.callback}. Tags are converted to option flagssimply by prepending a \code{-}\code{.Tcl.args.objv} serves a similar purpose as \code{.Tcl.args} butproduces a list of \code{tclObj} objects suitable for passing to\code{.Tcl.objv}. The names of the list are converted to Tcl optionstyle internally by \code{.Tcl.objv}.Callbacks can be either \emph{atomic callbacks} handled by\code{.Tcl.callback} or expressions. An expression is treated as alist of atomic callbacks, with the following exceptions: if anelement is a name, it is first evaluated in the callers frame, andlikewise if it is an explicit function definition; the \code{break}expression is translated directly to the Tcl counterpart.\code{.Tcl.callback} converts \R functions and unevaluated calls toTcl command strings. The argument must be either a function closureor an object of mode \code{"call"} followed by an environment. Thereturn value in the first case is of the form \code{R_call0x408b94d4} in which the hexadecimal number is the memory address ofthe function. In the second case it will be of the form\code{R_call_lang 0x8a95904 0x819bfd0}. For expressions, a sequenceof similar items is generated, separated bysemicolons. \code{.Tcl.args} takes special precautions to ensurethat functions or calls will continue to exist at the specifiedaddress by assigning thecallback into the relevant window environment (see below).Tk windows are represented as objects of class \code{tkwin} which arelists containing a \code{ID} field and an \code{env} field which isan \R environments, enclosed in the global environment. The value ofthe \code{ID} field is identical to the Tk window name. The \code{env}environment contains a \code{parent} variable and a \code{num.subwin}variable. If the window obtains sub-windows and callbacks, they areadded as variables to the environment. \code{.TkRoot} is the topwindow with ID "."; this window is not displayed in order to avoidill effects of closing it via window manager controls. The\code{parent} variable is undefined for \code{.TkRoot}.\code{.Tk.ID} extracts the \code{ID} of a window,\code{.Tk.newwin} creates a new window environment with a given ID and\code{.Tk.subwin} creates a new window which is a sub-window of a givenparent window.\code{tkdestroy} destroys a window and also removes the reference to awindow from its parent.\code{is.tkwin} can be used to test whether a given object is a windowenvironment.\code{tclVar} creates a new Tcl variable and initializes it to\code{init}. An R object of class \code{tclVar} is created torepresent it. Using \code{as.character} on the object returns the Tclvariable name. Accessing the Tcl variable from R is done using the\code{tclvalue} function, which can also occur on the left-hand side ofassignments. If \code{tclvalue} is passed an argument which is not a\code{tclVar} object, then it will assume that it is a character stringexplicitly naming global Tcl variable. Tcl variables created by\code{tclVar} are uniquely named and automatically unset by thegarbage collector when the representing object is no longer in use.\code{tclArray} creates a new Tcl array and initializes it to the emptyarray. An R object of class \code{tclArray} and inheriting from class\code{tclVar} is created to represent it. You can access elements ofthe Tcl array using indexing with \code{[[} or \code{$}, which alsoallow replacement forms. Notice that Tcl arrays are associative bynature and hence unordered; indexing with a numeric index \code{i}refers to the element with the \emph{name}\code{as.character(i)}. Multiple indices are pasted together separatedby commas to form a single name. You can query thelength and the set of names in an array using methods for\code{length} and \code{names}, respectively; these cannotmeaningfully be set so assignment forms exist only to print an errormessage.It is possible to access Tcl's \sQuote{dual-ported} objects directly,thus avoiding parsing and deparsing of their string representation.This works by using objects of class \code{tclObj}. The stringrepresentation of such objects can be extracted (but not set) using\code{tclvalue} and conversion to vectors of mode \code{"character"},\code{"double"}, \code{"integer"}, \code{"logical"}, and \code{"raw"}is performed using the standard coercion functions \code{as.character},etc. Conversely, suchvectors can be converted using \code{as.tclObj}. There is anambiguity as to what should happen for length one vectors, controlledby the \code{drop} argument; there are cases where the distinctionmatters to Tcl, although mostly it treats them equivalently. Noticethat \code{tclvalue} and \code{as.character} differ on an object whosestring representation has embedded spaces, the former is sometimes tobe preferred, in particular when applied to the result of\code{tclread}, \code{tkgetOpenFile}, and similar functions. The\code{as.raw} method returns a raw vector or a list of raw vectors andcan be used to return binary data from Tcl.The object behind a \code{tclVar} object is extracted using\code{tclObj(x)} which also allows an assignment form, in which theright hand side of the assignment is automatically converted using\code{as.tclObj}. There is a print method for \code{tclObj} objects;it prints \code{<Tcl>} followed by the string representation of theobject. Notice that \code{as.character} on a \code{tclVar} object isthe \emph{name} of the corresponding Tcl variable and not the value.Tcl packages can be loaded with \code{tclRequire}; it may be necessaryto add the directory where they are found to the Tcl search path with\code{addTclPath}. The return value is a class \code{"tclObj"} objectif it succeeds, or \code{FALSE} if it fails (when a warning isissued). To see the current search path as an \R character vector, use\preformatted{strsplit(tclvalue('auto_path'), " ")[[1]]}.The Tcl version (including patchlevel) is returned as a characterstring (such as \code{"8.6.3"}).}\note{Strings containing unbalanced braces are currently not handledwell in many circumstances.}\seealso{\code{\link{TkWidgets}},\code{\link{TkCommands}},\code{\link{TkWidgetcmds}}.\code{\link{capabilities}("tcltk")} to see if Tcl/Tk support wascompiled into this build of \R.}\examples{tclVersion()\dontrun{## These cannot be run by example() but should be OK when pasted## into an interactive R session with the tcltk package loaded.Tcl("format \"\%s\\n\" \"Hello, World!\"")f <- function()cat("HI!\n").Tcl.callback(f).Tcl.args(text = "Push!", command = f) # NB: Different addressxyzzy <- tclVar(7913)tclvalue(xyzzy)tclvalue(xyzzy) <- "foo"as.character(xyzzy)tcl("set", as.character(xyzzy))top <- tktoplevel() # a Tk widget, see Tk-widgetsls(envir = top$env, all.names = TRUE)ls(envir = .TkRoot$env, all.names = TRUE) # .Tcl.args put a callback ref in here}}\keyword{ misc }