[rkward/rkwarddev-javascript] packages/rkwarddev: js() is working

m.eik michalke meik.michalke at uni-duesseldorf.de
Mon Oct 26 21:23:57 UTC 2015


Git commit 3faa838f12278456a386f88e2db4456b7c170058 by m.eik michalke.
Committed on 26/10/2015 at 21:20.
Pushed by meikm into branch 'rkwarddev-javascript'.

js() is working

  - inside a js() call it is now possible to use (even nested) if/else conditions for JavaScript code
  - the function replaces jo(), it also took over the operator transition

M  +3    -5    packages/rkwarddev/DESCRIPTION
M  +0    -1    packages/rkwarddev/NAMESPACE
M  +5    -1    packages/rkwarddev/R/ite.R
D  +0    -70   packages/rkwarddev/R/jo.R
M  +10   -32   packages/rkwarddev/R/js.R
M  +42   -5    packages/rkwarddev/R/rk-internal.R
M  +4    -2    packages/rkwarddev/man/ite.Rd
D  +0    -51   packages/rkwarddev/man/jo.Rd
M  +4    -2    packages/rkwarddev/man/js.Rd

http://commits.kde.org/rkward/3faa838f12278456a386f88e2db4456b7c170058

diff --git a/packages/rkwarddev/DESCRIPTION b/packages/rkwarddev/DESCRIPTION
index a7847b3..6fc4e98 100644
--- a/packages/rkwarddev/DESCRIPTION
+++ b/packages/rkwarddev/DESCRIPTION
@@ -8,14 +8,13 @@ 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-26
 Collate:
@@ -37,7 +36,6 @@ Collate:
     'i18n.R'
     'id.R'
     'ite.R'
-    'jo.R'
     'join.R'
     'js.R'
     'qp.R'
diff --git a/packages/rkwarddev/NAMESPACE b/packages/rkwarddev/NAMESPACE
index 4ea8d5b..e5a0ad7 100644
--- a/packages/rkwarddev/NAMESPACE
+++ b/packages/rkwarddev/NAMESPACE
@@ -5,7 +5,6 @@ export(echo)
 export(i18n)
 export(id)
 export(ite)
-export(jo)
 export(join)
 export(js)
 export(plugin2script)
diff --git a/packages/rkwarddev/R/ite.R b/packages/rkwarddev/R/ite.R
index 552cccd..05b4e8b 100644
--- a/packages/rkwarddev/R/ite.R
+++ b/packages/rkwarddev/R/ite.R
@@ -18,6 +18,9 @@
 
 #' Generate JavaScript if/then/else constructs
 #' 
+#' This function is very similar to \code{\link[base:ifelse]{ifelse}}. If you would like to use if conditions
+#' directly, have a look at the \code{\link[rkwarddev:js]{js}} wrapper instead.
+#' 
 #' @param 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).
 #' @param thenjs Either a character string, the code to be executed in case the \code{if()} statement is true,
@@ -27,7 +30,8 @@
 #' @param elsejs Like \code{thenjs}, the code to be executed in case the \code{if()} statement is not true.
 #' @return An object of class \code{rk.JS.ite}
 #' @include 00_class_03_rk.JS.ite.R
-#' @seealso \code{\link[rkwarddev:rk.paste.JS]{rk.paste.JS}},
+#' @seealso \code{\link[rkwarddev:js]{js}},
+#'    \code{\link[rkwarddev:rk.paste.JS]{rk.paste.JS}},
 #'    \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}},
diff --git a/packages/rkwarddev/R/jo.R b/packages/rkwarddev/R/jo.R
deleted file mode 100644
index 1438c8f..0000000
--- a/packages/rkwarddev/R/jo.R
+++ /dev/null
@@ -1,70 +0,0 @@
-# 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/>.
-
-
-#' Keep operators when replace XiMpLe.nodes with ID value
-#' 
-#' 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
-#' keep operators like \code{">="} or \code{"!="} unevaluated in the resulting output. This can be used to
-#' in "if" clauses, e.g. with \code{\link[rkwarddev:ite]{ite}}, so you don't have to quote half of it.
-#' 
-#' 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 will end up pasted in the result.
-#' 
-#' The abbreviation stands for "JavaScript operators".
-#' 
-#' 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 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!"))
-
-jo <- function(...){
-  full.content <- eval(substitute(alist(...)))
-  ID.content <- sapply(
-    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)){
-        # replace JS operators
-        return(do.call("replaceJSOperators", args=list(this.part)))
-      } else {
-        return(this.part)
-      }
-    }
-  )
-  return(id(js=TRUE, .objects=ID.content))
-}
diff --git a/packages/rkwarddev/R/js.R b/packages/rkwarddev/R/js.R
index 6416d75..ef336bc 100644
--- a/packages/rkwarddev/R/js.R
+++ b/packages/rkwarddev/R/js.R
@@ -34,6 +34,7 @@
 #' @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.
+#' @param level Integer value, first indetation level.
 #' @return A character string.
 #' @export
 #' @seealso \code{\link[rkwarddev:rk.JS.vars]{rk.JS.vars}},
@@ -45,9 +46,9 @@
 #' @examples
 #' # an example checkbox XML node
 #' cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
-#' ite(jo(cbox1 == "foo1"), echo("gotcha!"))
+#' rk.pasteJS(js(if(cbox1 == "foo1") { echo("gotcha!") }))
 
-js <- function(..., level=1, paste.id=TRUE){
+js <- function(..., level=2){
   full.content <- eval(substitute(alist(...)))
   ID.content <- lapply(
     full.content,
@@ -57,39 +58,16 @@ js <- function(..., level=1, paste.id=TRUE){
         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)))
-        }
+        # recursively check for if conditions
+        if(inherits(this.part, "if")){
+          this.part <- replaceJSIf(this.part, level=level)
+        } 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))
-  }
+  return(id(js=TRUE, .objects=ID.content))
 }
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index db713a4..9057937 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -1474,7 +1474,7 @@ replaceJSOperators <- function(..., call="id"){
           )
           return(paste0(unlist(result), collapse=""))
         } else {
-          return(thisItem)
+          return(eval(thisItem))
         }
       } else {
         return(thisItem)
@@ -1491,12 +1491,49 @@ replaceJSOperators <- function(..., call="id"){
 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))
+      cond <- do.call("js", args=list(cond[[2]], level=level))
     } else {
-    message("no {")
-      cond <- do.call("js", args=list(cond, level=level+1))
+      cond <- do.call("js", args=list(cond, level=level))
     }
   } else {}
   return(cond)
 } ## end function uncurl()
+
+
+## function replaceJSIf
+replaceJSIf <- function(cond, level=1, paste=TRUE){
+  if(inherits(cond, "if")){
+    # if condition -- should be save to give to js()
+    cond.if   <- do.call("js", args=list(cond[[2]], level=level))
+    # then do -- could be nested with another if condition
+    if(inherits(cond[[3]], "if")){
+      cond.then <- replaceJSIf(cond[[3]], level=level+1, paste=FALSE)
+    } else {
+      cond.then <- do.call("js", args=list(uncurl(cond[[3]], level=level+1), level=level))
+    }
+    # else do -- could be missing or yet another if condition
+    try.else <- NULL
+    cond.else <- tryCatch(
+      if(inherits(cond[[4]], "if")){
+        try.else <- replaceJSIf(cond[[4]], level=level+1, paste=FALSE)
+      } else {
+        try.else <- do.call("js", args=list(uncurl(cond[[4]], level=level+1), level=level))
+      },
+      error=function(e) NULL,
+      finally=try.else
+    )
+    iteObject <- ite(
+      ifjs=cond.if,
+      thenjs=cond.then,
+      elsejs=cond.else 
+    )
+    if(isTRUE(paste)){
+      return(rk.paste.JS(iteObject, level=level))
+    } else {
+      return(iteObject)
+    }
+  } else {
+    cond <- do.call("js", args=list(cond, level=level))
+    return(cond)
+  }
+}
diff --git a/packages/rkwarddev/man/ite.Rd b/packages/rkwarddev/man/ite.Rd
index 6de42a4..e1a80c7 100644
--- a/packages/rkwarddev/man/ite.Rd
+++ b/packages/rkwarddev/man/ite.Rd
@@ -23,7 +23,8 @@ You can also give another object of class \code{rk.JS.ite}.}
 An object of class \code{rk.JS.ite}
 }
 \description{
-Generate JavaScript if/then/else constructs
+This function is very similar to \code{\link[base:ifelse]{ifelse}}. If you would like to use if conditions
+directly, have a look at the \code{\link[rkwarddev:js]{js}} wrapper instead.
 }
 \examples{
 # first create an example checkbox XML node
@@ -32,7 +33,8 @@ cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
 ite(cbox1, echo("bar <- \\"", cbox1, "\\""), echo("bar <- NULL"))
 }
 \seealso{
-\code{\link[rkwarddev:rk.paste.JS]{rk.paste.JS}},
+\code{\link[rkwarddev:js]{js}},
+   \code{\link[rkwarddev:rk.paste.JS]{rk.paste.JS}},
    \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}},
diff --git a/packages/rkwarddev/man/jo.Rd b/packages/rkwarddev/man/jo.Rd
deleted file mode 100644
index 8fa3673..0000000
--- a/packages/rkwarddev/man/jo.Rd
+++ /dev/null
@@ -1,51 +0,0 @@
-% Generated by roxygen2 (4.1.1): do not edit by hand
-% Please edit documentation in R/jo.R
-\name{jo}
-\alias{jo}
-\title{Keep operators when replace XiMpLe.nodes with ID value}
-\usage{
-jo(...)
-}
-\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 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
-keep operators like \code{">="} or \code{"!="} unevaluated in the resulting output. This can be used to
-in "if" clauses, e.g. with \code{\link[rkwarddev:ite]{ite}},
-      so you don't have to quote half of it.
-}
-\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 will end up pasted in the result.
-
-The abbreviation stands for "JavaScript operators".
-
-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/js.Rd b/packages/rkwarddev/man/js.Rd
index 769ace8..7500001 100644
--- a/packages/rkwarddev/man/js.Rd
+++ b/packages/rkwarddev/man/js.Rd
@@ -4,12 +4,14 @@
 \alias{js}
 \title{R to JavaScript translation}
 \usage{
-js(...)
+js(..., level = 2)
 }
 \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.}
+
+\item{level}{Integer value, first indetation level.}
 }
 \value{
 A character string.
@@ -36,7 +38,7 @@ 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")
-ite(jo(cbox1 == "foo1"), echo("gotcha!"))
+rk.pasteJS(js(if(cbox1 == "foo1") { echo("gotcha!") }))
 }
 \seealso{
 \code{\link[rkwarddev:rk.JS.vars]{rk.JS.vars}},



More information about the rkward-tracker mailing list