~l3on/ubuntu/precise/rkward/rebuild1

« back to all changes in this revision

Viewing changes to doc/rkward/writing_plugins_introduction.docbook

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Friedrichsmeier
  • Date: 2010-10-04 14:30:00 UTC
  • mfrom: (12.1.1 sid)
  • Revision ID: james.westby@ubuntu.com-20101004143000-qey73molmxxwy4w6
Tags: 0.5.4-1
* new upstream release
* bump standards version to 3.9.1 (no changes needed)
* no more need to remove svncopy.sh-script in rules

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
<?xml version="1.0" ?>
2
 
<!DOCTYPE book PUBLIC "-//KDE//DTD DocBook XML V4.1-Based Variant V1.0//EN" "dtd/kdex.dtd" [
 
2
<!DOCTYPE book PUBLIC "-//KDE//DTD DocBook XML V4.2-Based Variant V1.1//EN" "dtd/kdex.dtd" [
3
3
  <!ENTITY rkward '<application>RKWard</application>'>
4
4
  <!ENTITY kapp "&rkward;"><!-- replace rkward here -->
5
5
  <!ENTITY % addindex "IGNORE">
43
43
     and in the FDL itself on how to use it. -->
44
44
<legalnotice>&FDLNotice;</legalnotice>
45
45
 
46
 
<date>2010-03-04</date>
47
 
<releaseinfo>0.5.3.00</releaseinfo>
 
46
<date>2010-09-10</date>
 
47
<releaseinfo>0.5.4.00</releaseinfo>
48
48
 
49
49
<abstract>
50
50
<para>
65
65
<title>Introduction</title>
66
66
 
67
67
<para>
68
 
Documentation as of &kapp; release 0.5.3.
 
68
Documentation as of &kapp; release 0.5.4.
69
69
</para>
70
70
<para>
71
71
This document describes how to write your own plugins. Note, that at the time of this writing, some of the concepts are not yet set it stone. Therefore, this document should be regarded as an introduction to the current approach, and as a basis for discussion. All sorts of comments are welcome.
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.
513
513
</para>
514
514
<para>
515
 
If the user explicitely asks for a variable to be saved, you will need to assign to that object with the "&lt;&lt;-" operator.
 
515
If the user explicitely asks for a variable to be saved, you will need to assign to that object using ".GlobalEnv$objectname &lt;- value".
516
516
</para>
517
517
<para>
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:
794
794
<para>
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.
796
796
</para>
 
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>
 
800
<programlisting>
 
801
        [...]
 
802
        &lt;code file="code.js"/&gt;
 
803
 
 
804
        &lt;logic&gt;
 
805
                &lt;script&gt;&lt;![CDATA[
 
806
                        // ECMAScript code in this block
 
807
                        // the top-level statement is only called once
 
808
                        gui.addChangeCommand ("mode.string", "modeChanged ()");
 
809
 
 
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);
 
817
                        }
 
818
                ]]&gt;&lt;/script&gt;
 
819
        &lt;/logic&gt;
 
820
 
 
821
        &lt;dialog label="T-Test"&gt;
 
822
        [...]
 
823
</programlisting>
 
824
<para>
 
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 &lt;connect&gt;-statements, before.</para>
 
826
<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.
 
828
</para>
 
829
<para>
 
830
Note that the scripted approach to GUI logic can be mixed with &lt;connect&gt; and &lt;convert&gt;-statements if you like. Also note that the &lt;script&gt;-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.
 
831
</para>
 
832
</section>
797
833
</chapter>
798
834
 
799
835
<chapter id="embedding">
1311
1347
        </section>
1312
1348
</section>
1313
1349
 
 
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 &lt;varslot&gt;s, or by using <link linkend="logic_scripted">GUI logic scripting</link>.</para>
 
1354
</section>
 
1355
 
1314
1356
</chapter>
1315
1357
 
1316
1358
<appendix id="reference">
1768
1810
        <term>&lt;required&gt;</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>
 
1813
        <varlistentry>
 
1814
        <term>&lt;checkable&gt;</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>
 
1816
        </varlistentry>
 
1817
        <varlistentry>
 
1818
        <term>&lt;checked&gt;</term>
 
1819
        <listitem>For checkable saveobject-elements, only: Whether the control is checked/enabled by default (optional, defaults to false)</listitem>
 
1820
        </varlistentry>
1771
1821
        </variablelist></listitem>
1772
1822
</varlistentry>
1773
1823
<varlistentry>
1983
2033
        <term>selected</term>
1984
2034
        <listitem>The objects currently selected. You probably do not want to use this. Used internally (RObject)</listitem>
1985
2035
        </varlistentry>
 
2036
        <varlistentry>
 
2037
        <term>root</term>
 
2038
        <listitem>The root/parent object of the objects offered for selection (RObject)</listitem>
 
2039
        </varlistentry>
1986
2040
        </variablelist></listitem>
1987
2041
</varlistentry>
1988
2042
<varlistentry>
2077
2131
        <variablelist>
2078
2132
        <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>
 
2135
        </varlistentry>
 
2136
        <varlistentry>
 
2137
        <term>parent</term>
 
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>
 
2139
        </varlistentry>
 
2140
        <varlistentry>
 
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>
 
2143
        </varlistentry>
 
2144
        <varlistentry>
 
2145
        <term>active</term>
 
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>
2361
2427
</section>
2362
2428
 
 
2429
<section id="guilogic_functions"><title>Functions available for GUI logic scripting</title>
 
2430
<variablelist>
 
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":
 
2433
        <variablelist>
 
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>
 
2440
</varlistentry>
 
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>
 
2443
        <variablelist>
 
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>
 
2461
</varlistentry>
 
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>
 
2464
</varlistentry>
 
2465
<varlistentry><term>include()-function</term>
 
2466
<listitem><command>include(filename)</command>can be used to include a separate JS file.</listitem>
 
2467
</varlistentry>
 
2468
</variablelist>
 
2469
</section>
 
2470
 
2363
2471
</appendix>
2364
2472
 
2365
2473
<appendix id="troubleshooting">