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

tfry at users.sourceforge.net tfry at users.sourceforge.net
Tue Oct 2 16:19:15 UTC 2012


Revision: 4347
          http://rkward.svn.sourceforge.net/rkward/?rev=4347&view=rev
Author:   tfry
Date:     2012-10-02 16:19:15 +0000 (Tue, 02 Oct 2012)
Log Message:
-----------
Fix broken df when changing data type of column in new data.frame to factor.
The important part is: Never set levels to NULL on a factor. The changes to .rk.set.vector.mode are mostly cosmetic.

Modified Paths:
--------------
    trunk/rkward/rkward/core/rkvariable.cpp
    trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R

Modified: trunk/rkward/rkward/core/rkvariable.cpp
===================================================================
--- trunk/rkward/rkward/core/rkvariable.cpp	2012-10-02 15:45:07 UTC (rev 4346)
+++ trunk/rkward/rkward/core/rkvariable.cpp	2012-10-02 16:19:15 UTC (rev 4347)
@@ -724,7 +724,9 @@
 		// using attr (..., "levels) instead of levels (...) in order to bypass checking
 		RKGlobals::rInterface ()->issueCommand ("attr (" + getFullName () + ", \"levels\") <- " + level_string, RCommand::App | RCommand::Sync, QString::null, 0, 0, chain);
 	} else {
-		RKGlobals::rInterface ()->issueCommand ("attr (" + getFullName () + ", \"levels\") <- NULL", RCommand::App | RCommand::Sync, QString::null, 0, 0, chain);
+		QString empty = "NULL";
+		if (getDataType () == DataFactor) empty = "NA";	// never set levels to NULL on a factor!
+		RKGlobals::rInterface ()->issueCommand ("attr (" + getFullName () + ", \"levels\") <- " + empty, RCommand::App | RCommand::Sync, QString::null, 0, 0, chain);
 	}
 }
 

Modified: trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R
===================================================================
--- trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R	2012-10-02 15:45:07 UTC (rev 4346)
+++ trunk/rkward/rkward/rbackend/rpackages/rkward/R/internal.R	2012-10-02 16:19:15 UTC (rev 4347)
@@ -272,22 +272,18 @@
 }
 
 # Change storage type of x to mode newmode.
-# Most attributes will be kept, but the data is erased!
+# Keeps the .rk.meta attribute, and levels attributes, but the data is erased!
 #' @export
 ".rk.set.vector.mode" <- function (x, fun, envir=parent.frame ()) {
-	old_attr <- attributes (x)
-	old_attr$class <- NULL
-	old_attr[[".rk.invalid.fields"]] <- list ()	# will be reset, anyway!
+	y <- fun(rep(NA, length.out = length(x)))
 
-	y <- fun (rep (NA, length.out=length (x)))
+	newattrs <- attributes(y)
+	if (is.null (newattrs)) newattrs <- list ()
+	newattrs[[".rk.meta"]] <- attributes(x)[[".rk.meta"]]
+	lvls <- attributes(x)[["levels"]]
+	if (!is.null (lvls)) newattrs[["levels"]] <- lvls
+	attributes(y) <- newattrs
 
-	# merge old attributes with new ones
-	newattrs <- attributes (y)
-	for (nattr in names (newattrs)) {
-		old_attr[[nattr]] <- newattrs[[nattr]]
-	}
-
-	attributes (y) <- old_attr
 	eval (substitute (x <- y), envir=envir)
 }
 

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