[rkward/rkwarddev-javascript] packages/rkwarddev: added function js() (to replace jo() later)

m.eik michalke meik.michalke at uni-duesseldorf.de
Mon Oct 26 15:22:06 UTC 2015


Git commit 5a880e04c4167ac783ff32795071bce10b77bfe4 by m.eik michalke.
Committed on 26/10/2015 at 15:14.
Pushed by meikm into branch 'rkwarddev-javascript'.

added function js() (to replace jo() later)

  - next to the operators, trying also to translate R's if conditions directly into JavaScript code.
    this is basically working here, but needs some more tweaks to get recursion right for nested statements,
    as well as loping through all statements in curly brackets, not just the first one.

M  +3    -2    packages/rkwarddev/ChangeLog
M  +6    -4    packages/rkwarddev/DESCRIPTION
M  +1    -0    packages/rkwarddev/NAMESPACE
A  +95   -0    packages/rkwarddev/R/js.R
M  +17   -0    packages/rkwarddev/R/rk-internal.R
M  +1    -1    packages/rkwarddev/R/rkwarddev-package.R
M  +3    -2    packages/rkwarddev/inst/NEWS.Rd
M  +1    -1    packages/rkwarddev/man/ite.Rd
A  +49   -0    packages/rkwarddev/man/js.Rd
M  +1    -1    packages/rkwarddev/man/rkwarddev-package.Rd
M  +1    -1    packages/rkwarddev/man/tf.Rd

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

diff --git a/packages/rkwarddev/ChangeLog b/packages/rkwarddev/ChangeLog
index 41cad86..e26f7f7 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-24)
+changes in version 0.07-4 (2015-10-26)
 unreleased:
   - this version is under development
 fixed:
@@ -20,7 +20,8 @@ added:
   - new wrapper function jo() for id() makes it possible to use several
     operators without quoting, it will detect and quote them for you
   - new option ".objects" in id() to provide objects as a list
-  - new option "rk.JS.vars" to maje JS varaibles match the name of the original R object
+  - new option "rk.JS.vars" to maje JS varaibles match the name of the
+    original R object
 changed:
   - improved error handling in rk.JS.header(), error messages are more
     informative now
diff --git a/packages/rkwarddev/DESCRIPTION b/packages/rkwarddev/DESCRIPTION
index 5adbc82..a7847b3 100644
--- a/packages/rkwarddev/DESCRIPTION
+++ b/packages/rkwarddev/DESCRIPTION
@@ -8,15 +8,16 @@ Depends:
 Suggests:
     testthat
 Enhances: rkward
-Description: Provides functions to create plugin skeletons and XML structures
-    for RKWard.
+Description: Provides functions to create plugin skeletons and XML
+    structures for RKWard.
 License: GPL (>= 3)
 Encoding: UTF-8
 LazyLoad: yes
 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")))
+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-24
+Date: 2015-10-26
 Collate:
     '00_class_01_rk.JS.arr.R'
     '00_class_02_rk.JS.var.R'
@@ -38,6 +39,7 @@ Collate:
     'ite.R'
     'jo.R'
     'join.R'
+    'js.R'
     'qp.R'
     'rk.comment.R'
     'rk-internal.R'
diff --git a/packages/rkwarddev/NAMESPACE b/packages/rkwarddev/NAMESPACE
index 5ddc0da..4ea8d5b 100644
--- a/packages/rkwarddev/NAMESPACE
+++ b/packages/rkwarddev/NAMESPACE
@@ -7,6 +7,7 @@ export(id)
 export(ite)
 export(jo)
 export(join)
+export(js)
 export(plugin2script)
 export(qp)
 export(rk.JS.array)
diff --git a/packages/rkwarddev/R/js.R b/packages/rkwarddev/R/js.R
new file mode 100644
index 0000000..6416d75
--- /dev/null
+++ b/packages/rkwarddev/R/js.R
@@ -0,0 +1,95 @@
+# Copyright 2010-2014 Meik Michalke <meik.michalke at hhu.de>
+#
+# This file is part of the R package rkwarddev.
+#
+# rkwarddev is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# rkwarddev is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with rkwarddev.  If not, see <http://www.gnu.org/licenses/>.
+
+
+#' R to JavaScript translation
+#' 
+#' This function is a wrapper for \code{\link[rkwarddev:id]{id}} similar to \code{\link[rkwarddev:qp]{qp}}
+#' that uses \code{eval(substitute(alist(...)))} to preserve the value of \code{...} as-is to be able to
+#' both keep operators like \code{">="} or \code{"!="} unevaluated in the resulting output, as well as translating
+#' \code{if/else} clauses from R to JavaScript.
+#' 
+#' Normally, \code{id} would simply evaluate the condition and then return the result of that evaluation, which
+#' most of the time is not what you want. With this function, you can test conditions in usual R syntax, yet
+#' the operators and \code{if/else} clauses will end up pasted in the result.
+#' 
+#' The following operators are supported: +, -, *, /, ==, !=, >, <, >=, <=, || and &&
+#' 
+#' These are currently unsupported and still need to be quoted: \%, ++, --, =, +=, -=, *=, /=, \%=, ===, !== and !
+#'
+#' @param ... One or several character strings and/or \code{XiMpLe.node} objects with plugin nodes,
+#'   and/or objects of classes \code{rk.JS.arr} or \code{rk.JS.opt}, simply separated by comma.
+#'   JavaScript operators and \code{if} conditions will be kept as-is.
+#' @return A character string.
+#' @export
+#' @seealso \code{\link[rkwarddev:rk.JS.vars]{rk.JS.vars}},
+#'    \code{\link[rkwarddev:rk.JS.array]{rk.JS.array}},
+#'    \code{\link[rkwarddev:rk.JS.options]{rk.JS.options}},
+#'    \code{\link[rkwarddev:echo]{echo}},
+#'    \code{\link[rkwarddev:id]{id}},
+#'    and the \href{help:rkwardplugins}{Introduction to Writing Plugins for RKWard}
+#' @examples
+#' # an example checkbox XML node
+#' cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
+#' ite(jo(cbox1 == "foo1"), echo("gotcha!"))
+
+js <- function(..., level=1, paste.id=TRUE){
+  full.content <- eval(substitute(alist(...)))
+  ID.content <- lapply(
+    full.content,
+    function(this.part){
+      # get the object, not just a name from eval(substitute(alist(...)))
+      if (is.name(this.part)){
+        this.part <- eval(this.part)
+      } else {}
+      if(is.call(this.part)){
+        message(paste0("call: ", this.part))
+        if(inherits(full.content[[1]], "if")){
+          # the if condition
+          cond.if   <- do.call("js", args=list(full.content[[1]][[2]], level=level+1))
+          cond.then <- do.call("js", args=list(uncurl(full.content[[1]][[3]]), level=level+1, paste.id=FALSE))
+          try.else <- NULL
+          cond.else <- tryCatch(
+            try.else <- do.call("js", args=list(uncurl(full.content[[1]][[4]]), level=level+1, paste.id=FALSE)),
+            error=function(e) NULL,
+            finally=try.else
+          )
+          iteObject <- ite(
+            ifjs=cond.if,
+            thenjs=cond.then,
+            elsejs=cond.else 
+          )
+          if(isTRUE(paste.id)){
+            return(rk.paste.JS(iteObject, level=level))
+          } else {
+            return(iteObject)
+          }
+        } else {
+          # replace JS operators
+          return(do.call("replaceJSOperators", args=list(this.part)))
+        }
+      } else {
+        return(this.part)
+      }
+    }
+  )
+  if(isTRUE(paste.id)){
+    return(id(js=TRUE, .objects=ID.content))
+  } else {
+    return(unlist(ID.content))
+  }
+}
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index 91cefe3..db713a4 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -1483,3 +1483,20 @@ replaceJSOperators <- function(..., call="id"){
   )
   return(unlist(dotlist))
 } ## end function replaceJSOperators
+
+
+## function uncurl()
+# used by js() to fetch calls from then/else segments of if conditions,
+# omitting curly brackets that would get in the way with ite()
+uncurl <- function(cond, level=1){
+  if(!is.null(cond)){
+    if(identical(as.character(cond[[1]]), "{")){
+    message("{")
+      cond <- do.call("js", args=list(cond[[2]], level=level+1))
+    } else {
+    message("no {")
+      cond <- do.call("js", args=list(cond, level=level+1))
+    }
+  } else {}
+  return(cond)
+} ## end function uncurl()
diff --git a/packages/rkwarddev/R/rkwarddev-package.R b/packages/rkwarddev/R/rkwarddev-package.R
index 08fa47a..1da2349 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-24\cr
+#' Date: \tab 2015-10-26\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 f715a29..8370b76 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-24)}{
+\section{Changes in rkwarddev version 0.07-4 (2015-10-26)}{
   \subsection{unreleased}{
     \itemize{
       \item this version is under development
@@ -28,7 +28,8 @@
       \item new wrapper function \code{jo()} for \code{id()} makes it possible to use several
         operators without quoting, it will detect and quote them for you
       \item new option \code{".objects"} in \code{id()} to provide objects as a list
-      \item new option \code{"rk.JS.vars"} to maje JS varaibles match the name of the original R object
+      \item new option \code{"rk.JS.vars"} to maje JS varaibles match the name of the
+        original R object
     }
   }
   \subsection{changed}{
diff --git a/packages/rkwarddev/man/ite.Rd b/packages/rkwarddev/man/ite.Rd
index 79dd9ad..6de42a4 100644
--- a/packages/rkwarddev/man/ite.Rd
+++ b/packages/rkwarddev/man/ite.Rd
@@ -7,7 +7,7 @@
 ite(ifjs, thenjs, elsejs = NULL)
 }
 \arguments{
-\item{ifjs}{Either a character string to be placed in the brackets if an \code{if()} statement,
+\item{ifjs}{Either a character string to be placed in the brackets of an \code{if()} statement,
 or an object of class \code{XiMpLe.node}. \code{rk.JS.arr} or \code{rk.JS.opt} (whose identifier will be used).}
 
 \item{thenjs}{Either a character string,
diff --git a/packages/rkwarddev/man/js.Rd b/packages/rkwarddev/man/js.Rd
new file mode 100644
index 0000000..769ace8
--- /dev/null
+++ b/packages/rkwarddev/man/js.Rd
@@ -0,0 +1,49 @@
+% Generated by roxygen2 (4.1.1): do not edit by hand
+% Please edit documentation in R/js.R
+\name{js}
+\alias{js}
+\title{R to JavaScript translation}
+\usage{
+js(...)
+}
+\arguments{
+\item{...}{One or several character strings and/or \code{XiMpLe.node} objects with plugin nodes,
+and/or objects of classes \code{rk.JS.arr} or \code{rk.JS.opt}, simply separated by comma.
+JavaScript operators and \code{if} conditions will be kept as-is.}
+}
+\value{
+A character string.
+}
+\description{
+This function is a wrapper for \code{\link[rkwarddev:id]{id}} similar to \code{\link[rkwarddev:qp]{qp}}
+that uses \code{eval(substitute(alist(...)))} to preserve the value of \code{...} as-is to be able to
+both keep operators like \code{">="} or \code{"!="} unevaluated in the resulting output,
+      as well as translating
+\code{if/else} clauses from R to JavaScript.
+}
+\details{
+Normally,
+      \code{id} would simply evaluate the condition and then return the result of that evaluation, which
+most of the time is not what you want. With this function,
+      you can test conditions in usual R syntax, yet
+the operators and \code{if/else} clauses will end up pasted in the result.
+
+The following operators are supported: +, -, *, /, ==, !=, >, <, >=, <=, || and &&
+
+These are currently unsupported and still need to be quoted: \%, ++, --, =, +=, -=, *=,
+      /=, \%=, ===, !== and !
+}
+\examples{
+# an example checkbox XML node
+cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
+ite(jo(cbox1 == "foo1"), echo("gotcha!"))
+}
+\seealso{
+\code{\link[rkwarddev:rk.JS.vars]{rk.JS.vars}},
+   \code{\link[rkwarddev:rk.JS.array]{rk.JS.array}},
+   \code{\link[rkwarddev:rk.JS.options]{rk.JS.options}},
+   \code{\link[rkwarddev:echo]{echo}},
+   \code{\link[rkwarddev:id]{id}},
+   and the \href{help:rkwardplugins}{Introduction to Writing Plugins for RKWard}
+}
+
diff --git a/packages/rkwarddev/man/rkwarddev-package.Rd b/packages/rkwarddev/man/rkwarddev-package.Rd
index d24920f..2140e27 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-24\cr
+Date: \tab 2015-10-26\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/man/tf.Rd b/packages/rkwarddev/man/tf.Rd
index 01bd6ff..0fa6e00 100644
--- a/packages/rkwarddev/man/tf.Rd
+++ b/packages/rkwarddev/man/tf.Rd
@@ -34,7 +34,7 @@ E.g.,
       if \code{false=FALSE} then the option will be set to \code{FALSE} if the box is not checked,
 or in case \code{not=TRUE}, if the box is checked.}
 
-\item{opt}{A character string, naming the R option o be set. If \code{NULL},
+\item{opt}{A character string, naming the R option to be set. If \code{NULL},
       the XML ID of the checkbox node
 will be used.}
 



More information about the rkward-tracker mailing list