[rkward] packages/rkwarddev: solved the scope issue of js()

m.eik michalke meik.michalke at uni-duesseldorf.de
Sat Nov 28 19:18:10 UTC 2015


Git commit 6d4a7d5fa7a74d51859fd6c05b1b8a23d1fdb01e by m.eik michalke.
Committed on 28/11/2015 at 19:15.
Pushed by meikm into branch 'master'.

solved the scope issue of js()

  - added a new function rk.local() to replace local(), hope this is a clean enough solution

M  +5    -3    packages/rkwarddev/ChangeLog
M  +7    -3    packages/rkwarddev/R/js.R
M  +2    -0    packages/rkwarddev/R/rk-internal.R
A  +32   -0    packages/rkwarddev/R/rk.local.R
A  +21   -0    packages/rkwarddev/man/rk.local.Rd

http://commits.kde.org/rkward/6d4a7d5fa7a74d51859fd6c05b1b8a23d1fdb01e

diff --git a/packages/rkwarddev/ChangeLog b/packages/rkwarddev/ChangeLog
index 9471e04..1f3c2a8 100644
--- a/packages/rkwarddev/ChangeLog
+++ b/packages/rkwarddev/ChangeLog
@@ -1,6 +1,6 @@
 ChangeLog for package rkwarddev
 
-changes in version 0.07-4 (2015-11-23)
+changes in version 0.08-1 (2015-11-28)
 unreleased:
   - this version is under development
 fixed:
@@ -27,8 +27,8 @@ added:
     will be translated to JavaScript equivalents without the need for quoting.
     however, the "for" loop code is not publicly documented and remains a
     proof of concept kind of thing for now
-  - new option ".objects" in id() and ".ite" in rk.JS.options() to provide objects
-    as a list
+  - new option ".objects" in id() and ".ite" in rk.JS.options() to provide
+    objects as a list
   - new option "rk.JS.vars" to make JS variables match the name of the
     original R object
   - new function idq() to print quoted IDs of XML nodes for use in JavaScript
@@ -45,6 +45,8 @@ added:
     update_plugin_messages.py, the package now comes with its own copy of this core i18n script
   - the generator info gained support for hinting at the very rkwarddev
     script file that was used to generate a particular plugin file
+  - new function rk.local() to be used as an alternative to local() with a
+    predefined local environment to solve scope issues with js()
 changed:
   - improved error handling in rk.JS.header(), error messages are more
     informative now
diff --git a/packages/rkwarddev/R/js.R b/packages/rkwarddev/R/js.R
index 4f88d19..ab4b9e2 100644
--- a/packages/rkwarddev/R/js.R
+++ b/packages/rkwarddev/R/js.R
@@ -31,6 +31,9 @@
 #' 
 #' These are currently unsupported and still need to be quoted: \%, ++, --, =, +=, -=, *=, /=, \%=, ===, !== and !
 #'
+#' @note You should nest your plugin script inside \code{\link[rkwarddev:rk.local]{rk.local}} if you're
+#'    making use of js(), to be sure it can find all defined objects.
+#'
 #' @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.
@@ -43,7 +46,8 @@
 #'    of a pasted character string. Comes in handy if used inside \code{\link[rkwarddev:rk.JS.options]{rk.JS.options}}.
 #' @return A character string (or \code{rk.JS.ite}, if \code{keep.ite=TRUE} and input is an \code{if/else} condition).
 #' @export
-#' @seealso \code{\link[rkwarddev:rk.JS.vars]{rk.JS.vars}},
+#' @seealso \code{\link[rkwarddev:rk.local]{rk.local}},
+#'    \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}},
@@ -89,8 +93,8 @@ js <- function(..., level=2, indent.by=rk.get.indent(), linebreaks=TRUE, empty.e
         if(isTRUE(keep.ite)){
           return(this.part)
         } else {
-          if(identical(this.part[[1]], "rk.comment")){
-            return(rk.paste.JS(eval(this.part), level=level, indent.by=indent.by, empty.e=empty.e))
+          if(as.character(this.part[[1]]) %in% c("rk.comment", "tf", "rk.JS.optionset")){
+            return(rk.paste.JS(eval(this.part, envir=.rk.local.env), level=level, indent.by=indent.by, empty.e=empty.e))
           } else {}
           # replace JS operators
           return(do.call("replaceJSOperators", args=list(this.part)))
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index 08dcc88..51f4419 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -21,6 +21,8 @@
 
 # set up an internal environment, e.g. for prompter settings or indentation
 .rkdev.env <- new.env()
+# another environment for use with rk.local()
+.rk.local.env <- new.env()
 
 # internal functions for the rk.* functions
 
diff --git a/packages/rkwarddev/R/rk.local.R b/packages/rkwarddev/R/rk.local.R
new file mode 100644
index 0000000..ff21db7
--- /dev/null
+++ b/packages/rkwarddev/R/rk.local.R
@@ -0,0 +1,32 @@
+# Copyright 2015 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/>.
+
+#' Evaluate code in a local environment
+#' 
+#' Can be used like \code{\link[base:local]{local}}, but evaluation is being done in a speacial
+#' local environment of the rkwarddev package. This can be neccessary if you want to call functions
+#' nested insinde \code{\link[rkwarddev:js]{js}}, because it might not find all objects if they were
+#' only defined in a standard local environment.
+#' 
+#' @param ... The code to be evaluated.
+#' @return The result of evaluating the object(s).
+#' @export
+
+rk.local <- function(...){
+  rm(list=ls(), envir=.rk.local.env)
+  local(..., envir=.rk.local.env)
+}
diff --git a/packages/rkwarddev/man/rk.local.Rd b/packages/rkwarddev/man/rk.local.Rd
new file mode 100644
index 0000000..c51f85f
--- /dev/null
+++ b/packages/rkwarddev/man/rk.local.Rd
@@ -0,0 +1,21 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/rk.local.R
+\name{rk.local}
+\alias{rk.local}
+\title{Evaluate code in a local environment}
+\usage{
+rk.local(...)
+}
+\arguments{
+\item{...}{The code to be evaluated.}
+}
+\value{
+The result of evaluating the object(s).
+}
+\description{
+Can be used like \code{\link[base:local]{local}}, but evaluation is being done in a speacial
+local environment of the rkwarddev package. This can be neccessary if you want to call functions
+nested insinde \code{\link[rkwarddev:js]{js}}, because it might not find all objects if they were
+only defined in a standard local environment.
+}
+



More information about the rkward-tracker mailing list