[rkward-cvs] SF.net SVN: rkward:[4276] trunk/rkward

tfry at users.sourceforge.net tfry at users.sourceforge.net
Tue Aug 14 09:25:11 UTC 2012


Revision: 4276
          http://rkward.svn.sourceforge.net/rkward/?rev=4276&view=rev
Author:   tfry
Date:     2012-08-14 09:25:11 +0000 (Tue, 14 Aug 2012)
Log Message:
-----------
Add rk.list.labels, make rk.get.label return empty string, instead of NULL, in case no label has been assigned

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/rkward/rbackend/rpackages/rkward/NAMESPACE
    trunk/rkward/rkward/rbackend/rpackages/rkward/R/rk.label-functions.R
    trunk/rkward/rkward/rbackend/rpackages/rkward/man/rk.get.label.Rd
    trunk/rkward/rkward/windows/rkcommandeditorwindow.cpp

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2012-08-08 16:22:37 UTC (rev 4275)
+++ trunk/rkward/ChangeLog	2012-08-14 09:25:11 UTC (rev 4276)
@@ -1,3 +1,5 @@
+- New function rk.list.labels() to retrieve column labels in a data.frame
+- rk.get.label() will now return an empty string ("") instead of NULL, in case no label is assigned
 - Fixed: Changed configuration settings would not be saved for script editor
 - Fixed: One character missing in R commands on lines longer than 4096 characters
 - Fixed: "Next"-button in wizards would remain enabled while settings are missing on a page

Modified: trunk/rkward/rkward/rbackend/rpackages/rkward/NAMESPACE
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkward/NAMESPACE	2012-08-08 16:22:37 UTC (rev 4275)
+++ trunk/rkward/rkward/rbackend/rpackages/rkward/NAMESPACE	2012-08-14 09:25:11 UTC (rev 4276)
@@ -51,6 +51,7 @@
 export(.rk.killPreviewDevice)
 export(rk.last.plot)
 export(rk.list)
+export(rk.list.labels)
 export(rk.list.names)
 export(rk.list.plugins)
 export(rk.load.pluginmaps)

Modified: trunk/rkward/rkward/rbackend/rpackages/rkward/R/rk.label-functions.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkward/R/rk.label-functions.R	2012-08-08 16:22:37 UTC (rev 4275)
+++ trunk/rkward/rkward/rbackend/rpackages/rkward/R/rk.label-functions.R	2012-08-14 09:25:11 UTC (rev 4276)
@@ -3,7 +3,15 @@
 #' \code{rk.get.label} retrieves the rkward label (if any) of the given object.
 #' 
 #' \code{rk.set.label} sets the rkward label for the given object.
-#' 
+#'
+#' \code{rk.list.labels} retrieves the rkward labels for a list of objects.
+#' Most importantly, this can be used for extracting all column labels in a
+#' \code{data.frame}, conveniently. The parameter \code{fill} controls, what
+#' happens, when no rkward labels have been assigned. The default (\code{FALSE})
+#' is to return empty strings for any missing labels. For \code{fill=TRUE}, missing
+#' labels will be filled with the short names of the object. You can also pass
+#' a character vector of default labels to use as the \code{fill} parameter.
+#'
 #' \code{rk.get.short.name} creates a short name for the given object.
 #' 
 #' \code{rk.get.description} creates descriptive string(s) for each of the
@@ -25,6 +33,7 @@
 #' @param x any R object
 #' @param label a string, to set the label attribute of an object
 #' @param envir an environment, where the attribute is evaluated
+#' @param fill a logical or character. See Details.
 #' @param paste.sep a string, used as the \code{collapse} argument for paste
 #' @param is.substitute a logical (not NA). See Details.
 #' @return \code{rk.set.label} returns the result of the evaluation of "setting
@@ -39,6 +48,8 @@
 #' rk.get.short.name (x$a)                   # "x$a"
 #' rk.get.label (x$a)                        # "First column"
 #' rk.get.description (x$a)                  # "x$a (First column)"
+#' rk.list.labels (x)                        # "First column" ""
+#' rk.list.labels (x, TRUE)                  # "First column" "b"
 #' rk.list.names (x, x$a, x$b)               # "x" "x$a" "x$b"
 #' names (rk.list (x$a, x$b))                # "x$a (First column)" "x$b"
 #' 
@@ -47,11 +58,11 @@
 #' @export
 "rk.get.label" <- function (x) {
 	if (is.call (x) || is.name (x)) {
-		ret <- attr (eval (x), ".rk.meta")[names (attr (eval (x), ".rk.meta")) == "label"]
-	} else {
-		ret <- attr (x, ".rk.meta")[names (attr (x, ".rk.meta")) == "label"]
+		x <- eval (x)
 	}
-	as.character (as.vector (ret))
+	ret <- attr (x, ".rk.meta")[["label"]]
+	if (is.null (ret) || is.na (ret)) ""
+	else as.character (as.vector (ret))
 }
 
 # set rkward label
@@ -67,6 +78,19 @@
 	eval(substitute(attr(x, ".rk.meta") <- meta), envir = envir)
 }
 
+# retrieve the rkward labels for items in the given list
+#' @rdname rk.label
+#' @export
+"rk.list.labels" <- function (x, fill=FALSE) {
+	ret <- sapply (x, rk.get.label)
+	if (isTRUE (fill)) {
+		ret[ret == ""] <- names(x)[ret == ""]
+	} else if (!is.logical (fill)) {
+		ret[ret == ""] <- as.character (fill)[ret == ""]
+	}
+	ret
+}
+
 # get a short name for the given object
 #' @rdname rk.label
 #' @export

Modified: trunk/rkward/rkward/rbackend/rpackages/rkward/man/rk.get.label.Rd
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkward/man/rk.get.label.Rd	2012-08-08 16:22:37 UTC (rev 4275)
+++ trunk/rkward/rkward/rbackend/rpackages/rkward/man/rk.get.label.Rd	2012-08-14 09:25:11 UTC (rev 4276)
@@ -2,6 +2,7 @@
 
 \alias{rk.get.label}
 \alias{rk.set.label}
+\alias{rk.list.labels}
 \alias{rk.get.short.name}
 \alias{rk.get.description}
 \alias{rk.list.names}
@@ -12,6 +13,7 @@
 \usage{
 rk.get.label(x)
 rk.set.label(x, label, envir = parent.frame())
+rk.list.labels (x, fill=FALSE)
 rk.get.short.name(x)
 rk.get.description(..., paste.sep = NULL, is.substitute = FALSE)
 rk.list.names(..., deparse.level = 2)
@@ -22,6 +24,7 @@
   \item{x}{any R object}
   \item{label}{a string, to set the label attribute of an object}
   \item{envir}{an environment, where the attribute is evaluated}
+  \item{fill}{a logical or character. See Details.}
   \item{paste.sep}{a string, used as the \code{collapse} argument for paste}
   \item{is.substitute}{a logical (not NA). See Details.}
 }
@@ -31,6 +34,8 @@
 
  \code{rk.set.label} sets the rkward label for the given object.
 
+ \code{rk.list.labels} retrieves the rkward labels for a list of objects. Most importantly, this can be used for extracting all column labels in a \code{data.frame}, conveniently. The parameter \code{fill} controls, what happens, when no rkward labels have been assigned. The default (\code{FALSE}) is to return empty strings for any missing labels. For \code{fill=TRUE}, missing labels will be filled with the short names of the object. You can also pass a character vector of default labels to use as the \code{fill} parameter.
+ 
  \code{rk.get.short.name} creates a short name for the given object.
 
  \code{rk.get.description} creates descriptive string(s) for each of the arguments in "\code{\dots}"; collapsing into a single string using \code{paste.sep} (if not NULL). If \code{is.substitute=TRUE}, the arguments will be deparsed, first, which can be useful when using \code{rk.get.description} inside a function.
@@ -52,6 +57,8 @@
 rk.get.short.name (x$a)                   # "x$a"
 rk.get.label (x$a)                        # "First column"
 rk.get.description (x$a)                  # "x$a (First column)"
+rk.list.labels (x)                        # "First column" ""
+rk.list.labels (x, TRUE)                  # "First column" "b"
 rk.list.names (x, x$a, x$b)               # "x" "x$a" "x$b"
 names (rk.list (x$a, x$b))                # "x$a (First column)" "x$b"
 }

Modified: trunk/rkward/rkward/windows/rkcommandeditorwindow.cpp
===================================================================
--- trunk/rkward/rkward/windows/rkcommandeditorwindow.cpp	2012-08-08 16:22:37 UTC (rev 4275)
+++ trunk/rkward/rkward/windows/rkcommandeditorwindow.cpp	2012-08-14 09:25:11 UTC (rev 4276)
@@ -158,6 +158,8 @@
 RKCommandEditorWindow::~RKCommandEditorWindow () {
 	RK_TRACE (COMMANDEDITOR);
 
+	// NOTE: TODO: Ideally we'd only write out a changed config, but how to detect config changes?
+	// 	Alternatively, only for the last closed script window
 	m_doc->editor ()->writeConfig ();
 
 	delete hinter;

This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site.





More information about the rkward-tracker mailing list