512
512
More often than not you will have to create one or more temporary R objects in the code generated by your plugin. Normally, you do not want those to be placed in the user's workspace, potentially even overwriting user variables. Hence, all plugin generated code is run in a local() environment (see R help page on function local()). This means, all variables you create are temporary and will not be saved permanently.
515
If the user explicitely asks for a variable to be saved, you will need to assign to that object with the "<<-" operator.
515
If the user explicitely asks for a variable to be saved, you will need to assign to that object using ".GlobalEnv$objectname <- value".
518
518
One important pitfall is using eval() to evaluate substitutes. Here, you need to note that eval will by default use the current environment for evaluation, i.e. the local one. This will work well most of the times, but if you define any temporary variables in the local environment, and you are trying to fetch user variables of the same name using eval, you will get errors. For example:
795
795
Once again, for a complete list of properties, refer to the <link linkend="reference">reference</link>. One more property, however, is special in that all GUI elements have it: "enabled". This is slightly less drastic that "visible". It does not show/hide the GUI element, but only enables/disables it. Disabled elements are typically shown grayed out, and do not react to user input.
797
<section id="logic_scripted">
798
<title>Scripted GUI logic</title>
799
<para>While connecting properties as described above is often enough, sometimes it is more flexible or more convenient to use JS to script the GUI logic. In this way, the above example could be re-written as:</para>
802
<code file="code.js"/>
805
<script><![CDATA[
806
// ECMAScript code in this block
807
// the top-level statement is only called once
808
gui.addChangeCommand ("mode.string", "modeChanged ()");
810
// this function is called whenever the "mode" was changed
811
modeChanged = function () {
812
var varmode = (gui.getValue ("mode.string") == "variable");
813
gui.setValue ("y.required", varmode);
814
gui.setValue ("y.enabled", varmode);
815
gui.setValue ("constant.required", !varmode);
816
gui.setValue ("constant.enabled", !varmode);
818
]]></script>
821
<dialog label="T-Test">
825
The first line of code tells RKWard to call the function "modeChanged()" whenever the value of the "mode" radio-box changes. Inside this function, we define a helper-variable "varmode" which is true when the mode is "variable", false is it is "constant". Then we use "gui.setValue()" to set the "required"- and "enabled"-properties of "y" and "constant", in just the same way as we did using <connect>-statements, before.</para>
827
The scripted approach to GUI logic becomes particularily useful when you want to change the available option according to the type of object that the user has selected. See <link linkend="guilogic_functions">the reference</link> for available functions.
830
Note that the scripted approach to GUI logic can be mixed with <connect> and <convert>-statements if you like. Also note that the <script>-tag allows to specify a script file name in addition to or as an alternative to inlining the script code. Typically, inlining the script code as shown above is most convenient, however.
799
835
<chapter id="embedding">
1350
<section id="current_object">
1351
<title>Referencing the current object</title>
1352
<para>For many plugins it is desirable to work on the "current" object. For instance a "sort" plugin could pre-select the data.frame that is currently being edited for sorting. The name of the current object is available to plugins as a pre-defined property called "current_object". You can connect to this property in the usual way. If no object is current, the property equates to an empty string.</para>
1353
<para>Currently the "current_object" can only be of class data.frame, but please do not rely on this, since this will be extended to other types of data in the future. Therefore please check any requirements by using appropriate constraints on your <varslot>s, or by using <link linkend="logic_scripted">GUI logic scripting</link>.</para>
1316
1358
<appendix id="reference">
1768
1810
<term><required></term>
1769
1811
<listitem>Whether - for submitting the code - it is required that the field holds a permissible object name. See <link linkend="elementproperties">required-property</link> (optional, defaults to true)</listitem>
1770
1812
</varlistentry>
1814
<term><checkable></term>
1815
<listitem>In many use cases, saving to an R object is optional. In these cases, a checkbox can be integrated into the saveobject-element using this attribute. When set to true, the saveobject will be activated / deactivated by the checkbox. See the <link linkend="elementproperties">active-property</link> of saveobject (optional, defaults to false)</listitem>
1818
<term><checked></term>
1819
<listitem>For checkable saveobject-elements, only: Whether the control is checked/enabled by default (optional, defaults to false)</listitem>
1771
1821
</variablelist></listitem>
1772
1822
</varlistentry>
2079
2133
<term>selection</term>
2080
<listitem>Current text (selected object name) (string)</listitem>
2134
<listitem>Full name of the selected object (string; read-only - to set this programmatically, use "parent", and "objectname")</listitem>
2138
<listitem>The parent object of the selected object. This is always an existing R object of a type that can contain other objects (e.g. a list or data.frame). When set to an empty string or an invalid object, ".GlobalEnv" is assumed (RObject)</listitem>
2141
<term>objectname</term>
2142
<listitem>The base-name of the selected object, i.e. the string entered by the user (changed to a valid R name, if necessary) (string)</listitem>
2146
<listitem>For checkable saveobjects, only: Whether the control is checked/enabled. Always true for non-checkable saveobjects (bool)</listitem>
2081
2147
</varlistentry>
2082
2148
</variablelist></listitem>
2083
2149
</varlistentry>
2360
2426
</variablelist>
2429
<section id="guilogic_functions"><title>Functions available for GUI logic scripting</title>
2431
<varlistentry><term>Class "Component"</term>
2432
<listitem>Class which represents a single component or component-property. The most important instance of this class is the variable "gui" which is predefined as the root property of the current component. The following methods are available for instances of class "Component":
2434
<varlistentry><term>absoluteId(base_id)</term><listitem>Returns the absolute id of <emphasis>base_id</emphasis>, or - if base_id is omitted - the identifier of the component.</listitem></varlistentry>
2435
<varlistentry><term>getValue(id)</term><listitem>Returns the value of the given child property. Returns the value of this property, if id is omitted.</listitem></varlistentry>
2436
<varlistentry><term>setValue(id, value)</term><listitem>Set the value of the given child property to <emphasis>value</emphasis>.</listitem></varlistentry>
2437
<varlistentry><term>getChild(id)</term><listitem>Return an instance of the child-property with the given <emphasis>id</emphasis>.</listitem></varlistentry>
2438
<varlistentry><term>addChangeCommand(id, command)</term><listitem>Execute <emphasis>command</emphasis> whenever the child property given by <emphasis>id</emphasis> changes.</listitem></varlistentry>
2439
</variablelist></listitem>
2441
<varlistentry><term>Class "RObject"</term>
2442
<listitem>Class which represents a single R object. An instance of this class can be obtained by using <command>makeRObject(objectname)</command>. The following methods are available for instances of class "RObject": <warning>If any commands are still pending in the backend, the information provided by these methods can be out-of-date by the time that the plugin code is run. Do <emphasis>not</emphasis> rely on it for critical operations (risking loss of data).</warning>
2444
<varlistentry><term>getName()</term><listitem>Returns the absolute name of the object.</listitem></varlistentry>
2445
<varlistentry><term>exists()</term><listitem>Returns whether the object exists. You should generally check this before using any of the methods listed below.</listitem></varlistentry>
2446
<varlistentry><term>dimensions()</term><listitem>Returns an array of dimensions (similar to <command>dim()</command> in R).</listitem></varlistentry>
2447
<varlistentry><term>classes()</term><listitem>Returns an array of classes (similar to <command>class()</command> in R).</listitem></varlistentry>
2448
<varlistentry><term>isClass(class)</term><listitem>Returns true, if the object is of class <emphasis>class</emphasis>.</listitem></varlistentry>
2449
<varlistentry><term>isDataFrame()</term><listitem>Returns true, if the object is a data.frame.</listitem></varlistentry>
2450
<varlistentry><term>isMatrix()</term><listitem>Returns true, if the object is a matrix.</listitem></varlistentry>
2451
<varlistentry><term>isList()</term><listitem>Returns true, if the object is a list.</listitem></varlistentry>
2452
<varlistentry><term>isFunction()</term><listitem>Returns true, if the object is a function.</listitem></varlistentry>
2453
<varlistentry><term>isEnvironment()</term><listitem>Returns true, if the object is an environment.</listitem></varlistentry>
2454
<varlistentry><term>isDataNumeric()</term><listitem>Returns true, if the object is a vector of numeric data.</listitem></varlistentry>
2455
<varlistentry><term>isDataFactor()</term><listitem>Returns true, if the object is a vector of factor data.</listitem></varlistentry>
2456
<varlistentry><term>isDataCharacter()</term><listitem>Returns true, if the object is a vector of character data.</listitem></varlistentry>
2457
<varlistentry><term>isDataLogical()</term><listitem>Returns true, if the object is a vector of logical data.</listitem></varlistentry>
2458
<varlistentry><term>parent()</term><listitem>Returns an instance of "RObject" representing the parent of this object.</listitem></varlistentry>
2459
<varlistentry><term>child(childname)</term><listitem>Returns an instance of "RObject" representing the child <emphasis>childname</emphasis> of this object.</listitem></varlistentry>
2460
</variablelist></listitem>
2462
<varlistentry><term>Class "RObjectArray"</term>
2463
<listitem>An array of RObject instances. An instance of this class can be obtained by using <command>makeRObjectArray(objectnames)</command>. It is particularily useful when dealing with varslots which allow to select multiple objects.</listitem>
2465
<varlistentry><term>include()-function</term>
2466
<listitem><command>include(filename)</command>can be used to include a separate JS file.</listitem>
2365
2473
<appendix id="troubleshooting">