[rkward] packages/rkwarddev: fixed JS for loops

m.eik michalke meik.michalke at uni-duesseldorf.de
Wed Oct 28 15:26:02 UTC 2015


Git commit 5bf1d197fb0d49b3eae9e234800505da56094593 by m.eik michalke.
Committed on 28/10/2015 at 15:26.
Pushed by meikm into branch 'master'.

fixed JS for loops

  - collapse vectors using commas, R vectors won't work so well in JS code
  - improved and fixed the docs for js() a bit

M  +3    -4    packages/rkwarddev/ChangeLog
M  +1    -1    packages/rkwarddev/DESCRIPTION
M  +19   -3    packages/rkwarddev/R/js.R
M  +7    -3    packages/rkwarddev/R/rk-internal.R
M  +1    -1    packages/rkwarddev/R/rkwarddev-package.R
M  +3    -4    packages/rkwarddev/inst/NEWS.Rd
M  +21   -3    packages/rkwarddev/man/js.Rd
M  +1    -1    packages/rkwarddev/man/rkwarddev-package.Rd

http://commits.kde.org/rkward/5bf1d197fb0d49b3eae9e234800505da56094593

diff --git a/packages/rkwarddev/ChangeLog b/packages/rkwarddev/ChangeLog
index 48169c1..b5bc8d1 100644
--- a/packages/rkwarddev/ChangeLog
+++ b/packages/rkwarddev/ChangeLog
@@ -1,6 +1,6 @@
 ChangeLog for package rkwarddev
 
-changes in version 0.07-4 (2015-10-27)
+changes in version 0.07-4 (2015-10-28)
 unreleased:
   - this version is under development
 fixed:
@@ -18,9 +18,8 @@ added:
   - new option "ignore" enables rk.uniqueIDs() to not check nodes for
     duplicates (e.g., <copy>)
   - new wrapper function js() for id() makes it possible to use several
-    operators without quoting, preserve "if" conditions and "for" loops,
-    which all will be translated to JavaScript equivalents without the
-    need for quoting
+    operators without quoting, preserve "if" conditions and "for" loops, which all
+    will be translated to JavaScript equivalents without the need for quoting
   - new option ".objects" in id() to provide objects as a list
   - new option "rk.JS.vars" to make JS variables match the name of the
     original R object
diff --git a/packages/rkwarddev/DESCRIPTION b/packages/rkwarddev/DESCRIPTION
index d929a87..18ec091 100644
--- a/packages/rkwarddev/DESCRIPTION
+++ b/packages/rkwarddev/DESCRIPTION
@@ -17,7 +17,7 @@ URL: https://rkward.kde.org
 Authors at R: c(person(given="m.eik", family="michalke",
         email="meik.michalke at hhu.de", role=c("aut", "cre")))
 Version: 0.07-4
-Date: 2015-10-27
+Date: 2015-10-28
 Collate:
     '00_class_01_rk.JS.arr.R'
     '00_class_02_rk.JS.var.R'
diff --git a/packages/rkwarddev/R/js.R b/packages/rkwarddev/R/js.R
index 87445b9..e2e3584 100644
--- a/packages/rkwarddev/R/js.R
+++ b/packages/rkwarddev/R/js.R
@@ -29,8 +29,9 @@
 #' 
 #' Using \code{for} loops is a bit more delicate, as they are very differently constructed in JavaScript. As
 #' a workaround, \code{js} will define an array and a counter variable with randomly generated names, fill
-#' the array with the values you provided and iterate through the array. It will keep the iterator variable
-#' name you used in the original R loop, so you can use it inside the loop body.
+#' the array with the values you provided and iterate through the array. In order to keep the iterator variable
+#' name you used in the original R loop, so you can use it inside the loop body, you will have to define it before
+#' the \code{js} call with a substitution of itself (see examples). Otherwise, you will get an "object not found" error.
 #' 
 #' The following operators are supported: +, -, *, /, ==, !=, >, <, >=, <=, || and &&
 #' 
@@ -51,7 +52,22 @@
 #' @examples
 #' # an example checkbox XML node
 #' cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
-#' rk.pasteJS(js(if(cbox1 == "foo1") { echo("gotcha!") }))
+#' cat(rk.paste.JS(js(
+#'   if(cbox1 == "foo1") {
+#'     echo("gotcha!")
+#'   } else {
+#'     echo("nothing!")
+#'   }
+#' )))
+#' 
+#' # let's try preserving a for loop
+#' # to use iterator variable i, we must initialize it first
+#' i <- substitute(i)  # DON'T FORGET THIS!
+#' cat(rk.paste.JS(js(
+#'   for (i in 1:10) {
+#'     echo(i)
+#'   }
+#' )))
 
 js <- function(..., level=2){
   full.content <- eval(substitute(alist(...)))
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index 21927ba..5059172 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -1474,7 +1474,12 @@ replaceJSOperators <- function(..., call="id"){
           )
           return(paste0(unlist(result), collapse=""))
         } else {
-          return(eval(thisItem))
+          thisItem <- eval(thisItem)
+          # R vectors don't make much sense, collapse them for JS
+          if(is.vector(thisItem)){
+            thisItem <- paste0(thisItem, collapse=", ")
+          } else {}
+          return(thisItem)
         }
       } else {
         return(thisItem)
@@ -1560,10 +1565,9 @@ replaceJSFor <- function(loop, level=1){
     arrayName <- paste0("a", paste0(sample(c(letters,LETTERS,0:9), 5, replace=TRUE), collapse=""))
     iterName <- paste0("i", paste0(sample(c(letters,LETTERS,0:9), 5, replace=TRUE), collapse=""))
     loop <- paste(
-      paste0(paste0(rep("\t", level-1), collapse=""), "// the following array variable has a randomly generated name"),
+      paste0(paste0(rep("\t", level-1), collapse=""), "// the variable names \"", arrayName, "\" and \"", iterName, "\" were randomly generated"),
       paste0("var ", arrayName, " = new Array();"),
       paste0(arrayName, ".push(", do.call("js", args=list(loop[[3]], level=level)), ");"),
-      "// the counter variable also has a randomly generated name",
       paste0("for (var ", as.character(loop[[2]]), "=", arrayName, "[0], ", iterName, "=0; ",
         iterName, " < ", arrayName, ".length; ",
         iterName, "++, ", as.character(loop[[2]]), "=", arrayName, "[", iterName, "]) {"),
diff --git a/packages/rkwarddev/R/rkwarddev-package.R b/packages/rkwarddev/R/rkwarddev-package.R
index 69008bb..f84f6f6 100644
--- a/packages/rkwarddev/R/rkwarddev-package.R
+++ b/packages/rkwarddev/R/rkwarddev-package.R
@@ -4,7 +4,7 @@
 #' Package: \tab rkwarddev\cr
 #' Type: \tab Package\cr
 #' Version: \tab 0.07-4\cr
-#' Date: \tab 2015-10-27\cr
+#' Date: \tab 2015-10-28\cr
 #' Depends: \tab R (>= 2.9.0),methods,XiMpLe (>= 0.03-21),rkward (>= 0.5.7)\cr
 #' Enhances: \tab rkward\cr
 #' Encoding: \tab UTF-8\cr
diff --git a/packages/rkwarddev/inst/NEWS.Rd b/packages/rkwarddev/inst/NEWS.Rd
index 8dc2d91..91ed8d4 100644
--- a/packages/rkwarddev/inst/NEWS.Rd
+++ b/packages/rkwarddev/inst/NEWS.Rd
@@ -1,7 +1,7 @@
 \name{NEWS}
 \title{News for Package 'rkwarddev'}
 \encoding{UTF-8}
-\section{Changes in rkwarddev version 0.07-4 (2015-10-27)}{
+\section{Changes in rkwarddev version 0.07-4 (2015-10-28)}{
   \subsection{unreleased}{
     \itemize{
       \item this version is under development
@@ -26,9 +26,8 @@
       \item new option \code{"ignore"} enables \code{rk.uniqueIDs()} to not check nodes for
         duplicates (e.g., <copy>)
       \item new wrapper function \code{js()} for \code{id()} makes it possible to use several
-        operators without quoting, preserve \code{"if"} conditions and \code{"for"} loops,
-        which all will be translated to JavaScript equivalents without the
-        need for quoting
+        operators without quoting, preserve \code{"if"} conditions and \code{"for"} loops, which all
+        will be translated to JavaScript equivalents without the need for quoting
       \item new option \code{".objects"} in \code{id()} to provide objects as a list
       \item new option \code{"rk.JS.vars"} to make JS variables match the name of the
         original R object
diff --git a/packages/rkwarddev/man/js.Rd b/packages/rkwarddev/man/js.Rd
index f0e3122..7a27ce3 100644
--- a/packages/rkwarddev/man/js.Rd
+++ b/packages/rkwarddev/man/js.Rd
@@ -34,8 +34,11 @@ Using \code{for} loops is a bit more delicate,
       as they are very differently constructed in JavaScript. As
 a workaround,
       \code{js} will define an array and a counter variable with randomly generated names, fill
-the array with the values you provided and iterate through the array. It will keep the iterator variable
-name you used in the original R loop, so you can use it inside the loop body.
+the array with the values you provided and iterate through the array. In order to keep the iterator variable
+name you used in the original R loop, so you can use it inside the loop body,
+      you will have to define it before
+the \code{js} call with a substitution of itself (see examples). Otherwise,
+      you will get an "object not found" error.
 
 The following operators are supported: +, -, *, /, ==, !=, >, <, >=, <=, || and &&
 
@@ -45,7 +48,22 @@ These are currently unsupported and still need to be quoted: \%, ++, --, =, +=,
 \examples{
 # an example checkbox XML node
 cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
-rk.pasteJS(js(if(cbox1 == "foo1") { echo("gotcha!") }))
+cat(rk.paste.JS(js(
+  if(cbox1 == "foo1") {
+    echo("gotcha!")
+  } else {
+    echo("nothing!")
+  }
+)))
+
+# let's try preserving a for loop
+# to use iterator variable i, we must initialize it first
+i <- substitute(i)  # DON'T FORGET THIS!
+cat(rk.paste.JS(js(
+  for (i in 1:10) {
+    echo(i)
+  }
+)))
 }
 \seealso{
 \code{\link[rkwarddev:rk.JS.vars]{rk.JS.vars}},
diff --git a/packages/rkwarddev/man/rkwarddev-package.Rd b/packages/rkwarddev/man/rkwarddev-package.Rd
index 30f7f77..7921bf7 100644
--- a/packages/rkwarddev/man/rkwarddev-package.Rd
+++ b/packages/rkwarddev/man/rkwarddev-package.Rd
@@ -12,7 +12,7 @@ A Collection of Tools for RKWard Plugin Development.
 Package: \tab rkwarddev\cr
 Type: \tab Package\cr
 Version: \tab 0.07-4\cr
-Date: \tab 2015-10-27\cr
+Date: \tab 2015-10-28\cr
 Depends: \tab R (>= 2.9.0),methods,XiMpLe (>= 0.03-21),rkward (>= 0.5.7)\cr
 Enhances: \tab rkward\cr
 Encoding: \tab UTF-8\cr



More information about the rkward-tracker mailing list