Rcmdr Utility Functions

These functions support writing additions to the Rcmdr package, preferably by writing an Rcmdr plug-in package. Although it is not recommended, additional R code can also be placed in files with file type .R in the etc subdirectory of the Rcmdr package. In this case, you can add menus, submenus, and menu items by editing the file Rcmdr-menus.txt in the same directory.

activateMenus() activeDataSet(dsname, flushModel=TRUE, flushDialogMemory=TRUE) ActiveDataSet(name) activeDataSetP() activeModel(model) ActiveModel(name) activeModelP() aicP() beginRmdBlock() beginRnwBlock() checkActiveDataSet() checkActiveModel() checkBoxes(window=top, frame=stop("frame not supplied"), boxes=stop("boxes not supplied"), initialValues=NULL, labels=stop("labels not supplied"), title=NULL, ttk=FALSE) # macro checkClass(object, class, message=NULL) # macro checkFactors(n=1) checkMethod(generic, object, message=NULL, default=FALSE, strict=FALSE, reportError=TRUE) # macro checkNumeric(n=1) checkReplace(name, type=gettextRcmdr("Variable")) checkTwoLevelFactors(n=1) checkVariables(n=1) closeCommander(ask=TRUE, ask.save=ask) closeDialog(window, release=TRUE) # macro CommanderWindow() data(..., list = character(), package = NULL, lib.loc = NULL, verbose = getOption("verbose"), envir = .GlobalEnv) dataSetsP(n=1) defmacro(..., expr) dialogSuffix(window=top, onOK=onOK, onCancel=onCancel, rows, columns, focus=top, bindReturn=TRUE, preventGrabFocus=FALSE, preventDoubleClick=FALSE, preventCrisp, use.tabs=FALSE, notebook=notebook, tabs=c("dataTab", "optionsTab"), tab.names=c("Data", "Options"), grid.buttons=FALSE, resizable=FALSE, force.wait=FALSE) # macro doItAndPrint(command, log=TRUE, rmd=log) endRmdBlock() endRnwBlock() enterMarkdown(command) enterKnitr(command) errorCondition(window=top, recall=NULL, message, model=FALSE) # macro exists.method(generic, object, default=TRUE, strict=FALSE) Factors(names) factorsP(n=1) formulaFields(model, hasLhs=TRUE, glm=FALSE) flushDialogMemory(what) gassign(x, value) getDialog(dialog, defaults=NULL) "getFrame"(object) "getFrame"(object) "getSelection"(object) "getSelection"(object) getRcmdr(x, mode="any", fail=TRUE) gettextRcmdr(...) glmP() GrabFocus(value) groupsBox(recall=NULL, label=gettextRcmdr("Plot by:"), initialLabel=gettextRcmdr("Plot by groups"), errorText=gettextRcmdr("There are no factors in the active data set."), variables=Factors(), plotLinesByGroup=FALSE, positionLegend=FALSE, plotLinesByGroupsText=gettextRcmdr("Plot lines by group"), initialGroup=NULL, initialLinesByGroup=1, window=top) # macro groupsLabel(frame=top, groupsBox=groupsBox, columnspan=1, initialText=NULL) # macro hclustSolutionsP() initializeDialog(window=top, title="", offset=10, preventCrisp, use.tabs=FALSE, notebook=notebook, tabs=c("dataTab", "optionsTab"), suppress.window.resize.buttons=TRUE) # macro is.valid.name(x) is.valid.number(string) is.SciViews() justDoIt(command) knitrP() Library(package, pos=length(search()), rmd=TRUE) listAllModels(envir=.GlobalEnv, ...) listAOVModels(envir=.GlobalEnv, ...) listDataSets(envir=.GlobalEnv, ...) listFactors(dataSet=ActiveDataSet()) listGeneralizedLinearModels(envir=.GlobalEnv, ...) listLinearModels(envir=.GlobalEnv, ...) listMultinomialLogitModels(envir=.GlobalEnv, ...) listNumeric(dataSet=ActiveDataSet()) listPlugins(loaded=FALSE) listProportionalOddsModels(envir=.GlobalEnv, ...) listTwoLevelFactors(dataSet=ActiveDataSet()) listVariables(dataSet=ActiveDataSet()) lmP() logger(command, rmd=TRUE) LogWindow() MacOSXP(release) manualTranslationP() MarkdownP() mavericksP() Message(message, type=c("note", "error", "warning")) MessagesWindow() modelFormula(frame=top, hasLhs=TRUE, rhsExtras=NULL, formulaLabel=gettextRcmdr("Model Formula")) # macro modelsP(n=1) multinomP() Numeric(names) numericP(n=1) OKCancelHelp(window=top, helpSubject=NULL, model=FALSE, reset=NULL, apply=NULL, helpPackage=NULL) # macro OutputWindow() packageAvailable(name) polrP() popCommand(keep=FALSE) popOutput(keep=FALSE) putDialog(dialog, values=NULL, resettable=TRUE) putRcmdr(x, value) radioButtons(window=top, name=stop("name not supplied"), buttons=stop("buttons not supplied"), values=NULL, initialValue=..values[1], labels=stop("labels not supplied"), title="", title.color=getRcmdr("title.color"), right.buttons=FALSE, command=function(){}) # macro RappP() RcmdrEditor(buffer, title="R Commander Editor", ok, help=NULL, file.menu=NULL, edit.menu=NULL, context.menu=NULL, toolbar.buttons=NULL) RcmdrTclSet(name, value) RcmdrTkmessageBox(message, icon=c("info", "question", "warning", "error"), type=c("okcancel", "yesno", "ok"), default, title="") removeLastRmdBlock() removeLastRnwBlock() removeNullRmdBlocks() removeNullRnwBlocks() removeStrayRmdBlocks() removeStrayRnwBlocks() RExcelSupported() rglLoaded() RmdWindow() RnwWindow() setBusyCursor() setIdleCursor() sortVarNames(x) subOKCancelHelp(window=subdialog, helpSubject=NULL) # macro subsetBox(window = top, subset.expression = NULL, model = FALSE) # macro suppressMarkdown(command) tclvalue(x) titleLabel(...) tkfocus(...) tkspinbox(parent, ...) trim.blanks(text) TwoLevelFactors(names) twoLevelFactorsP(n=1) UpdateModelNumber(increment=1) variableComboBox(parentWindow, variableList=Variables(), export="FALSE", state="readonly", initialSelection=gettextRcmdr(""), title="") variableListBox(parentWindow, variableList=Variables(), bg="white", selectmode="single", export="FALSE", initialSelection=NULL, listHeight=getRcmdr("variable.list.height"), title) Variables(names) varPosn(variables, type=c("all", "factor", "numeric", "nonfactor", "twoLevelFactor"), vars=NULL) WindowsP() X11P()
# the following functions are exported for technical reasons, # but are not meant to be called directly
ask for confirmation.
ask whether to save contents of script and output windows.
if non-null (the default is NULL), an Apply button is included in the dialog's button bar. This argument should be set to the quoted name of the function that initiates the dialog; when the button is pressed, the onOK function for the dialog is executed, and then the function named in apply is (re)called.
background color.
if TRUE, the Return key is bound to the onOK function in the dialog.
vector of quoted names for check boxes, used to generate each box and its associated variable.
a text string, typically representing the contents of a text widget, such as an R Markdown or knitr document.
vector of quoted names for buttons in a set of related radio buttons.
quoted name of class.
number of dialog-box columns to be spanned by frame.
a character string that evaluates to an R command or (in the case of radioButton) a function to be called when a button is pressed.
NULL or a list containing one or more two-element lists: the first element, label, supplies the text label for a menu item in the RcmdrEditor right-click context menu; the second element, command, is a call-back function to be evaluated when the menu item is selected. If NULL (the default), no item will be added to the file menu.
dataSet, dsname
the quoted name of a data frame in memory.
default button: if not specified, "ok" for "okcancel", "yes" for "yesno", and "ok" for "ok"; or look for a default method; for putDialog, a list of defaults for the dialog box if there are no stored previous values.
a list of named default values for options in a dialog if no previous selections are stored.
the quoted name of a dialog box under which previous selections are stored.
NULL or a list containing one or more two-element lists: the first element, label, supplies the text label for a menu item in the RcmdrEditor Edit menu; the second element, command, is a call-back function to be evaluated when the menu item is selected. If NULL (the default), no item will be added to the file menu.
the environment to be searched; should generally be left at the default.
error message to print if a suitable factor isn't available.
export selection?
expression constituting the body of the macro; typically a compound expression.
if TRUE, the default, getRcmdr will generate an error if the object sought doesn't exist; if FALSE and the object doesn't exist, NULL is returned.
NULL or a list containing one or more two-element lists: the first element, label, supplies the text label for a menu item in the RcmdrEditor File menu; the second element, command, is a call-back function to be evaluated when the menu item is selected. If NULL (the default), no item will be added to the file menu.
remove saved values of dialog options so that getDialog returns NULL for all dialogs.
set (or reset) the active model to NULL? Should normally be TRUE when the active data set is changed; an exception is when variables are simply added to, deleted from, or modified in the data set set.
Tk window to get the focus.
call tkwait.window so that processing is suspended until the dialog is closed; overrides the Rcmdr tkwait.dialog option (see Commander) if the latter is set to FALSE (its default). The force.wait argument should be set to TRUE for subdialogs.
text label printed above the formula widget.
frame or quoted name for frame depending upon the function.
quoted name of generic function.
TRUE if the model is a glm object, FALSE otherwise.
insert call to tkgrid for the buttons frame (default FALSE); use TRUE for tabbed dialogs and optionally for other dialogs.
listbox object for selecting groups variable.
initial text to display in the groups label; if NULL, "" will be displayed.
does the model formula have a left-hand side?
a two element list: the first element, label, supplies the text label for a menu item in the RcmdrEditor Help menu; the second element, command, is a call-back function to be evaluated when the menu item is selected. If NULL (the default), no item will be added to the editor Help menu.
the quoted name of a help subject, to be called as help(helpSubject) when the dialog Help button is pressed.
the quoted name of the package in which to look for help; the default, NULL, produces a search in all loaded packages --- see help.
Message-box icon.
increment to model number; -1 to set back after error.
quoted name of variable to define groups, set as initial selection in Groups variable list; NULL (the default) for no initial selection.
if 1, the lines-by-groups check box is initially checked; 0 to uncheck.
label for groups button before a selection is made.
index of item initially selected, 0-base indexing.
for a set of related radio buttons.
for a set of related check boxes.
if TRUE, keep (rather than pop) last output or command in the stack; the default is FALSE.
label prefix for groups button after a selection is made.
a vector of character strings to label a set of radio buttons or check boxes.
see documentation for data.
see documentation for data.
Maximum number of elements displayed simultaneously in list box.
if TRUE, plug-in packages that are loaded are included in the vector of names returned.
echo command to the script window, as well as executing it and printing its output.
error (or other) message.
mode of object to retrieve.
the name of a model, as a character string, or a model object, or TRUE or FALSE, depending upon the function.
number of items to check for.
quoted name.
optional names to be stored.
notebook windows for a tabbed dialog (default notebook).
an object (depends on context).
in pixels, from top-left of Commander window.
a function called when the OK button is pressed in a RcmdrEditor window that saves the editor buffer to the appropriate Tcl variable.
function to execute when the OK button is pressed.
function to execute when the Cancel button or Esc key is pressed.
quoted name of package to load; also see documentation for data.
parent Tk window.
include a check box for plotting lines by group?
the label for the plot-lines-by-group check box.
position on search path at which to load package; default is just before the end of the path.
include a check box for a legend?
prevent the dialog box from grabbing the focus.
prevent double-clicking from pressing the OK button, even when the double.click option is set; necessary for statistical modelling dialogs, which use double-clicking to build the model formula.
this argument is ignored, and is present only for backwards compatibility.
function to call after error --- usually the function that initiates the dialog.
release the focus if the grab.focus option has been set. In MacOSXP, the minimum release (version) required.
if TRUE, report an error message.
quoted name of dialog function, to be invoked with all defaults by Reset button.
should dialog state be reset when the data set changes? The default is codeTRUE.
should the dialog be resizable by the user? The default is FALSE.
show controls for splines and polynomials for a model formula; for backwards compatibility, defaults to TRUE for a two-sided formula and FALSE for a one-sided formula.
radio button placed to right of button-labels; defaults to FALSE.
enter the command in the R Markdown tab.
rows, columns
numbers of rows and columns of widgets in the dialog box; this is actually no longer used, but is still present for backwards compatibility.
"single" or "multiple".
state of the combobox widget; default "readonly" means user can't type in the box; set to "normal" to permit typing.
if TRUE, only use first element of class vector.
a character string, or vector of strings, to be tested whether it can be coerced to a number or numbers; returns either TRUE or FALSE.
a quoted expression to subset the data set.
if TRUE, the default, the window maximize/minimize buttons will not be displayed.
text to print as tab labels (default c("Data", "Options")).
quoted names of tabs for a tabbed dialog (default c("dataTab", "optionsTab")).
a text string.
Window or dialog-box-element title.
color for title above radio buttons; defaults to "blue".
NULL or a list containing one or more three-element lists: the first element, label, supplies the text label for a button in the RcmdrEditor toolbar; the second element, command, is a call-back function to be evaluated when the button is pressed; the third element is the name of a tk image to display as an icon in the button. If NULL (the default), no buttons will be added to the toolbar.
use ttk themed widget for check boxes.
quoted type of object to check; used to generate check-replace dialog box; or type of message to print in Message window. For varPosn, type of variable list.
(default FALSE) construct a tabbed dialog.
an object to be stored or assigned.
vector of quoted values associated with radio buttons or check boxes; for putDialog, a list of current selections to be stored in support of dialog memory.
a vector of variable names.
a vector of one or more variable names.
a vector of one or more variable names; if given type argument is ignored.
see documentation for data.
optional character vector of one or more dialog names for which the memory is to be flushed; if not specified, all dialog memory will be flushed.
window, parentWindow
a Tk window.
an R object name, as a character string, or a tcl variable or object, or a vector of variable names to be sorted.
For gettextRcmdr, text string or vector of text strings to translate; for titleLabel, arguments to be passed to labelRcmdr and from there to ttklabel; for defmacro, arguments for the macro; otherwise disregard.

There are several groups of functions exported by the Rcmdr package and documented briefly here. To see how these functions work, it is simplest to examine the dialog-generating functions in the Rcmdr package. Also see the RcmdrPlugin.survival package for examples.

Executing and logging commands: The functions doItAndPrint, justDoIt, and logger control the execution, logging, and printing of commands generated by menus and dialogs. logger(command) adds command to the log/script window and to the output window. justDoIt(command) causes command to be executed. doItAndPrint(command) does both of these operations, and also prints the output produced by the command. The R Commander maintains a list of output objects, by default including the last 10 outputs. popOutput() ``pops'' (i.e., returns and removes) the first entry of the output stack. Note that, as a stack, the queue is LIFO (``last in, first out''). Use popOutput(keep=TRUE) to access the last output but keep it in the stack. There is also a stack of commands, which is accessed similarly by popCommand(). Ocassionally, it's necessary to assign an object directly in the global environment, and this can be done with the gassign function. Normally commands also generate an R Markdown block. suppressMarkdown takes a command in character-string form and adds an attribute to it that will cause the command not to be entered in the R Markdown tab. This is useful when a command, such as identify, requires direct user interaction and won't generate useful Markdown. enterMarkdown can be used to enter command blocks directly in the R Markdown tab; this should rarely be required. The functions beginRmdBlock, endRmdBlock, removeNullRmdBlocks, removeLastRmdBlock, and removeStrayRmdBlocks should normally not be called directly. The functions enterKnitr, beginRnwBlock, endRnwBlock, removeNullRnwBlocks, removeLastRnwBlock, and removeStrayRnwBlocks perform similar functions for Knitr documents. Checking for errors: The function is.valid.name checks whether a character string specifies a valid name for an R object. The function is.valid.number checks whether a character string (or vector) can be coerced to a number (or numbers). The functions checkActiveDataSet, checkActiveModel, checkFactors, checkNumeric, checkTwoLevelFactors, and checkVariables check for the existence of objects and write an error message to the log if they are absent (or insufficiently numerous, in the case of different kinds of variables). The function checkReplace opens a dialog to query whether an existing object should be replaced. The function checkMethod, checks whether a method exists for a particular generic that is appropriate for a particular object. The function checkClass checks whether an object is of a specific class. Both of these functions write error messages to the log if the condition fails. The function errorCondition reports an error to the user and (optionally) re-starts a dialog. Information: Several functions return vectors of object names: listAllModels, listAOVModels, listDataSets, listGeneralizedLinearModels, listFactors, listLinearModels, listMultinomialLogitModels, listNumeric, listProportionalOddsModels, listTwoLevelFactors, listVariables. The functions activeDataSet and activeModel respectively report or set the active data set and model. The function packageAvailable reports whether the named package is available to be loaded (or has possibly already been loaded). The function exists.method checks whether a method exists for a particular generic that is appropriate for a particular object, and returns TRUE or FALSE. The function is.SciViews always returns FALSE since the SciViews GUI is no longer supported. Building dialog boxes: Several functions simplify the process of constructing Tk dialogs: initializing a dialog box, initializeDialog, and completing the definition of a dialog box, dialogSuffix; a set of check boxes, checkBoxes; a set of radio buttons, radioButtons; a list box with associated scrollbars and state variable, variableListBox (and associated methods for the generic functions getFrame and getSelection); a drop-down "combo" box, variablecomboBox (with getFrame and getSelection methods); a button and subdialog for selecting a "grouping" variable, groupsBox; displaying the currently defined groups in a dialog, groupsLabel; a dialog-box structure for entering a model formula, modelFormula; a text box for entering a subsetting expression, subsetBox; OK, Cancel, and Help buttons for dialogs, OKCancelHelp, and subdialogs, subOKCancelHelp. The functions putDialog, getDialog, and varPosn support dialog-box memory---i.e., retaining selections across invocations of a dialog. The tkspinbox function is omitted from the tcltk package and may be used to create a spinbox widget. The titleLabel function may be used to format a title label to use the standard title label font and color. ``Themed'' Tk widgets: Tk 8.5 introduced so-called ``themed'' widgets, which look better than the traditional Tk widgets. Several functions, contributed by Brian Ripley, are written to access the new widgets by switching automatically between the new and old widget sets depending upon the availability of the former: buttonRcmdr, to access either ttkbutton or tkbutton; labelRcmdr, to access either ttklabel or tklabel; ttkentry, to access either ttkentry or tkentry; ttkframe, to access either ttkframe or tkframe; ttkradiobutton, to access either ttkradiobutton or tkradiobutton; and ttkscrollbar, to access either ttkscrollbar or tkscrollbar. Note that the last four functions mask functions of the same names in the tcltk package. `Predicate' functions: A number of functions of the form nameP are `predicate' functions, which return TRUE or FALSE depending upon whether some condition obtains. For example, lmP() returns TRUE if there is an active model that is a linear model; and factorsP(2) returns TRUE if there are at least two factors in the active data set. WindowsP(), MacOSXP(), and X11P() return TRUE if the R Commander is running under Windows, Mac OS X, or X-Windows, consecutively. Translating text: The gettextRcmdr function simply passes its argument(s) to gettext, adding the argument domain="R-Rcmdr" It is not meant to be used in plug-in packages. Miscellaneous: The function trim.blanks removes spaces from the beginning and end of a character string. The function installPlugin installs an Rcmdr plug-in from a ZIP file or directory; this function may be used to create self-installing plug-ins in the form of packages. The function nobs returns the number of observations on which a statistical model is based. The function formulaFields returns information about the left-hand side, right-hand side, data, subset, and (for GLMs) family and link, of a model object. The function sortVarNames sorts variable names, including those containing numerals, into a more "natural" order than does the standard sort function. The function Library may be used to load packages; it checks whether a package is already loaded, and if not by default puts it in position 4 on the search path. The function RExcelSupported is used for the RExcel interface. Some of these functions, marked # macro under Usage, are "macro-like" in their behaviour, in that they execute in the environment from which they are called. These were defined with an adaptation (used with permission) of Thomas Lumley's defmacro function, described in Lumley (2001), and are used in the R Commander to deal with scoping issues related to Tcl/Tk. The tkfocus function is exported for historical reasons. The data function, imported from the utils package, is exported for technical reasons.


T. Lumley (2001) Programmer's niche: Macros in R. R News, 1(3), 11--13.

  • Rcmdr.Utilities
  • Rcmdr Utilities
  • R Commander Utilities
  • activateMenus
  • activeDataSet
  • ActiveDataSet
  • activeDataSetP
  • activeModel
  • ActiveModel
  • activeModelP
  • aicP
  • beginRmdBlock
  • beginRnwBlock
  • checkActiveDataSet
  • checkActiveModel
  • checkBoxes
  • checkClass
  • checkFactors
  • checkMethod
  • checkNumeric
  • checkReplace
  • checkTwoLevelFactors
  • checkVariables
  • closeCommander
  • closeDialog
  • commanderPosition
  • CommanderWindow
  • dataSetsP
  • defmacro
  • dialogSuffix
  • doItAndPrint
  • endRmdBlock
  • endRnwBlock
  • enterMarkdown
  • enterKnitr
  • errorCondition
  • exists.method
  • Factors
  • factorsP
  • formulaFields
  • flushDialogMemory
  • gassign
  • getDialog
  • getFrame
  • getFrame.combobox
  • getFrame.listbox
  • getRcmdr
  • getSelection
  • getSelection.combobox
  • getSelection.listbox
  • gettextRcmdr
  • glmP
  • GrabFocus
  • groupsBox
  • groupsLabel
  • hclustSolutionsP
  • initializeDialog
  • is.SciViews
  • is.valid.name
  • is.valid.number
  • justDoIt
  • knitrP
  • Library
  • listAllModels
  • listAOVModels
  • listDataSets
  • listGeneralizedLinearModels
  • listFactors
  • listLinearModels
  • listMultinomialLogitModels
  • listNumeric
  • listPlugins
  • listProportionalOddsModels
  • listTwoLevelFactors
  • listVariables
  • lmP
  • logger
  • LogWindow
  • MacOSXP
  • manualTranslationP
  • MarkdownP
  • mavericksP
  • Message
  • MessagesWindow
  • modelFormula
  • modelsP
  • multinomP
  • Numeric
  • numericP
  • OKCancelHelp
  • OutputWindow
  • packageAvailable
  • polrP
  • popCommand
  • popOutput
  • putDialog
  • putRcmdr
  • radioButtons
  • RappP
  • RcmdrEditor
  • RcmdrTclSet
  • RcmdrTkmessageBox
  • removeLastRmdBlock
  • removeLastRnwBlock
  • removeNullRmdBlocks
  • removeNullRnwBlocks
  • removeStrayRmdBlocks
  • removeStrayRnwBlocks
  • RExcelSupported
  • rglLoaded
  • RmdWindow
  • RnwWindow
  • setBusyCursor
  • setIdleCursor
  • sortVarNames
  • subOKCancelHelp
  • subsetBox
  • suppressMarkdown
  • tclvalue
  • titleLabel
  • tkspinbox
  • trim.blanks
  • TwoLevelFactors
  • twoLevelFactorsP
  • UpdateModelNumber
  • variableComboBox
  • variableListBox
  • Variables
  • varPosn
  • WindowsP
  • X11P
  • buttonRcmdr
  • labelRcmdr
  • ttkentry
  • ttkframe
  • ttkradiobutton
  • ttkscrollbar
  • tkfocus
  • data
Documentation reproduced from package Rcmdr, version 2.3-0, License: GPL (>= 2)

Community examples

Looks like there are no examples yet.