[rkward-cvs] SF.net SVN: rkward:[3897] trunk/rkward/packages/rkwarddev

m-eik at users.sourceforge.net m-eik at users.sourceforge.net
Thu Oct 6 21:30:29 UTC 2011


Revision: 3897
          http://rkward.svn.sourceforge.net/rkward/?rev=3897&view=rev
Author:   m-eik
Date:     2011-10-06 21:30:28 +0000 (Thu, 06 Oct 2011)
Log Message:
-----------
rkwarddev: saveobject nodes can now trigger automatic javascript code generation

Modified Paths:
--------------
    trunk/rkward/packages/rkwarddev/ChangeLog
    trunk/rkward/packages/rkwarddev/DESCRIPTION
    trunk/rkward/packages/rkwarddev/NAMESPACE
    trunk/rkward/packages/rkwarddev/R/rk-internal.R
    trunk/rkward/packages/rkwarddev/R/rk.plugin.skeleton.R
    trunk/rkward/packages/rkwarddev/man/rk.plugin.skeleton.Rd

Added Paths:
-----------
    trunk/rkward/packages/rkwarddev/R/rk.JS.saveobj.R
    trunk/rkward/packages/rkwarddev/man/rk.JS.saveobj.Rd

Modified: trunk/rkward/packages/rkwarddev/ChangeLog
===================================================================
--- trunk/rkward/packages/rkwarddev/ChangeLog	2011-10-06 16:07:10 UTC (rev 3896)
+++ trunk/rkward/packages/rkwarddev/ChangeLog	2011-10-06 21:30:28 UTC (rev 3897)
@@ -1,12 +1,13 @@
 ChangeLog for package rkwarddev
 
-## 0.02-5 (2011-10-05)
+## 0.02-5 (2011-10-06)
   - added functions rk.rkh.caption(), rk.rkh.link(), rk.rkh.related(), rk.rkh.section(), rk.rkh.setting(),
     rk.rkh.settings(), rk.rkh.summary(), rk.rkh.technical(), rk.rkh.title() and rk.rkh.usage()
   - rk.rkh.doc() now only accepts nodes of the above functions as input (and gained support for title and sections)
   - rk.rkh.scan() now also looks for captions
   - added full help page support to rk.plugin.skeleton()
   - added support for properties to rk.JS.vars()
+  - added function rk.JS.saveobj() to generate JS code for saveobject nodes.
   - added function rk.JS.options() to generate code for JS variables holding multiple options
   - added class rk.JS.opt and a show method for it (use rk.paste.JS() on that)
   - id() and echo() now also replace objects of classes rk.JS.opt and rk.JS.arr with their relevant ID

Modified: trunk/rkward/packages/rkwarddev/DESCRIPTION
===================================================================
--- trunk/rkward/packages/rkwarddev/DESCRIPTION	2011-10-06 16:07:10 UTC (rev 3896)
+++ trunk/rkward/packages/rkwarddev/DESCRIPTION	2011-10-06 21:30:28 UTC (rev 3897)
@@ -28,6 +28,7 @@
     'rk.JS.doc.R'
     'rk.JS.opt-class.R'
     'rk.JS.options.R'
+    'rk.JS.saveobj.R'
     'rk.JS.scan.R'
     'rk.JS.vars.R'
     'rk.paste.JS.R'

Modified: trunk/rkward/packages/rkwarddev/NAMESPACE
===================================================================
--- trunk/rkward/packages/rkwarddev/NAMESPACE	2011-10-06 16:07:10 UTC (rev 3896)
+++ trunk/rkward/packages/rkwarddev/NAMESPACE	2011-10-06 21:30:28 UTC (rev 3897)
@@ -9,6 +9,7 @@
 export(rk.JS.array)
 export(rk.JS.doc)
 export(rk.JS.options)
+export(rk.JS.saveobj)
 export(rk.JS.scan)
 export(rk.JS.vars)
 export(rk.paste.JS)

Modified: trunk/rkward/packages/rkwarddev/R/rk-internal.R
===================================================================
--- trunk/rkward/packages/rkwarddev/R/rk-internal.R	2011-10-06 16:07:10 UTC (rev 3896)
+++ trunk/rkward/packages/rkwarddev/R/rk-internal.R	2011-10-06 21:30:28 UTC (rev 3897)
@@ -69,8 +69,10 @@
 		} else {
 			this.tag.name <- tolower(XiMpLe:::XML.tagName(this.tag))
 			# we're only interested in entries with an ID
-			if(this.tag.name %in% relevant.tags & grepl("[[:space:]]+id=", this.tag)){
-				cleaned.tags[length(cleaned.tags)+1] <- this.tag
+			if(this.tag.name %in% relevant.tags){
+				if("id" %in% names(XiMpLe:::parseXMLAttr(this.tag))){
+					cleaned.tags[length(cleaned.tags)+1] <- this.tag
+				} else {}
 			} else {}
 		}
 	}

Added: trunk/rkward/packages/rkwarddev/R/rk.JS.saveobj.R
===================================================================
--- trunk/rkward/packages/rkwarddev/R/rk.JS.saveobj.R	                        (rev 0)
+++ trunk/rkward/packages/rkwarddev/R/rk.JS.saveobj.R	2011-10-06 21:30:28 UTC (rev 3897)
@@ -0,0 +1,101 @@
+#' Create JavaScript saveobject code from plugin XML
+#'
+#' @param pXML Either an object of class \code{XiMpLe.doc} or \code{XiMpLe.node}, or path to a plugin XML file.
+#' @param R.objects Character vector, the names of the internal R objects to be saved. If not empty must have
+#'		the same length as <saveobject> nodes in the document, or be the keyword "initial", in which case the
+#'		\code{intital} attribute values of the nodes are used.
+#' @param vars Logocal, whether the variables needed should also be defined in the JavaScript code.
+#' @param add.abbrev Logical, if \code{TRUE} the JavaScript variables will all have a prefix with an
+#'		three letter abbreviation of the XML tag type to improve the readability of the code. But it's
+#'		probably better to add this in the XML code in the first place.
+#' @param indent.by Character string used to indent each entry if \code{js=TRUE}.
+#' @return A character vector.
+#' @seealso \href{help:rkwardplugins}{Introduction to Writing Plugins for RKWard}
+#' @export
+
+rk.JS.saveobj <- function(pXML, R.objects="initial", vars=TRUE, add.abbrev=FALSE, indent.by="\t"){
+
+	single.tags <- get.single.tags(XML.obj=pXML, drop=c("comments","cdata", "declarations", "doctype"))
+
+	# filter for relevant tags
+	cleaned.tags <- list()
+	for(this.tag in child.list(single.tags)){
+		this.tag.name <- tolower(XiMpLe:::XML.tagName(this.tag))
+		# we're only interested in entries with an ID
+		if(identical(this.tag.name, "saveobject")){
+			if("id" %in% names(XiMpLe:::parseXMLAttr(this.tag))){
+				cleaned.tags[length(cleaned.tags)+1] <- this.tag
+			} else {}
+		} else {}
+	}
+
+	num.tags <- length(cleaned.tags)
+
+	if(!is.null(R.objects)){
+		num.obj <- length(R.objects)
+		if(num.obj != num.tags & !identical(R.objects, "initial")){
+			stop(simpleError(paste("Length of 'R.objects' (",num.obj,") is unequal to saveobject nodes found:\n  ",
+				paste(unlist(cleaned.tags), collapse="\n  "), sep="")))
+		} else {}
+	} else {}
+	
+	if(length(cleaned.tags) > 0){
+		if(isTRUE(vars)){
+			JS.vars <- paste(unlist(sapply(1:num.tags, function(this.tagnum){
+					this.tag <- cleaned.tags[this.tagnum]
+					if(XiMpLe:::parseXMLAttr(this.tag)[["checkable"]] %in% c("T", "true", "TRUE", "1")){
+						properties=c("active", "parent")
+					} else {
+						properties="parent"
+					}
+					JS.id <- get.IDs(single.tags=this.tag, relevant.tags="saveobject", add.abbrev=add.abbrev)
+					return(get.JS.vars(
+						JS.var=JS.id[1,"abbrev"],
+						XML.var=JS.id[1,"id"],
+						properties=properties,
+						default=TRUE,
+						indent.by=indent.by))
+				})), collapse="")
+		} else {
+			JS.vars <- NULL
+		}
+		JS.assign <- paste(unlist(sapply(1:num.tags, function(this.tagnum){
+				this.tag <- cleaned.tags[this.tagnum]
+				JS.id <- get.IDs(single.tags=this.tag, relevant.tags="saveobject", add.abbrev=add.abbrev)
+				JS.var <- camelCode(JS.id[1,"abbrev"])
+				JS.var.parent <- camelCode(c(JS.id[1,"abbrev"], "parent"))
+				if(is.null(R.objects)){
+					this.obj <- "REPLACE.ME.obj"
+				} else {
+					if(identical(R.objects, "initial")){
+						this.obj <- XiMpLe:::parseXMLAttr(this.tag)[["initial"]]
+					} else {
+						this.obj <- R.objects[this.tagnum]
+					}
+				}
+				# this can't be done by echo() because aof the substitution
+				echo.code <- paste("echo(\"assign(\\\"\" + ", JS.var, " + \"\\\", ", this.obj, ", envir=\" + ", JS.var.parent, " + \")\\n\");", sep="")
+				if(XiMpLe:::parseXMLAttr(this.tag)[["checkable"]] %in% c("T", "true", "TRUE", "1")){
+					JS.var.active <- camelCode(c(JS.id[1,"abbrev"], "active"))
+					JS.code <- ite(JS.var.active, echo.code)
+				} else {
+					JS.code <- echo.code
+				}
+				return(rk.paste.JS(JS.code, level=2, indent.by=indent.by))
+			})), collapse="\n")
+	} else {
+		return(invisible(NULL))
+	}
+
+# 	var saveCorpFreq			= getValue("saveCorpFreq.active");
+# 	var saveCorpFreqName		= getValue("saveCorpFreq");
+# 	var saveCorpFreqEnv		= getValue("saveCorpFreq.parent");
+
+# 	if (saveFreq) {
+# 		echo("assign(\""+saveFreqName+"\", REPLACE.ME.obj, envir="+saveFreqEnv+")\n");
+# 	}
+
+	results <- paste(JS.vars, "\n", JS.assign, sep="")
+
+	return(results)
+}

Modified: trunk/rkward/packages/rkwarddev/R/rk.plugin.skeleton.R
===================================================================
--- trunk/rkward/packages/rkwarddev/R/rk.plugin.skeleton.R	2011-10-06 16:07:10 UTC (rev 3896)
+++ trunk/rkward/packages/rkwarddev/R/rk.plugin.skeleton.R	2011-10-06 21:30:28 UTC (rev 3897)
@@ -30,10 +30,14 @@
 #' @param results.header A character string to headline the printed results.
 #' @param JS.prep A character string with JavaScript code to be included in the \code{preprocess()} function of the .js file. This string will be
 #'		pasted as-is, see \code{\link[rkwarddev:rk.JS.doc]{rk.JS.doc}}.
-#' @param JS.calc A character string with JavaScript code to be included in the \code{calculate()} function of the .js file. This string will be
+#' @param JS.calc Either a character string with JavaScript code to be included in the \code{calculate()} function of the .js file. This string will be
 #'		pasted as-is, see \code{\link[rkwarddev:rk.JS.doc]{rk.JS.doc}}.
 #' @param JS.prnt A character string with JavaScript code to be included in the \code{printout()} function of the .js file. This string will be
 #'		pasted as-is, see \code{\link[rkwarddev:rk.JS.doc]{rk.JS.doc}}.
+#' @param var.scan Logical, if \code{TRUE} \code{\link{rk.JS.scan}} will be called automatically to  to define the needed variables
+#'		which will be added to the code in the \code{calculate()} function.
+#' @param saveobj.scan Logical, if \code{TRUE} \code{\link{rk.JS.saveobj}} will be called automatically to generate code to save R results
+#'		which will be appended to the code in the \code{printout()} function.
 #' @param summary An object of class \code{XiMpLe.node} to be pasted as the \code{<summary>} section of the help file. See
 #'		\code{\link[rkwarddev:rk.rkh.summary]{rk.rkh.summary}} for details.
 #' @param usage An object of class \code{XiMpLe.node} to be pasted as the \code{<usage>} section of the help file. See
@@ -126,7 +130,7 @@
 
 rk.plugin.skeleton <- function(name, about=NULL, path=tempdir(), dialog=NULL, wizard=NULL, logic=NULL, snippets=NULL,
 	provides=c("logic", "dialog"), dial.require=c(), overwrite=FALSE, tests=TRUE, lazyLoad=TRUE, menu="test", results.header=NULL,
-	JS.prep=NULL, JS.calc=NULL, JS.prnt=NULL,
+	JS.prep=NULL, JS.calc=NULL, JS.prnt=NULL, var.scan=TRUE, saveobj.scan=TRUE,
 	summary=NULL, usage=NULL, sections=NULL, settings="scan", related=NULL, technical=NULL,
 	create=c("pmap", "xml", "js", "rkh", "desc"), edit=FALSE, load=FALSE, show=FALSE){
 	# to besure, remove all non-character symbols from name
@@ -219,9 +223,17 @@
 			if(is.null(results.header)){
 				results.header <- paste(name.orig, " results", sep="")
 			} else {}
+			if(isTRUE(var.scan)){
+				variables <- rk.JS.scan(XML.plugin)
+			} else {
+				variables <- NULL
+			}
+			if(isTRUE(saveobj.scan)){
+				JS.prnt <- paste(JS.prnt, rk.JS.saveobj(XML.plugin), sep="\n")
+			} else {}
 			JS.code <- rk.JS.doc(
 				require=dial.require,
-				variables=rk.JS.scan(XML.plugin),
+				variables=variables,
 				results.header=results.header,
 				preprocess=JS.prep,
 				calculate=JS.calc,

Added: trunk/rkward/packages/rkwarddev/man/rk.JS.saveobj.Rd
===================================================================
--- trunk/rkward/packages/rkwarddev/man/rk.JS.saveobj.Rd	                        (rev 0)
+++ trunk/rkward/packages/rkwarddev/man/rk.JS.saveobj.Rd	2011-10-06 21:30:28 UTC (rev 3897)
@@ -0,0 +1,40 @@
+\name{rk.JS.saveobj}
+\alias{rk.JS.saveobj}
+\title{Create JavaScript saveobject code from plugin XML}
+\usage{
+  rk.JS.saveobj(pXML, R.objects = "initial", vars = TRUE,
+  add.abbrev = FALSE, indent.by = "\t")
+}
+\arguments{
+  \item{pXML}{Either an object of class \code{XiMpLe.doc}
+  or \code{XiMpLe.node}, or path to a plugin XML file.}
+
+  \item{R.objects}{Character vector, the names of the
+  internal R objects to be saved. If not empty must have
+  the same length as <saveobject> nodes in the document, or
+  be the keyword "initial", in which case the
+  \code{intital} attribute values of the nodes are used.}
+
+  \item{vars}{Logocal, whether the variables needed should
+  also be defined in the JavaScript code.}
+
+  \item{add.abbrev}{Logical, if \code{TRUE} the JavaScript
+  variables will all have a prefix with an three letter
+  abbreviation of the XML tag type to improve the
+  readability of the code. But it's probably better to add
+  this in the XML code in the first place.}
+
+  \item{indent.by}{Character string used to indent each
+  entry if \code{js=TRUE}.}
+}
+\value{
+  A character vector.
+}
+\description{
+  Create JavaScript saveobject code from plugin XML
+}
+\seealso{
+  \href{help:rkwardplugins}{Introduction to Writing Plugins
+  for RKWard}
+}
+

Modified: trunk/rkward/packages/rkwarddev/man/rk.plugin.skeleton.Rd
===================================================================
--- trunk/rkward/packages/rkwarddev/man/rk.plugin.skeleton.Rd	2011-10-06 16:07:10 UTC (rev 3896)
+++ trunk/rkward/packages/rkwarddev/man/rk.plugin.skeleton.Rd	2011-10-06 21:30:28 UTC (rev 3897)
@@ -7,8 +7,9 @@
   NULL, provides = c("logic", "dialog"), dial.require =
   c(), overwrite = FALSE, tests = TRUE, lazyLoad = TRUE,
   menu = "test", results.header = NULL, JS.prep = NULL,
-  JS.calc = NULL, JS.prnt = NULL, summary = NULL, usage =
-  NULL, sections = NULL, settings = "scan", related = NULL,
+  JS.calc = NULL, JS.prnt = NULL, var.scan = TRUE,
+  saveobj.scan = TRUE, summary = NULL, usage = NULL,
+  sections = NULL, settings = "scan", related = NULL,
   technical = NULL, create = c("pmap", "xml", "js", "rkh",
   "desc"), edit = FALSE, load = FALSE, show = FALSE)
 }
@@ -82,9 +83,9 @@
   .js file. This string will be pasted as-is, see
   \code{\link[rkwarddev:rk.JS.doc]{rk.JS.doc}}.}
 
-  \item{JS.calc}{A character string with JavaScript code to
-  be included in the \code{calculate()} function of the .js
-  file. This string will be pasted as-is, see
+  \item{JS.calc}{Either a character string with JavaScript
+  code to be included in the \code{calculate()} function of
+  the .js file. This string will be pasted as-is, see
   \code{\link[rkwarddev:rk.JS.doc]{rk.JS.doc}}.}
 
   \item{JS.prnt}{A character string with JavaScript code to
@@ -92,6 +93,16 @@
   file. This string will be pasted as-is, see
   \code{\link[rkwarddev:rk.JS.doc]{rk.JS.doc}}.}
 
+  \item{var.scan}{Logical, if \code{TRUE}
+  \code{\link{rk.JS.scan}} will be called automatically to
+  to define the needed variables which will be added to the
+  code in the \code{calculate()} function.}
+
+  \item{saveobj.scan}{Logical, if \code{TRUE}
+  \code{\link{rk.JS.saveobj}} will be called automatically
+  to generate code to save R results which will be appended
+  to the code in the \code{printout()} function.}
+
   \item{summary}{An object of class \code{XiMpLe.node} to
   be pasted as the \code{<summary>} section of the help
   file. See

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