[rkward/work/generalized_preview] rkward: Also support HTML previews out of the box.

Thomas Friedrichsmeier thomas.friedrichsmeier at ruhr-uni-bochum.de
Sun Jan 10 21:14:04 UTC 2016


Git commit fd01965a008ab95226e12b30d58277477dbde0f3 by Thomas Friedrichsmeier.
Committed on 10/01/2016 at 21:13.
Pushed by tfry into branch 'work/generalized_preview'.

Also support HTML previews out of the box.

(TODO: document)

M  +27   -5    rkward/plugin/rkpreviewbox.cpp
M  +1    -0    rkward/plugin/rkpreviewbox.h
M  +5    -1    rkward/plugins/testing/preview.js
M  +2    -1    rkward/plugins/testing/preview.xml

http://commits.kde.org/rkward/fd01965a008ab95226e12b30d58277477dbde0f3

diff --git a/rkward/plugin/rkpreviewbox.cpp b/rkward/plugin/rkpreviewbox.cpp
index f86e456..467541c 100644
--- a/rkward/plugin/rkpreviewbox.cpp
+++ b/rkward/plugin/rkpreviewbox.cpp
@@ -44,7 +44,7 @@ RKPreviewBox::RKPreviewBox (const QDomElement &element, RKComponent *parent_comp
 	// get xml-helper
 	XMLHelper *xml = parent_component->xmlHelper ();
 
-	preview_mode = (PreviewMode) xml->getMultiChoiceAttribute (element, "mode", "plot;data;custom", 0, DL_INFO);
+	preview_mode = (PreviewMode) xml->getMultiChoiceAttribute (element, "mode", "plot;data;html;custom", 0, DL_INFO);
 	placement = (PreviewPlacement) xml->getMultiChoiceAttribute (element, "placement", "default;attached;detached;docked", (preview_mode == PlotPreview) ? 0 : 3, DL_INFO);
 	preview_active = xml->getBoolAttribute (element, "active", false, DL_INFO);
 	idprop = RObject::rQuote (QString ().sprintf ("%p", this));
@@ -79,9 +79,21 @@ RKPreviewBox::RKPreviewBox (const QDomElement &element, RKComponent *parent_comp
 			QWidget *container = new KVBox ();
 			RKWorkplace::mainWorkplace ()->registerNamedWindow (idprop, this, container);
 			uicomp->addDockedPreview (container, state, toggle_preview_box->text ());
-			// create an empty data.frame as dummy. This is only really appropriate for data-previews, but even for other docked previews it has the effect of initializing the preview area
-			// with _something_.
-			RKGlobals::rInterface ()->issueCommand ("local ({\nrk.assign.preview.data(" + idprop + ", data.frame ())\n})\n" + placement_command + "rk.edit(rkward::.rk.variables$.rk.preview.data[[" + idprop + "]])" + placement_end, RCommand::Plugin | RCommand::Sync);
+
+			if (preview_mode == HtmlPreview) {
+				RKGlobals::rInterface ()->issueCommand ("local ({\n"
+				    "outfile <- tempfile (fileext='html')\n"
+				    "rk.assign.preview.data(" + idprop + ", list (filename=outfile, on.delete=function (id) {\n"
+				    "	rk.flush.output (outfile, ask=FALSE)\n"
+				    "	unlink (outfile)\n"
+				    "}))\n"
+				    "oldfile <- rk.set.output.html.file (outfile)  # for initialization\n"
+				    "rk.set.output.html.file (oldfile)\n"
+				    "})\n" + placement_command + "rk.show.html(rk.get.preview.data (" + idprop + ")$filename)" + placement_end, RCommand::Plugin | RCommand::Sync);
+			} else {
+				// For all others, create an empty data.frame as dummy. Even for custom docked previews it has the effect of initializing the preview area with _something_.
+				RKGlobals::rInterface ()->issueCommand ("local ({\nrk.assign.preview.data(" + idprop + ", data.frame ())\n})\n" + placement_command + "rk.edit(rkward::.rk.variables$.rk.preview.data[[" + idprop + "]])" + placement_end, RCommand::Plugin | RCommand::Sync);
+			}
 
 			// A bit of a hack: For now, in wizards, docked previews are always active, and control boxes are meaningless.
 			if (uicomp->isWizardish ()) {
@@ -185,6 +197,13 @@ void RKPreviewBox::tryPreviewNow () {
 		RKGlobals::rInterface ()->issueCommand ("local({\n" + code_property->preview () + "})\n", RCommand::Plugin | RCommand::Sync, QString (), this, DO_PREVIEW);
 	} else if (preview_mode == DataPreview) {
 		RKGlobals::rInterface ()->issueCommand ("local({try({\n" + code_property->preview () + "\n})\nif(!exists(\"preview_data\",inherits=FALSE)) preview_data <- data.frame ('ERROR')\nrk.assign.preview.data(" + idprop + ", preview_data)\n})\n" + placement_command + "rk.edit(rkward::.rk.variables$.rk.preview.data[[" + idprop + "]])" + placement_end, RCommand::Plugin | RCommand::Sync, QString (), this, DO_PREVIEW);
+	} else if (preview_mode == HtmlPreview) {
+		RKGlobals::rInterface ()->issueCommand (placement_command + "local({\n"
+		    "	oldfile <- rk.set.output.html.file(rk.get.preview.data (" + idprop + ")$filename)\n"
+		    "	rk.flush.output(ask=FALSE)\n"
+		    "	local({try({\n" + code_property->preview () + "\n})})\n"  // nested local to make sure "oldfile" is not overwritten.
+		    "	rk.set.output.html.file(oldfile)\n})\n"
+		    "rk.show.html(rk.get.preview.data (" + idprop + ")$filename)" + placement_end, RCommand::Plugin | RCommand::Sync, QString (), this, DO_PREVIEW);
 	} else {
 		RKGlobals::rInterface ()->issueCommand ("local({\n" + placement_command + code_property->preview () + placement_end + "})\n", RCommand::Plugin | RCommand::Sync, QString (), this, DO_PREVIEW);
 	}
@@ -209,7 +228,10 @@ void RKPreviewBox::killPreview (bool force) {
 	if (!preview_active) return;
 	preview_active = false;
 
-	if (force || placement != DockedPreview) {
+	// hmm, lots of special casing, here. But the reasons are:
+	// - docked preview are only hidden, really, and their window should not be destroyed
+	// - for HTML previews, removing the file has no effect in the first place, other than making code more difficult...
+	if (force || (placement != DockedPreview && preview_mode != HtmlPreview)) {
 		QString command;
 		if (preview_mode == PlotPreview) command = ".rk.killPreviewDevice (" + idprop + ')';
 		else command = "rk.discard.preview.data (" + idprop + ')';
diff --git a/rkward/plugin/rkpreviewbox.h b/rkward/plugin/rkpreviewbox.h
index 547446d..e5760f3 100644
--- a/rkward/plugin/rkpreviewbox.h
+++ b/rkward/plugin/rkpreviewbox.h
@@ -61,6 +61,7 @@ private:
 	enum PreviewMode {
 		PlotPreview,
 		DataPreview,
+		HtmlPreview,
 		CustomPreview
 	} preview_mode;
 	enum PreviewPlacement {
diff --git a/rkward/plugins/testing/preview.js b/rkward/plugins/testing/preview.js
index 18dd918..3b3dfd8 100644
--- a/rkward/plugins/testing/preview.js
+++ b/rkward/plugins/testing/preview.js
@@ -8,5 +8,9 @@ function printout () {
 
 function doPrintout (real) {
 	echo ('x <- as.data.frame (matrix (' + getString ("value") + ', nrow=' + getString ("size") + ', ncol=' + getString ("size") + '))\n');
-	if (!real) echo ('preview_data <- x');
+	if (!real) {
+		// Not to be imitated, for testing purposes, we handle both a data-preview, and an html-preview, here
+		echo ('preview_data <- x\n');
+		echo ('rk.print (x)\n');
+	}
 }
\ No newline at end of file
diff --git a/rkward/plugins/testing/preview.xml b/rkward/plugins/testing/preview.xml
index 8632925..6e76267 100644
--- a/rkward/plugins/testing/preview.xml
+++ b/rkward/plugins/testing/preview.xml
@@ -6,7 +6,8 @@
 	<dialog label="Testing various preview features">
 		<spinbox id="size" type="integer" min="0" label="Size" initial="3"/>
 		<spinbox id="value" type="real" min="0" label="Value" initial="1.23"/>
-		<preview mode="data" label="Preview data"/>
+		<!--		<preview mode="data" label="Preview data"/> -->
+		<preview mode="html" label="Preview data"/>
 	</dialog>
 	<wizard label="Testing various preview features">
 		<page>



More information about the rkward-tracker mailing list