[rkward] packages/rkwarddev: added funtion jo() to keep some javascript operators

m.eik michalke meik.michalke at uni-duesseldorf.de
Sat Oct 24 12:43:25 UTC 2015


Git commit bd1a3ff12140743afc28012172d2fcfb0b47d893 by m.eik michalke.
Committed on 24/10/2015 at 12:35.
Pushed by meikm into branch 'master'.

added funtion jo() to keep some javascript operators

  - the previous attempt was kind of troublesome, so i reverted some of the changes and moved them to a designated new function
  - the new function is called jo() (for "javascript operators") and can be used instead of id() to use some operators without quoting
  - there are some JS operators that aren't supported yet (like "++"). if i can figure out how to define them as dummy R operators first, that should change
  - things like "=" might never be possible, though

M  +4    -4    packages/rkwarddev/ChangeLog
M  +2    -1    packages/rkwarddev/DESCRIPTION
M  +1    -0    packages/rkwarddev/NAMESPACE
M  +2    -13   packages/rkwarddev/R/id.R
M  +2    -2    packages/rkwarddev/R/ite.R
A  +70   -0    packages/rkwarddev/R/jo.R
M  +2    -3    packages/rkwarddev/R/qp.R
M  +1    -1    packages/rkwarddev/R/rk-internal.R
M  +1    -1    packages/rkwarddev/R/rkwarddev-package.R
M  +4    -4    packages/rkwarddev/inst/NEWS.Rd
M  +2    -8    packages/rkwarddev/man/id.Rd
A  +51   -0    packages/rkwarddev/man/jo.Rd
M  +1    -4    packages/rkwarddev/man/qp.Rd
M  +1    -1    packages/rkwarddev/man/rkwarddev-package.Rd

http://commits.kde.org/rkward/bd1a3ff12140743afc28012172d2fcfb0b47d893

diff --git a/packages/rkwarddev/ChangeLog b/packages/rkwarddev/ChangeLog
index 9040fe8..a3e7bc6 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-19)
+changes in version 0.07-4 (2015-10-24)
 unreleased:
   - this version is under development
 fixed:
@@ -17,9 +17,9 @@ added:
     working rkwarddev script calls
   - new option "ignore" enables rk.uniqueIDs() to not check nodes for
     duplicates (e.g., <copy>)
-  - new option ".objects" in id() and qp(), to provide objects as a list
-  - new option "quoteOperators" in id() makes it possible to use several operators
-    without quoting, id() will detect and quote them for you
+  - 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
 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 e9cc1ce..5adbc82 100644
--- a/packages/rkwarddev/DESCRIPTION
+++ b/packages/rkwarddev/DESCRIPTION
@@ -16,7 +16,7 @@ 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")))
 Version: 0.07-4
-Date: 2015-10-19
+Date: 2015-10-24
 Collate:
     '00_class_01_rk.JS.arr.R'
     '00_class_02_rk.JS.var.R'
@@ -36,6 +36,7 @@ Collate:
     'i18n.R'
     'id.R'
     'ite.R'
+    'jo.R'
     'join.R'
     'qp.R'
     'rk.comment.R'
diff --git a/packages/rkwarddev/NAMESPACE b/packages/rkwarddev/NAMESPACE
index c193b34..5ddc0da 100644
--- a/packages/rkwarddev/NAMESPACE
+++ b/packages/rkwarddev/NAMESPACE
@@ -5,6 +5,7 @@ export(echo)
 export(i18n)
 export(id)
 export(ite)
+export(jo)
 export(join)
 export(plugin2script)
 export(qp)
diff --git a/packages/rkwarddev/R/id.R b/packages/rkwarddev/R/id.R
index 568d373..f61e7d5 100644
--- a/packages/rkwarddev/R/id.R
+++ b/packages/rkwarddev/R/id.R
@@ -26,14 +26,11 @@
 #' 
 #' @param ... One or several character strings and/or \code{XiMpLe.node} objects with plugin nodes,
 #'   and/or objects of classes \code{rk.JS.arr}, \code{rk.JS.opt} or \code{rk.JS.var}, simply separated by comma.
-#' @param quote Logical, it the character strings should be deparsed, so they come out "as-is" when
+#' @param quote Logical, if the character strings should be deparsed, so they come out "as-is" when
 #'    written to files, e.g. by \code{cat}.
 #' @param collapse Character string, defining if and how the individual elements should be glued together.
 #' @param js Logical, if \code{TRUE} returns JavaScript varaible names for \code{XiMpLe.node} objects.
 #'    Otherwise their actual ID is returned.
-#' @param quoteOperators Logical, if \code{TRUE} operators like \code{">="} or \code{"!="} that
-#'    are valid for JavaScript code can be used without quoting and will be replaced by \code{id}
-#'    automatically.
 #' @param .objects Alternative way of specifying objects, if you already have them as a list.
 #' @return A character string.
 #' @export
@@ -48,12 +45,8 @@
 #' cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
 #' id("The variable name is: ", cbox1, "!")
 
-id <- function(..., quote=FALSE, collapse="", js=TRUE, quoteOperators=TRUE, .objects=eval(substitute(alist(...)))){
+id <- function(..., quote=FALSE, collapse="", js=TRUE, .objects=list(...)){
   ID.content <- sapply(.objects, function(this.part){
-      # get the object, not just a name from eval(substitute(alist(...)))
-      if (inherits(this.part, "name")){
-        this.part <- eval(this.part)
-      } else {}
       # if this is a plot options object, by default only paste the printout slot
       # and discard the rest
       this.part <- stripCont(this.part, get="printout")
@@ -94,10 +87,6 @@ id <- function(..., quote=FALSE, collapse="", js=TRUE, quoteOperators=TRUE, .obj
         # strip all semicolons from i18n calls
         node.id <- slot(this.part, "value")
         return(node.id)
-      } else if(inherits(this.part, "call") & isTRUE(quoteOperators)){
-        # replace JS operators
-        node.id <- do.call("replaceJSOperators", args=list(this.part))
-        return(node.id)
       } else {
         if(isTRUE(quote)){
           text.part <- deparse(this.part)
diff --git a/packages/rkwarddev/R/ite.R b/packages/rkwarddev/R/ite.R
index 6ac88ab..8b0115d 100644
--- a/packages/rkwarddev/R/ite.R
+++ b/packages/rkwarddev/R/ite.R
@@ -60,8 +60,8 @@ ite <- function(ifjs, thenjs, elsejs=NULL){
     } else {}
   }
   result <- new("rk.JS.ite",
-    ifJS=do.call("id", args=list(ifjs, js=TRUE)),
-    thenJS=do.call("id", args=list(thenjs, js=TRUE)),
+    ifJS=id(ifjs, js=TRUE),
+    thenJS=id(thenjs, js=TRUE),
     thenifJS=thenifJS,
     elseJS=elsejs,
     elifJS=elifJS
diff --git a/packages/rkwarddev/R/jo.R b/packages/rkwarddev/R/jo.R
new file mode 100644
index 0000000..1438c8f
--- /dev/null
+++ b/packages/rkwarddev/R/jo.R
@@ -0,0 +1,70 @@
+# 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/qp.R b/packages/rkwarddev/R/qp.R
index e6ef241..bedbcb2 100644
--- a/packages/rkwarddev/R/qp.R
+++ b/packages/rkwarddev/R/qp.R
@@ -23,7 +23,6 @@
 #' 
 #' @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.
-#' @param .objects Alternative way of specifying objects, if you already have them as a list.
 #' @return A character string.
 #' @export
 #' @seealso \code{\link[rkwarddev:rk.JS.vars]{rk.JS.vars}},
@@ -37,7 +36,7 @@
 #' cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
 #' qp("The variable name is: ", cbox1, "!")
 
-qp <- function(..., .objects=eval(substitute(alist(...)))){
-  result <- id(quote=TRUE, collapse=" + ", js=TRUE, .objects=.objects)
+qp <- function(...){
+  result <- id(..., quote=TRUE, collapse=" + ", js=TRUE)
   return(result)
 }
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index 24d92f2..2a419e9 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -1435,7 +1435,7 @@ JS.operators <- c(
   "==", "===", "!=", "!==", ">", "<", ">=", "<=",
   "!", "||", "&&"
 ) ## end JS.operators
-
+# currently not working: "%", "++", "--", "=", "+=", "-=", "*=", "/=", "%=", "===", "!==", "!"
 
 ## function replaceJSOperators
 # takes arbitrary R code and tries to replace R operators with character strings.
diff --git a/packages/rkwarddev/R/rkwarddev-package.R b/packages/rkwarddev/R/rkwarddev-package.R
index 8dca6cc..08fa47a 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-19\cr
+#' Date: \tab 2015-10-24\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 e25234c..6160ee8 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-19)}{
+\section{Changes in rkwarddev version 0.07-4 (2015-10-24)}{
   \subsection{unreleased}{
     \itemize{
       \item this version is under development
@@ -25,9 +25,9 @@
         working rkwarddev script calls
       \item new option \code{"ignore"} enables \code{rk.uniqueIDs()} to not check nodes for
         duplicates (e.g., <copy>)
-      \item new option \code{".objects"} in \code{id()} and \code{qp()}, to provide objects as a list
-      \item new option \code{"quoteOperators"} in \code{id()} makes it possible to use several operators
-        without quoting, \code{id()} will detect and quote them for you
+      \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
     }
   }
   \subsection{changed}{
diff --git a/packages/rkwarddev/man/id.Rd b/packages/rkwarddev/man/id.Rd
index 9453e4a..cdf998f 100644
--- a/packages/rkwarddev/man/id.Rd
+++ b/packages/rkwarddev/man/id.Rd
@@ -4,15 +4,14 @@
 \alias{id}
 \title{Replace XiMpLe.node objects with their ID value}
 \usage{
-id(..., quote = FALSE, collapse = "", js = TRUE, quoteOperators = TRUE,
-  .objects = list(...))
+id(..., quote = FALSE, collapse = "", js = TRUE, .objects = list(...))
 }
 \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}, \code{rk.JS.opt} or \code{rk.JS.var},
       simply separated by comma.}
 
-\item{quote}{Logical, it the character strings should be deparsed,
+\item{quote}{Logical, if the character strings should be deparsed,
       so they come out "as-is" when
 written to files, e.g. by \code{cat}.}
 
@@ -23,11 +22,6 @@ written to files, e.g. by \code{cat}.}
       if \code{TRUE} returns JavaScript varaible names for \code{XiMpLe.node} objects.
 Otherwise their actual ID is returned.}
 
-\item{quoteOperators}{Logical,
-      if \code{TRUE} operators like \code{">="} or \code{"!="} that
-are valid for JavaScript code can be used without quoting and will be replaced by \code{id}
-automatically.}
-
 \item{.objects}{Alternative way of specifying objects,
       if you already have them as a list.}
 }
diff --git a/packages/rkwarddev/man/jo.Rd b/packages/rkwarddev/man/jo.Rd
new file mode 100644
index 0000000..8fa3673
--- /dev/null
+++ b/packages/rkwarddev/man/jo.Rd
@@ -0,0 +1,51 @@
+% 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/qp.Rd b/packages/rkwarddev/man/qp.Rd
index f5cd35d..afa2b22 100644
--- a/packages/rkwarddev/man/qp.Rd
+++ b/packages/rkwarddev/man/qp.Rd
@@ -4,15 +4,12 @@
 \alias{qp}
 \title{Replace XiMpLe.node objects with their ID value}
 \usage{
-qp(..., .objects = list(...))
+qp(...)
 }
 \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.}
-
-\item{.objects}{Alternative way of specifying objects,
-      if you already have them as a list.}
 }
 \value{
 A character string.
diff --git a/packages/rkwarddev/man/rkwarddev-package.Rd b/packages/rkwarddev/man/rkwarddev-package.Rd
index 8f3fd36..d24920f 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-19\cr
+Date: \tab 2015-10-24\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