[rkward] packages/rkwarddev: trying to get id() replace operators

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


Git commit 7ee945775a2d46dc255e43d57e4ad560fb4bbcdd by m.eik michalke.
Committed on 19/10/2015 at 20:33.
Pushed by meikm into branch 'master'.

trying to get id() replace operators

  - the goal is to make ite(id(foo == "foo"), echo("bar")) equivalent to ite(id(foo, " == \"foo\""), echo("bar"))
  - this is not really working yet and mostly broken, i will have to fix this later, can't really spot the show stoppers
  - on the plus side, it does actually work for simple cases already

M  +2    -0    packages/rkwarddev/ChangeLog
M  +12   -1    packages/rkwarddev/R/id.R
M  +2    -2    packages/rkwarddev/R/ite.R
M  +1    -1    packages/rkwarddev/R/qp.R
M  +57   -0    packages/rkwarddev/R/rk-internal.R
M  +2    -0    packages/rkwarddev/inst/NEWS.Rd
M  +7    -1    packages/rkwarddev/man/id.Rd

http://commits.kde.org/rkward/7ee945775a2d46dc255e43d57e4ad560fb4bbcdd

diff --git a/packages/rkwarddev/ChangeLog b/packages/rkwarddev/ChangeLog
index da877f3..9040fe8 100644
--- a/packages/rkwarddev/ChangeLog
+++ b/packages/rkwarddev/ChangeLog
@@ -18,6 +18,8 @@ added:
   - 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
 changed:
   - improved error handling in rk.JS.header(), error messages are more
     informative now
diff --git a/packages/rkwarddev/R/id.R b/packages/rkwarddev/R/id.R
index 2fb0dc9..568d373 100644
--- a/packages/rkwarddev/R/id.R
+++ b/packages/rkwarddev/R/id.R
@@ -31,6 +31,9 @@
 #' @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
@@ -45,8 +48,12 @@
 #' 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, .objects=list(...)){
+id <- function(..., quote=FALSE, collapse="", js=TRUE, quoteOperators=TRUE, .objects=eval(substitute(alist(...)))){
   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")
@@ -87,6 +94,10 @@ id <- function(..., quote=FALSE, collapse="", js=TRUE, .objects=list(...)){
         # 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 8b0115d..6ac88ab 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=id(ifjs, js=TRUE),
-    thenJS=id(thenjs, js=TRUE),
+    ifJS=do.call("id", args=list(ifjs, js=TRUE)),
+    thenJS=do.call("id", args=list(thenjs, js=TRUE)),
     thenifJS=thenifJS,
     elseJS=elsejs,
     elifJS=elifJS
diff --git a/packages/rkwarddev/R/qp.R b/packages/rkwarddev/R/qp.R
index 74028ce..e6ef241 100644
--- a/packages/rkwarddev/R/qp.R
+++ b/packages/rkwarddev/R/qp.R
@@ -37,7 +37,7 @@
 #' cbox1 <- rk.XML.cbox(label="foo", value="foo1", id.name="CheckboxFoo.ID")
 #' qp("The variable name is: ", cbox1, "!")
 
-qp <- function(..., .objects=list(...)){
+qp <- function(..., .objects=eval(substitute(alist(...)))){
   result <- id(quote=TRUE, collapse=" + ", js=TRUE, .objects=.objects)
   return(result)
 }
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index 1e89044..24d92f2 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -1424,3 +1424,60 @@ check.JS.lines <- function(relevant.tags, single.tags, add.abbrev, js, indent.by
   } else {}
   return(result)
 } ## end function check.JS.lines()
+
+
+## JS.operators
+# a complilation of operators we would like to fetch from R calls and 
+# substitute with character equivalents for JS code
+JS.operators <- c(
+  "+", "-", "*", "/", "%",
+  "++", "--", "=", "+=", "-=", "*=", "/=", "%=",
+  "==", "===", "!=", "!==", ">", "<", ">=", "<=",
+  "!", "||", "&&"
+) ## end JS.operators
+
+
+## function replaceJSOperators
+# takes arbitrary R code and tries to replace R operators with character strings.
+# makes it possible to use these operators in calls like id() without the need
+# for quoting them
+replaceJSOperators <- function(..., call="id"){
+  dotlist <- eval(substitute(alist(...)))
+  dotlist <- lapply(
+    dotlist,
+    function(thisItem){
+      # operators like ">" or "|" are represented as call objects
+      # with the operator as first argument (name).
+      # there can also be calls nested in calls so we need to test this recursively
+      if(inherits(thisItem, "call")){
+        # break the 
+        callList <- unlist(thisItem)
+        if(as.character(callList[[1]]) %in% JS.operators){
+          result <- list(
+            if(is.call(callList[[2]])){
+              do.call("replaceJSOperators", list(callList[[2]]))
+            } else if(is.character(callList[[2]])){
+              paste0("\"", callList[[2]], "\"")
+            } else {
+              do.call(call, list(callList[[2]]))
+            },
+            paste0(" ", as.character(callList[[1]]), " "),
+            if(is.call(callList[[3]])){
+              do.call("replaceJSOperators", list(callList[[3]]))
+            } else if(is.character(callList[[3]])){
+              paste0("\"", callList[[3]], "\"")
+            } else {
+              do.call(call, list(callList[[3]]))
+            }
+          )
+          return(paste0(unlist(result), collapse=""))
+        } else {
+          return(thisItem)
+        }
+      } else {
+        return(thisItem)
+      }
+    }
+  )
+  return(unlist(dotlist))
+} ## end function replaceJSOperators
diff --git a/packages/rkwarddev/inst/NEWS.Rd b/packages/rkwarddev/inst/NEWS.Rd
index a57bdb8..e25234c 100644
--- a/packages/rkwarddev/inst/NEWS.Rd
+++ b/packages/rkwarddev/inst/NEWS.Rd
@@ -26,6 +26,8 @@
       \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
     }
   }
   \subsection{changed}{
diff --git a/packages/rkwarddev/man/id.Rd b/packages/rkwarddev/man/id.Rd
index b7f3bbd..9453e4a 100644
--- a/packages/rkwarddev/man/id.Rd
+++ b/packages/rkwarddev/man/id.Rd
@@ -4,7 +4,8 @@
 \alias{id}
 \title{Replace XiMpLe.node objects with their ID value}
 \usage{
-id(..., quote = FALSE, collapse = "", js = TRUE, .objects = list(...))
+id(..., quote = FALSE, collapse = "", js = TRUE, quoteOperators = TRUE,
+  .objects = list(...))
 }
 \arguments{
 \item{...}{One or several character strings and/or \code{XiMpLe.node} objects with plugin nodes,
@@ -22,6 +23,11 @@ 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.}
 }



More information about the rkward-tracker mailing list