[rkward-cvs] SF.net SVN: rkward:[4310] trunk/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Wed Sep 26 10:18:10 UTC 2012


Revision: 4310
          http://rkward.svn.sourceforge.net/rkward/?rev=4310&view=rev
Author:   tfry
Date:     2012-09-26 10:18:10 +0000 (Wed, 26 Sep 2012)
Log Message:
-----------
Add support for custom figure margins to the plot_options plugin.

* This patch contains some adjustments to existing plotting plugins, as not all plugins were including the preprocess section of the plot_options plugin so far.
* .rk.start.preview.device() now makes sure to reinstate the initial state of par() before each iteration of the preview.
* Documentation was enhanced to provide a canonical example of a plotting plugin.
* TODO: Create tests for new options; adjust test standards for those plugins where the formatting was changes, slightly; adjust IRT plot plugins

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/doc/rkwardplugins/index.docbook
    trunk/rkward/rkward/plugins/distributions/plot_dist_common.js
    trunk/rkward/rkward/plugins/plots/box_plot.js
    trunk/rkward/rkward/plugins/plots/density_plot.js
    trunk/rkward/rkward/plugins/plots/dotchart.js
    trunk/rkward/rkward/plugins/plots/ecdf_plot.js
    trunk/rkward/rkward/plugins/plots/histogram.js
    trunk/rkward/rkward/plugins/plots/pareto.js
    trunk/rkward/rkward/plugins/plots/piechart.js
    trunk/rkward/rkward/plugins/plots/plot.js
    trunk/rkward/rkward/plugins/plots/plot_options.js
    trunk/rkward/rkward/plugins/plots/plot_options.rkh
    trunk/rkward/rkward/plugins/plots/plot_options.xml
    trunk/rkward/rkward/plugins/plots/scatterplot.js
    trunk/rkward/rkward/plugins/plots/sieve_plot.js
    trunk/rkward/rkward/plugins/plots/stripchart_plot.js
    trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal_graphics.R
    trunk/rkward/rkward/scriptbackends/common.js

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/ChangeLog	2012-09-26 10:18:10 UTC (rev 4310)
@@ -1,4 +1,5 @@
-- Add option for installing packages from source (implicitly enabled on Unixoid platforms)
+- Most plot plugins gain options to control margins and tick label orientation				TODO: add test(s), adjust existing test standards where needed, adjust IRT plugins
+- Added option for installing packages from source (implicitly enabled on Unixoid platforms)
 - Fixed: Wrong handling of carriage returns ('\r') in the console window				TODO: also fix for the command log and other places
 - Fixed: Spinboxes had wrong initial values
 - Omit comments on missing function calls in dialog code windows (e.g., if prepare() is unused, there's no "## Prepare" in the output either)

Modified: trunk/rkward/doc/rkwardplugins/index.docbook
===================================================================
--- trunk/rkward/doc/rkwardplugins/index.docbook	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/doc/rkwardplugins/index.docbook	2012-09-26 10:18:10 UTC (rev 4310)
@@ -53,8 +53,8 @@
      and in the FDL itself on how to use it. -->
 <legalnotice>&FDLNotice;</legalnotice>
 
-<date>2011-10-17</date>
-<releaseinfo>0.5.7.00</releaseinfo>
+<date>2012-09-26</date>
+<releaseinfo>0.5.8.00</releaseinfo>
 
 <abstract>
 <para>
@@ -76,7 +76,7 @@
 
 	<note>
 		<para>
-			Documentation as of &rkward; release 0.5.7.
+			Documentation as of &rkward; release 0.5.8.
 		</para>
 	</note>
 	<para>
@@ -618,7 +618,6 @@
 }
 		</programlisting>
 	</sect2>
-
 </sect1>
 
 <sect1 id="jstips">
@@ -1248,71 +1247,137 @@
 This chapter contains information on some topics that are useful only to certain classes of plugins.
 </para>
 
-<sect1 id="preview_plots">
-	<title>Preview functionality for graphing plugins</title>
+<sect1 id="specialized_plugins_plots">
+	<title>Plugins than produce a plot</title>
 	<para>
-		A very useful feature for all plugins generating a plot/graph is to provide an automatically updating preview. To do so, you will need two things: Adding a <command><preview></command> checkbox to your <link linkend="mainxml">GUI definition</link>, and adjusting the <link linkend="jstemplate">generated code</link> for the preview. We will deal with both in turn.
+		Creating a plot from a plugin is easy to do. However, there are a few sublte gotchas to avoid, and also some great generic functionality that you should be aware of. This section shows you the basic concepts, and concludes with a canonical example that you should follow whenever creating plot plugins.
 	</para>
-	<para>
-		Adding a <command><preview></command> checkbox is simple. Just place the following somewhere in your GUI. It will take care of all the behind-the-scenes magic of creating a preview device, updaing the preview whenever the setting have changed, etc. Example:
-	</para>
-	<programlisting>
-<document>
-	[...]
-	<dialog [...]>
+	<sect2 id="rk_graph_on">
+		<title>Drawing a plot to the output window</title>
+		<para>
+			In order to draw a plot to the output window, use <function>rk.graph.on()</function> directly before creating the plot, and
+			<function>rk.graph.off()</function>, directly afterwards. This is similar to e.g. callling <function>postscript()</function> and 
+			<function>dev.off()</function> in a regular R session.
+		</para>
+		<para>
+			Importantly, however, you must <emphasis>always</emphasis> call <function>rk.graph.off()</function> after calling <function>rk.graph.on()</function>. Otherwise the output file will be left in a broken state. To ensure <function>rk.graph.off()</function> really gets called, you should wrap <emphasis>all</emphasis> R commands between the two calls in
+			<function>try()</function> statement. Never heard of that? Don't worry, it's easy. All you need to do is follow the pattern shown in
+			<link linkend="plot_plugin_example">example</link>, below.
+		</para>
+	</sect2>
+	<sect2 id="preview_plots">
+		<title>Adding preview functionality</title>
+		<para>
+			A very useful feature for all plugins generating a plot/graph is to provide an automatically updating preview. To do so, you will need two things: Adding a <command><preview></command> checkbox to your <link linkend="mainxml">GUI definition</link>, and adjusting the <link linkend="jstemplate">generated code</link> for the preview.
+		</para>
+		<para>
+			Adding a <command><preview></command> checkbox is simple. Just place the following somewhere in your GUI. It will take care of all the behind-the-scenes magic of creating a preview device, updaing the preview whenever the setting have changed, etc. Example:
+		</para>
+		<programlisting>
+	<document>
 		[...]
-		<preview id="preview"/>
+		<dialog [...]>
+			[...]
+			<preview id="preview"/>
+			[...]
+		</dialog>
 		[...]
-	</dialog>
-	[...]
-</document>
-	</programlisting>
-	<para>
-		And that's it for the GUI definition.
-	</para>
-	<para>
-		Adjusting the JS template is a little more work. You will have to create a new function called <function>preview()</function> in addition to the <function>preprocess()</function>, <function>calculate()</function>, etc. functions. This function should generate the code needed to produce the plot, and only that. Esp. no printing of headers, <function>rk.graphics.on()</function>, or similar calls. Below is an example of the typical pattern that you will use:
-	</para>
-	<programlisting>
-function preprocess () {
-	// the "somepackage" is needed to create the plot
-	ecoh ("require (somepackage)\n");
-}
+	</document>
+		</programlisting>
+		<para>
+			And that's it for the GUI definition.
+		</para>
+		<para>
+			Adjusting the JS template is a little more work. You will have to create a new function called <function>preview()</function> in addition to the <function>preprocess()</function>, <function>calculate()</function>, etc. functions. This function should generate the code needed to produce the plot, and only that. Esp. no printing of headers, <function>rk.graphics.on()</function>, or similar calls. See the <link linkend="plot_plugin_example">example</link>, below for the typical pattern that you will use.
+		</para>
+	</sect2>
+	<sect2 id="plot_options">
+		<title>Generic plot options</title>
+		<para>
+			You will have noticed that most plotting plugins in RKWard provide a wide range of generic options e.g. for customizing axis titles or figure margins. Adding these options to your plugin is easy. They are provided by an <link linkend="embedding">embeddable</link> plugin called <command>rkward::plot_options</command>. Embed this in your plugin UI like this:
+		</para>
+		<programlisting>
+	<document>
+		[...]
+		<logic [...]>
+			<connect client="plotoptions.xvar" governor="x.available"/>
+			<set id="plotoptions.allow_type" to="true"/>
+			<set id="plotoptions.allow_ylim" to="true"/>
+			<set id="plotoptions.allow_xlim" to="false"/>
+			<set id="plotoptions.allow_log" to="false"/>
+			<set id="plotoptions.allow_grid" to="true"/>
+		</logic>
+		<dialog [...]>
+			[...]
+			<embed id="plotoptions" component="rkward::plot_options" as_button="true" label="Plot Options"/>
+			[...]
+		</dialog>
+		[...]
+	</document>
+		</programlisting>
+		<para>
+			This will add a button to your UI to bring up a window with plot options. The logic section is just an example. It allows you some control over the plot options plugin. Read more in the plot_options plugin's help page (linked from the help page of any plugin providing the generic options).
+		</para>
+		<para>
+			Next you need to make sure that the code corresponding to your plot options is added to the generated code for your plot. To do so,
+			fetch the properties <command>code.preprocess</command>, <command>code.printout</command>, and <command>code.calculate</command> from the embedded plot options plugin, and insert them into your code as shown in the <link linkend="plot_plugin_example">example</link>, below.
+		</para>
+	</sect2>
+	<sect2 id="plot_plugin_example">
+		<title>A canonical example</title>
+		<para>
+			Here's an example .JS file that you should use as a template, whenever you create a plotting plugin:
+		</para>
+		<programlisting>
+	function preprocess () {
+		// the "somepackage" is needed to create the plot
+		ecoh ("require (somepackage)\n");
+	}
 
-function printout () {
-	// all the real work is moved to a custom defined function doPrintout (), below
+	function printout () {
+		// all the real work is moved to a custom defined function doPrintout (), below
 
-	doPrintout (true);	// true in this case means: We want all the headers that should be printed in the output
-}
+		doPrintout (true);	// in this case, 'true' means: We want all the headers that should be printed in the output, not just the preview
+	}
 
-function preview () {
-	// we call all stages of the general code. Only the printout () function need to be slightly different
-	preprocess ();
-	// calculate (); in this example the plugin has no calculate () function.
-	doPrintout (false);
-}
+	function preview () {
+		// we call all stages of the general code. Only the printout () function needs to be slightly different for the plot preview
+		preprocess ();
+		// calculate (); // in this example, the plugin has no calculate () function.
+		doPrintout (false);	// in this case, 'false' means: Create the plot, but not any headers or other output.
+	}
 
-function doPrintout (full) {
-	// this function takes care of generating the code for the printout() section. If "full" is set to true,
-	// it generates the full code, including headers. If full is set to false, only the essentials will
-	// be generated.
+	function doPrintout (full) {
+		// this function takes care of generating the code for the printout() section. If "full" is set to true,
+		// it generates the full code, including headers. If full is set to false, only the essentials will
+		// be generated.
 
-	if (full) {
-		echo ('rk.header ("An example plot")\n\n');
-		echo ('rk.graph.on ()\n');
-	} 
+		if (full) {
+			echo ('rk.header ("An example plot")\n\n');
+			echo ('rk.graph.on ()\n');
+		}
+		// only the following section will be generated for full==false
 
-	// only the following section will be generated for full==false
-	echo ('try ({plot (5, 5)})\n');
+		// remember: everything between rk.graph.on() and rk.graph.off() should be wrapped inside a try() statement:
+		echo ('try ({\n');
+		// insert any option-setting code that should be run before the actual plotting commands.
+		// The code itself is provided by the embedded plot options plugin. printIndentedUnlessEmpty() takes care of pretty formatting.
+		printIndentedUnlessEmpty ('\t', getValue ("plotoptions.code.preprocess"), '', '\n');
 
-	if (full) {
-		echo ('rk.graph.off ()\n');
+		// create the actual plot. plotoptions.code.printout provides the part of the generic plot options
+		// that have to be added to the plotting call, itself.
+		echo ('plot (5, 5' + getValue ("plotoptions.code.printout") + ')\n');
+
+		// insert any option-setting code that should be run after the actual plot.
+		printIndentedUnlessEmpty ('\t', getValue ("plotoptions.code.calculate"), '\n');
+		echo ('})'\n);	// the closure of the try() statement
+
+		if (full) {
+			echo ('rk.graph.off ()\n');
+		}
 	}
-}
-	</programlisting>
-	<para>
-		That's it, your plugin should now be preview-enabled.
-	</para>
+		</programlisting>
+	</sect2>
 </sect1>
 
 <sect1 id="contextualized_plugins">

Modified: trunk/rkward/rkward/plugins/distributions/plot_dist_common.js
===================================================================
--- trunk/rkward/rkward/plugins/distributions/plot_dist_common.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/distributions/plot_dist_common.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -26,7 +26,6 @@
 }
 
 function doPrintout (full) {
-	var plot_adds = "";
 	var fun = getValue ("function");
 	var _log = (getValue ("log") == 1);
 
@@ -74,15 +73,13 @@
 	}
 
 	echo ('try ({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
+
 	echo ('	curve (');
 	doFunCall ();
 	echo (', from=' + options['min'] + ', to=' + options['max'] + ', n=' + options['n'] + getValue ("plotoptions.code.printout") + ')\n');
 
-	plot_adds = getValue ("plotoptions.code.calculate");
-	if (plot_adds.length > 0) {
-		echo ('\n');
-		printIndented ("\t", plot_adds);
-	}
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('rk.graph.off ()\n');

Modified: trunk/rkward/rkward/plugins/plots/box_plot.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/box_plot.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/box_plot.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -16,7 +16,6 @@
 	var pch_sd_high = getValue ("pch_sd_high");
 	var pch_sd_low = getValue ("pch_sd_low");
 	var horizontal = getValue ("orientation") == "TRUE";
-	var plot_adds = getValue ("plotoptions.code.calculate"); //add grid and alike
 	var boxwex = getValue ("boxwex");
 	if (!dodged && (Number (boxwex) != .8)) boxwex = ", boxwex=" + boxwex;
 	else boxwex = "";
@@ -54,7 +53,10 @@
 		}
 		echo ('rk.graph.on()\n');
 	}
+
 	echo ('try ({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
+
 	echo ('	boxplot (data_list' + boxwex + positions + ', notch = ' + getValue ("notch") + ', outline = ' + getValue("outline") + ', horizontal = ' + getValue("orientation") + getValue ("plotoptions.code.printout") + ') #actual boxplot function\n');
 	if (do_mean) {
 	    var mean_fun = "mean, na.rm=TRUE";
@@ -90,14 +92,10 @@
 			echo ('	points(sd_high, pch=' + pch_sd_high + ', cex = ' + getValue ("cex_sd_mean") + getValue ("sd_mean_color.code.printout") + ')\n');
 		}
 	}
+
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n');
 	echo ('})\n');	// end of try ()
 
-	if (plot_adds.length > 0) {
-		echo ('\n');
-		// print the grid() related code
-		printIndented ("\t", plot_adds);
-	}
-
 	if (full) {
 		echo ('rk.graph.off ()\n');
 	}

Modified: trunk/rkward/rkward/plugins/plots/density_plot.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/density_plot.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/density_plot.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -52,6 +52,8 @@
 	}
 
 	echo ('try ({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
+
 	if (dodensity_plot) {
 		echo ('	plot(' + density_call + getValue ("plotoptions.code.printout") + ')\n');
 	} else {
@@ -60,6 +62,8 @@
 	if (dorug) {
 		echo ('	rug(' + x + ', ' + getValue ("rug_ticksize") + ', ' + getValue ("rug_lwd") + ', ' + getValue ("rug_side") + getValue ("rug_col.code.printout") + ')\n');
 	}
+
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('rk.graph.off ()\n');

Modified: trunk/rkward/rkward/plugins/plots/dotchart.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/dotchart.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/dotchart.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -20,9 +20,6 @@
 
 	var options = getValue ("plotoptions.code.printout");
 
-	var plotpre = getValue ("plotoptions.code.preprocess");
-	var plotpost = getValue ("plotoptions.code.calculate");
-
 	if (tabulate) {
 		echo (getValue ('tabulate_options.code.calculate'));
 		echo ('n <- names (x); x <- as.numeric (x); names (x) <- n		# dotchart() is somewhat picky about data type\n');
@@ -50,9 +47,9 @@
 	} else if (names_mode == "custom") {
 		echo ("names(x) <- c (\"" + str_replace (";", "\", \"", trim (getValue ("names_custom"))) + "\")\n");
 	}
-	if (plotpre != "") printIndented ("\t", plotpre);
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
 	echo ('	dotchart(x' + options + ')\n');
-	if (plotpost != "") printIndented ("\t", plotpost);
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('rk.graph.off ()\n');

Modified: trunk/rkward/rkward/plugins/plots/ecdf_plot.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/ecdf_plot.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/ecdf_plot.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -26,6 +26,8 @@
 		echo ('rk.graph.on ()\n');
 	}
 	echo ('try ({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
+
 	echo ('	plot.ecdf (' + x + ', ' + getValue ("stepfun_options.code.printout") + getValue ("plotoptions.code.printout") + ')\n');
 	if (getValue ("th_pnorm")) {
 		echo ('	curve (pnorm');
@@ -35,6 +37,8 @@
 	if (getValue ("rug")) {
 		echo ('	rug (' + x + ', ' + getValue ("ticksize") + ', ' + getValue ("lwd") + ', ' + getValue ("side") + getValue ("col_rug.code.printout") + ')\n');
 	}
+
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('rk.graph.off ()\n');

Modified: trunk/rkward/rkward/plugins/plots/histogram.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/histogram.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/histogram.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -14,7 +14,6 @@
 	var narm = getValue ("narm");
 	var n = getValue ("n"); //called "resolution"
 	var x = getValue ("x");
-	var plot_adds = getValue ("plotoptions.code.calculate");
 
 	if (full) {
 		echo ('rk.header ("Histogram", list ("Variable", rk.get.description (' + x + ') ');
@@ -35,16 +34,14 @@
 	}
 
 	echo ('try ({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
+
 	echo ('	hist (' + x + getValue ("histogram_opt.code.calculate") + getValue ("histogram_opt.code.printout") + getValue ("plotoptions.code.printout") + ')\n');
 	if ((densityscaled) && getValue ("density")) {
 		echo ('	lines(density(' + x + ', bw="' + bw + '", adjust = ' + adjust + ', ' + narm + ', n = ' + getValue ("n") + ')' + getValue ("col_density.code.printout") + ')\n');
 	}
-	if (plot_adds.length > 0) {
-		echo ('\n');
-		// print the grid() related code
-		printIndented ("\t", plot_adds);
-	}
-
+	
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('rk.graph.off ()\n');

Modified: trunk/rkward/rkward/plugins/plots/pareto.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/pareto.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/pareto.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -43,10 +43,14 @@
 	}
 
 	echo ('try ({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
+
 	echo ('\t');
 	if (full && descriptives) echo ('descriptives <- ');
 	echo ('pareto.chart(x' + getValue ("plotoptions.code.printout") + ')\n');
 	if (full && descriptives) echo ('	rk.results(data.frame(descriptives))\n');
+
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('rk.graph.off ()\n');

Modified: trunk/rkward/rkward/plugins/plots/piechart.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/piechart.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/piechart.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -44,9 +44,6 @@
 	else if (col == "grayscale") options += ", col=gray.colors (if(is.matrix(x)) dim(x) else length(x))";
 	options += getValue ("plotoptions.code.printout");
 
-	var plotpre = getValue ("plotoptions.code.preprocess");
-	var plotpost = getValue ("plotoptions.code.calculate");
-
 	if (tabulate) {
 		echo (getValue ('tabulate_options.code.calculate'));
 	} else {
@@ -68,7 +65,7 @@
 	}
 
 	echo ('try ({\n');
-	if (plotpre.length > 0) printIndented ("\t", plotpre);
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
 	if (names_mode == "rexp") {
 		echo ("\tnames(x) <- " + getValue ("names_exp") + "\n");
 	} else if (names_mode == "custom") {
@@ -76,7 +73,7 @@
 	}
 
 	echo ('	pie(x' + options + ')\n');
-	if (plotpost.length > 0) printIndented ("\t", plotpost);
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('rk.graph.off ()\n');

Modified: trunk/rkward/rkward/plugins/plots/plot.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/plot.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/plot.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -12,8 +12,6 @@
 	if (!(y == "")) {
 		y = ", " + y;
 	}
-	// get additional code (as of now grid) from the calculate section
-	var plot_adds = getValue ("plotoptions.code.calculate");
 
 	if (full) {
 
@@ -22,13 +20,11 @@
 	}
 
 	echo ('try({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
+
 	echo ('	plot(' + x + y + getValue ("plotoptions.code.printout") + ');\n');
-	if (plot_adds.length > 0) {
-		echo ('\n');
-		// print the grid() related code
-		printIndented ("\t", plot_adds);
-	}
 
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 

Modified: trunk/rkward/rkward/plugins/plots/plot_options.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/plot_options.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/plot_options.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -11,6 +11,11 @@
 	return label;
 }
 
+function preprocess () {
+	if (getValue ("custom_margins.checked")) {
+		echo ("par (mar=c (" + getValue ("margin_bottom") + ", " + getValue ("margin_left") + ", " + getValue ("margin_top") + ", " + getValue ("margin_right") + "))\n");
+	}
+}
 
 function calculate () {
 	if (getValue ("grid_enable") == "true") {

Modified: trunk/rkward/rkward/plugins/plots/plot_options.rkh
===================================================================
--- trunk/rkward/rkward/plugins/plots/plot_options.rkh	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/plot_options.rkh	2012-09-26 10:18:10 UTC (rev 4310)
@@ -14,6 +14,11 @@
 		<setting id="mainisquote">If this box is checked, the <i>main</i> title will be used literally (quoted). Otherwise is will be interpreted as an R statement</setting>
 		<setting id="sub">This is the <i>sub</i> argument to plot.default. See R reference on <b>plot.default</b>. To achieve a line-break (in a quoted string), enter '\n'.</setting>
 		<setting id="subisquote">If this box is checked, the <i>sub</i> title will be used literally (quoted). Otherwise is will be interpreted as an R statement</setting>
+		<setting id="custom_margins">Enable this option, if you want to customize the margins.</setting>
+		<setting id="margin_top">Size of top margin. Size is specified in (approximate) line heights.</setting>
+		<setting id="margin_bottom">Size of bottom margin. Size is specified in (approximate) line heights.</setting>
+		<setting id="margin_left">Size of left margin. Size is specified in (approximate) line heights.</setting>
+		<setting id="margin_right">Size of right margin. Size is specified in (approximate) line heights.</setting>
 
 		<caption id="axespage"/>
 		<setting id="xaxt">Uncheck this box to avoid drawing the X axis. This is the <i>xaxt</i> argument to par. See R reference on <b>par</b>.</setting>

Modified: trunk/rkward/rkward/plugins/plots/plot_options.xml
===================================================================
--- trunk/rkward/rkward/plugins/plots/plot_options.xml	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/plot_options.xml	2012-09-26 10:18:10 UTC (rev 4310)
@@ -72,7 +72,7 @@
 	</logic>
 	<dialog label="Default plot options" >
 		<tabbook>
-			<tab id="titlepage" label="Title">
+			<tab id="titlepage" label="Title and margins">
 				<column>
 					<frame label="Main title">
 						<input id="main" label="Main title (on top). Leave empty for default title" />
@@ -82,6 +82,12 @@
 						<input id="sub" label="Sub-title (at bottom). Leave empty for default title" />
 						<checkbox id="subisquote" label="Interpret this label as literal (quoted) string" checked="true" value="1" />
 					</frame>
+					<frame id="custom_margins" label="Custom margins" checkable="true" checked="false">
+						<spinbox id="margin_top" type="real" initial="4" default_precision="0" label="Top"/>
+						<spinbox id="margin_bottom" type="real" initial="5" default_precision="0" label="Bottom"/>
+						<spinbox id="margin_left" type="real" initial="4" default_precision="0" label="Left"/>
+						<spinbox id="margin_right" type="real" initial="2" default_precision="0" label="Right"/>
+					</frame>
 					<stretch/>
 				</column>
 			</tab>

Modified: trunk/rkward/rkward/plugins/plots/scatterplot.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/scatterplot.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/scatterplot.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -54,6 +54,8 @@
 		echo ('\n');
 	}
 	echo ('try ({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
+
 	echo ('	# make frame and axes\n');
 	echo ('	plot(Xrange, Yrange, type="n"' + getValue ("plotoptions.code.printout") + ')\n');
 	echo ('	\n');
@@ -68,6 +70,8 @@
 	echo ('			pch = pch[[i]]\n');
 	echo ('		)\n');
 	echo ('	}\n');
+
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('\n');

Modified: trunk/rkward/rkward/plugins/plots/sieve_plot.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/sieve_plot.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/sieve_plot.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -37,15 +37,12 @@
 		echo ('rk.graph.on ()\n');
 	}
 
-	var plotpre = getValue ("plotoptions.code.preprocess");
-	var plotpost = getValue ("plotoptions.code.calculate");
-
 	echo ('try ({\n');
-	if (plotpre.length > 0) printIndented ("\t", plotpre);
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
 
-
 	echo ('	sieve(x, shade = ' + shade + ', sievetype = "' + sievetype + '" ' + getValue ("plotoptions.code.printout") + ')\n');
-	if (plotpost.length > 0) printIndented ("\t", plotpost);
+
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
 	echo ('})\n');
 	if (full) {
 		echo ('rk.graph.off ()\n');

Modified: trunk/rkward/rkward/plugins/plots/stripchart_plot.js
===================================================================
--- trunk/rkward/rkward/plugins/plots/stripchart_plot.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/plugins/plots/stripchart_plot.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -21,20 +21,20 @@
 	}
 	var orientation = getValue ("orientation");
 	if (orientation == "Vertical") opts += ", vertical = TRUE";
-	var plot_adds = getValue ("plotoptions.code.calculate"); //add grid and alike
 
 	if (full) {
 		echo ('rk.header ("Stripchart", list ("Variable"=rk.get.description (' + x + '), "Group"=rk.get.description (' + g + '), "Method"=' + method + params + ', "Orientation"="' + orientation + '"))\n');
 		echo ('\n');
 		echo ('rk.graph.on ()\n');
 	}
-	echo ('try (stripchart (' + x + ' ~ (' + g + '), method = ' + method + opts + getValue ("plotoptions.code.printout") + '))\n');
-	if (plot_adds.length > 0) {
-		echo ('\n');
-		// print the grid() related code
-		printIndented ("\t", plot_adds);
-	}
+	echo ('try ({\n');
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.preprocess"), '', '\n');
 
+	echo ('\tstripchart (' + x + ' ~ (' + g + '), method = ' + method + opts + getValue ("plotoptions.code.printout") + ')\n');
+
+	printIndentedUnlessEmpty ("\t", getValue ("plotoptions.code.calculate"), '\n', '');
+	echo ('})\n');
+
 	if (full) {
 		echo ('rk.graph.off ()\n');
 	}

Modified: trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal_graphics.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal_graphics.R	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal_graphics.R	2012-09-26 10:18:10 UTC (rev 4310)
@@ -55,13 +55,14 @@
 ".rk.startPreviewDevice" <- function (x) {
 	a <- .rk.variables$.rk.preview.devices[[x]]
 	if (is.null (a)) {
-		a <- dev.cur ()
+		devnum <- dev.cur ()
 		x11 (is.preview.device = TRUE)
-		if (a != dev.cur ()) {
-			.rk.variables$.rk.preview.devices[[x]] <- dev.cur ()
+		if (devnum != dev.cur ()) {
+			.rk.variables$.rk.preview.devices[[x]] <- list (devnum=dev.cur(), par=par (no.readonly=TRUE))
 		}
 	} else {
-		dev.set (a)
+		dev.set (a$devnum)
+		par (a$par)
 	}
 }
 
@@ -69,8 +70,8 @@
 ".rk.killPreviewDevice" <- function (x) {
 	a <- .rk.variables$.rk.preview.devices[[x]]
 	if (!is.null (a)) {
-		if (a %in% dev.list ()) {
-			dev.off (a)
+		if (a$devnum %in% dev.list ()) {
+			dev.off (a$devnum)
 		}
 		.rk.variables$.rk.preview.devices[[x]] <- NULL
 	}

Modified: trunk/rkward/rkward/scriptbackends/common.js
===================================================================
--- trunk/rkward/rkward/scriptbackends/common.js	2012-09-25 18:13:35 UTC (rev 4309)
+++ trunk/rkward/rkward/scriptbackends/common.js	2012-09-26 10:18:10 UTC (rev 4310)
@@ -10,6 +10,13 @@
 	echo (indentation + lines.replace (/\n(.)/g, "\n" + indentation + "$1"));
 }
 
+printIndentedUnlessEmpty = function (indentation, lines, pre, post) {
+	if (lines.length <= 0) return;
+	if (typeof (pre) != 'undefined') echo (pre);
+	printIndented (indentation, lines);
+	if (typeof (post) != 'undefined') echo (post);
+}
+
 noquote = function (text) {
 	var ret = new String (text);
 	ret.noquote = 1;

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.





More information about the rkward-tracker mailing list