[rkward-cvs] SF.net SVN: rkward:[3728] trunk/rkward/rkward/rbackend/rpackages/rkwarddev

m-eik at users.sourceforge.net m-eik at users.sourceforge.net
Mon Sep 5 16:41:17 UTC 2011


Revision: 3728
          http://rkward.svn.sourceforge.net/rkward/?rev=3728&view=rev
Author:   m-eik
Date:     2011-09-05 16:41:17 +0000 (Mon, 05 Sep 2011)
Log Message:
-----------
rkwarddev: added rk.JS.array()

Modified Paths:
--------------
    trunk/rkward/rkward/rbackend/rpackages/rkwarddev/ChangeLog
    trunk/rkward/rkward/rbackend/rpackages/rkwarddev/DESCRIPTION
    trunk/rkward/rkward/rbackend/rpackages/rkwarddev/NAMESPACE
    trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk-internal.R
    trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk.JS.scan.R

Added Paths:
-----------
    trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk.JS.array.R
    trunk/rkward/rkward/rbackend/rpackages/rkwarddev/man/rk.JS.array.Rd

Modified: trunk/rkward/rkward/rbackend/rpackages/rkwarddev/ChangeLog
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkwarddev/ChangeLog	2011-09-05 12:21:50 UTC (rev 3727)
+++ trunk/rkward/rkward/rbackend/rpackages/rkwarddev/ChangeLog	2011-09-05 16:41:17 UTC (rev 3728)
@@ -1,7 +1,7 @@
 ChangeLog for package rkwardplugdev
 
 ## 0.01-1 (2011-09-05)
-  - added rk.JS.scan(), rk.JS.doc(), rk.rkh.scan() and rk.rkh.doc()
+  - added rk.JS.scan(), rk.JS.doc(), rk.JS.array(), rk.rkh.scan() and rk.rkh.doc()
   - shortened package name from "rkwardplugdev" to "rkwarddev"
   - merged code into RKWard's svn tree
 

Modified: trunk/rkward/rkward/rbackend/rpackages/rkwarddev/DESCRIPTION
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkwarddev/DESCRIPTION	2011-09-05 12:21:50 UTC (rev 3727)
+++ trunk/rkward/rkward/rbackend/rpackages/rkwarddev/DESCRIPTION	2011-09-05 16:41:17 UTC (rev 3728)
@@ -19,6 +19,7 @@
 Date: 2011-09-05
 Collate:
     'rk-internal.R'
+    'rk.JS.array.R'
     'rk.JS.doc.R'
     'rk.JS.scan.R'
     'rk.plugin.skeleton.R'

Modified: trunk/rkward/rkward/rbackend/rpackages/rkwarddev/NAMESPACE
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkwarddev/NAMESPACE	2011-09-05 12:21:50 UTC (rev 3727)
+++ trunk/rkward/rkward/rbackend/rpackages/rkwarddev/NAMESPACE	2011-09-05 16:41:17 UTC (rev 3728)
@@ -1,3 +1,4 @@
+export(rk.JS.array)
 export(rk.JS.doc)
 export(rk.JS.scan)
 export(rk.plugin.skeleton)

Modified: trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk-internal.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk-internal.R	2011-09-05 12:21:50 UTC (rev 3727)
+++ trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk-internal.R	2011-09-05 16:41:17 UTC (rev 3728)
@@ -88,9 +88,9 @@
 # (except for the first one) to upper case
 camelCode <- function(words){
 
-	words <- as.vector(sapply(words, function(cur.word){
+	words <- as.vector(unlist(sapply(words, function(cur.word){
 			unlist(strsplit(cur.word, split="\\."))
-		}))
+		})))
 
 	new.words <- sapply(words[-1], function(cur.word){
 		word.vector <- unlist(strsplit(cur.word, split=""))
@@ -103,3 +103,16 @@
 
 	return(results)
 } ## end function camelCode()
+
+## function get.JS.vars()
+#   <tag id="my.id" ...>
+# in XML will become
+#   var my.id = getValue("my.id");
+get.JS.vars <- function(JS.var, XML.var=NULL, JS.prefix="", indent.by="", names.only=FALSE){
+	if(isTRUE(names.only)){
+		results <- camelCode(c(JS.prefix, JS.var))
+	} else {
+		results <- paste(indent.by, "var ", camelCode(c(JS.prefix, JS.var)), " = getValue(\"", XML.var, "\");\n", sep="")
+	}
+	return(results)
+} ## function get.JS.vars()

Added: trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk.JS.array.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk.JS.array.R	                        (rev 0)
+++ trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk.JS.array.R	2011-09-05 16:41:17 UTC (rev 3728)
@@ -0,0 +1,57 @@
+#' Create a simple JavaScript array
+#'
+#' If you need to combine multiple options (like values of several checkboxes) into one vector or list,
+#' this function can help with that task. All relevant variables will become part of an array and
+#' then joined into the desired argument type.
+#'
+#' @param option A character string, naming the option of, e.g., an R function which should be
+#'		constructed from several variables.
+#' @param variables A character vector, the names of the variables to combine to a vector or list.
+#' @param list Logical, if \code{TRUE} the option will be constructed by \code{list()},
+#'		otherwise by \code{c()}.
+#' @param def.vars Logical, if \code{TRUE} the provided variables will also be defined.
+#' @param var.prefix A character string. If \code{def.vars=TRUE}, this string will be used as a prefix
+#'		for the JS variable names.
+#' @param indent.by A character string defining how indentation should be done.
+#' @return A character string.
+#' @export
+#' @examples
+#' cat(rk.JS.array("my.option", variables=c("frst.var", "scnd.var")))
+
+rk.JS.array <- function(option, variables=NULL, list=FALSE, def.vars=TRUE, var.prefix="chc", indent.by="\t"){
+	arr.name <- camelCode(c("arr", option))
+	opt.name <- camelCode(c("opt", option))
+
+	if(isTRUE(def.vars)){
+		JS.vars <- paste(unlist(sapply(variables, function(this.var){get.JS.vars(
+							JS.var=this.var,
+							XML.var=this.var,
+							JS.prefix=var.prefix,
+							indent.by=indent.by)
+					})), collapse="")
+	} else {
+		JS.vars <- ""
+	}
+
+	JS.array <- paste(
+		indent(2, by=indent.by), "var ", arr.name, " = new Array();\n",
+		indent(2, by=indent.by), arr.name, ".push(",
+		paste(unlist(sapply(variables, function(this.var){get.JS.vars(
+							JS.var=this.var,
+							JS.prefix=var.prefix,
+							names.only=TRUE)
+					})), collapse=", "), ");\n",
+		indent(2, by=indent.by), arr.name, " = ", arr.name, ".filter(String);\n",
+		indent(2, by=indent.by), "if(", arr.name, ".length > 0) {\n",
+		indent(3, by=indent.by), "var ", opt.name, " = \", ", option,"=",
+			ifelse(isTRUE(list), "list(", "c("),
+			"\" + ", arr.name, ".join(\", \") + \")\";\n",
+		indent(2, by=indent.by), "} else {\n",
+		indent(3, by=indent.by), "var ", opt.name, " = \"\";\n",
+		indent(2, by=indent.by), "}\n",
+		sep="")
+
+	results <- paste(JS.vars, JS.array, sep="\n")
+
+	return(results)
+}

Modified: trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk.JS.scan.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk.JS.scan.R	2011-09-05 12:21:50 UTC (rev 3727)
+++ trunk/rkward/rkward/rbackend/rpackages/rkwarddev/R/rk.JS.scan.R	2011-09-05 16:41:17 UTC (rev 3728)
@@ -26,7 +26,10 @@
 			# will become
 			#   var my.id = getValue("my.id");
 			JS.lines <- paste(unlist(sapply(1:nrow(JS.id), function(this.id){
-					return(paste(indent.by, "var ", camelCode(JS.id[this.id,"abbrev"]), " = getValue(\"", JS.id[this.id,"id"], "\");\n", sep=""))
+					return(get.JS.vars(
+						JS.var=JS.id[this.id,"abbrev"],
+						XML.var=JS.id[this.id,"id"],
+						indent.by=indent.by))
 				})), collapse="")
 		} else {
 			JS.lines <- JS.id[,"id"]

Added: trunk/rkward/rkward/rbackend/rpackages/rkwarddev/man/rk.JS.array.Rd
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkwarddev/man/rk.JS.array.Rd	                        (rev 0)
+++ trunk/rkward/rkward/rbackend/rpackages/rkwarddev/man/rk.JS.array.Rd	2011-09-05 16:41:17 UTC (rev 3728)
@@ -0,0 +1,42 @@
+\name{rk.JS.array}
+\alias{rk.JS.array}
+\title{Create a simple JavaScript array}
+\usage{
+  rk.JS.array(option, variables = NULL, list = FALSE,
+  def.vars = TRUE, var.prefix = "chc", indent.by = "\t")
+}
+\arguments{
+  \item{option}{A character string, naming the option of,
+  e.g., an R function which should be constructed from
+  several variables.}
+
+  \item{variables}{A character vector, the names of the
+  variables to combine to a vector or list.}
+
+  \item{list}{Logical, if \code{TRUE} the option will be
+  constructed by \code{list()}, otherwise by \code{c()}.}
+
+  \item{def.vars}{Logical, if \code{TRUE} the provided
+  variables will also be defined.}
+
+  \item{var.prefix}{A character string. If
+  \code{def.vars=TRUE}, this string will be used as a
+  prefix for the JS variable names.}
+
+  \item{indent.by}{A character string defining how
+  indentation should be done.}
+}
+\value{
+  A character string.
+}
+\description{
+  If you need to combine multiple options (like values of
+  several checkboxes) into one vector or list, this
+  function can help with that task. All relevant variables
+  will become part of an array and then joined into the
+  desired argument type.
+}
+\examples{
+cat(rk.JS.array("my.option", variables=c("frst.var", "scnd.var")))
+}
+

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