[rkward] packages/rkwarddev: tried to match R object name an JS variable name
m.eik michalke
meik.michalke at uni-duesseldorf.de
Sat Oct 24 15:53:42 UTC 2015
Git commit b37394317e84e20a5a872e880c3760c97ac0375c by m.eik michalke.
Committed on 24/10/2015 at 15:49.
Pushed by meikm into branch 'master'.
tried to match R object name an JS variable name
- there's now an option "object.names" in rk.JS.vars(), which will try to keep close to the original R object name when coming up with JS variable names.
this will however lead to massive chaos if you use it together with rk.JS.scan(), as the latter cannot access R object names, but only the IDs in XML tree structures.
therefore, it only makes some sense to use if you don't scan for variables but define them all on your own.
M +1 -0 packages/rkwarddev/ChangeLog
M +3 -1 packages/rkwarddev/R/rk-internal.R
M +29 -4 packages/rkwarddev/R/rk.JS.vars.R
M +1 -0 packages/rkwarddev/inst/NEWS.Rd
M +9 -1 packages/rkwarddev/man/rk.JS.vars.Rd
http://commits.kde.org/rkward/b37394317e84e20a5a872e880c3760c97ac0375c
diff --git a/packages/rkwarddev/ChangeLog b/packages/rkwarddev/ChangeLog
index a3e7bc6..41cad86 100644
--- a/packages/rkwarddev/ChangeLog
+++ b/packages/rkwarddev/ChangeLog
@@ -20,6 +20,7 @@ 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
changed:
- improved error handling in rk.JS.header(), error messages are more
informative now
diff --git a/packages/rkwarddev/R/rk-internal.R b/packages/rkwarddev/R/rk-internal.R
index 2a419e9..91cefe3 100644
--- a/packages/rkwarddev/R/rk-internal.R
+++ b/packages/rkwarddev/R/rk-internal.R
@@ -604,7 +604,7 @@ get.authors <- function(description, maintainer=TRUE, contributor=FALSE, copyrig
## function check.ID()
# - node: a XiMpLe.node to search for an ID
# - search.environment: if TRUE, the internal environment is searched for the ID
-# as well; a use case for this is IDs of oprions, which need their parent IDs as well;
+# as well; a use case for this is IDs of options, which need their parent IDs as well;
# see get.optionIDs() below
# - env.get: the ID type to fetch from the environment, if search.environment=TRUE
check.ID <- function(node, search.environment=FALSE, env.get="XML"){
@@ -1209,6 +1209,7 @@ dependenciesCompatWrapper <- function(dependencies, about, hints=FALSE){
return(results)
} ## end function dependenciesCompatWrapper()
+
## function get.rk.env()
# generic function to query the internal environment and declare a desired object, if not present yet
get.rk.env <- function(name, value=list()){
@@ -1437,6 +1438,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.
# makes it possible to use these operators in calls like id() without the need
diff --git a/packages/rkwarddev/R/rk.JS.vars.R b/packages/rkwarddev/R/rk.JS.vars.R
index 7d131a4..8a24120 100644
--- a/packages/rkwarddev/R/rk.JS.vars.R
+++ b/packages/rkwarddev/R/rk.JS.vars.R
@@ -36,6 +36,10 @@
#' useful alternatives. For backwards compatibility, the default is set to \code{"getValue"}.
#' @param guess.getter Logical, if \code{TRUE} try to get a good default getter function for JavaScript
#' variable values.
+#' @param object.name Logical, if \code{TRUE} the JS variable name will roughly match the R object name. If the
+#' object name contains dots, they will be removed and the JS name printed in camel code instead. Use this option
+#' with great caution and do not combine it with \code{\link[rkwarddev:rk.JS.scan]{rk.JS.scan}}, as it will likely result
+#' in unusable code. \code{rk.JS.scan} examines XML nodes and therefore does not know any R object names.
#' @return An object of class \code{rk.JS.var}.
#' @export
#' @seealso \code{\link[rkwarddev:rk.JS.array]{rk.JS.array}},
@@ -51,13 +55,34 @@
#' cat(rk.paste.JS(rk.JS.vars(list(checkA, checkB, checkC))))
rk.JS.vars <- function(..., var.prefix=NULL, modifiers=NULL, default=FALSE, join="", check.modifiers=TRUE,
- getter="getValue", guess.getter=FALSE){
- variables <- list(...)
+ getter="getValue", guess.getter=FALSE, object.name=FALSE){
+ variables <- child.list(list(...))
+ if(isTRUE(object.name)){
+ var.alist <- eval(substitute(alist(...)))
+ JS.var.names <- lapply(
+ 1:length(variables),
+ function(this.var.num){
+ this.var <- var.alist[[this.var.num]]
+ if(is.name(this.var)){
+ return(deparse(this.var))
+ } else if(is.character(this.var)){
+ return(this.var)
+ } else {
+ # fall back to the original input if we can't clearly make sense of it here
+ return(variables[[this.var.num]])
+ }
+ }
+ )
+ } else {}
JS.vars <- new("rk.JS.var",
- vars=sapply(child.list(variables), function(this.var){
+ vars=sapply(1:length(variables), function(this.var.num){
+ this.var <- this.JS.var <- variables[[this.var.num]]
+ if(isTRUE(object.name)){
+ this.JS.var <- JS.var.names[[this.var.num]]
+ } else {}
get.JS.vars(
- JS.var=this.var,
+ JS.var=this.JS.var,
XML.var=this.var,
JS.prefix=var.prefix,
modifiers=modifiers,
diff --git a/packages/rkwarddev/inst/NEWS.Rd b/packages/rkwarddev/inst/NEWS.Rd
index 6160ee8..f715a29 100644
--- a/packages/rkwarddev/inst/NEWS.Rd
+++ b/packages/rkwarddev/inst/NEWS.Rd
@@ -28,6 +28,7 @@
\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
}
}
\subsection{changed}{
diff --git a/packages/rkwarddev/man/rk.JS.vars.Rd b/packages/rkwarddev/man/rk.JS.vars.Rd
index 3fd8ed6..0eeaf92 100644
--- a/packages/rkwarddev/man/rk.JS.vars.Rd
+++ b/packages/rkwarddev/man/rk.JS.vars.Rd
@@ -6,7 +6,7 @@
\usage{
rk.JS.vars(..., var.prefix = NULL, modifiers = NULL, default = FALSE,
join = "", check.modifiers = TRUE, getter = "getValue",
- guess.getter = FALSE)
+ guess.getter = FALSE, object.name = FALSE)
}
\arguments{
\item{...}{Either one or more character strings (the names of the variables to define),
@@ -40,6 +40,14 @@ useful alternatives. For backwards compatibility,
\item{guess.getter}{Logical,
if \code{TRUE} try to get a good default getter function for JavaScript
variable values.}
+
+\item{object.name}{Logical,
+ if \code{TRUE} the JS variable name will roughly match the R object name. If the
+object name contains dots,
+ they will be removed and the JS name printed in camel code instead. Use this option
+with great caution and do not combine it with \code{\link[rkwarddev:rk.JS.scan]{rk.JS.scan}},
+ as it will likely result
+in unusable code. \code{rk.JS.scan} examines XML nodes and therefore does not know any R object names.}
}
\value{
An object of class \code{rk.JS.var}.
More information about the rkward-tracker
mailing list