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

tfry at users.sourceforge.net tfry at users.sourceforge.net
Tue Feb 8 09:14:54 UTC 2011


Revision: 3423
          http://rkward.svn.sourceforge.net/rkward/?rev=3423&view=rev
Author:   tfry
Date:     2011-02-08 09:14:53 +0000 (Tue, 08 Feb 2011)

Log Message:
-----------
Fix some problems with factor conversion, as pointed out by m-eik.

Modified Paths:
--------------
    trunk/rkward/ChangeLog
    trunk/rkward/rkward/core/rkvariable.cpp

Modified: trunk/rkward/ChangeLog
===================================================================
--- trunk/rkward/ChangeLog	2011-02-07 12:42:24 UTC (rev 3422)
+++ trunk/rkward/ChangeLog	2011-02-08 09:14:53 UTC (rev 3423)
@@ -1,4 +1,6 @@
 --- Version 0.5.5 - XXX-XX-2011
+- Fixed: Converting from factor to string in the data editor set values to numeric, internally
+- Allow entering factor labels instead of only numbers, when editing factors in the data editor
 - Display logical values as "TRUE" and "FALSE" in data editor, and accept "T"/"TRUE"/"F"/"FALSE" in addition to "0"/"1" while editing
 - Add support for R functions loadhistory(), savehistory(), history(), and timestamp ()
 - Do not load .RData file from current directory by default (configurable)

Modified: trunk/rkward/rkward/core/rkvariable.cpp
===================================================================
--- trunk/rkward/rkward/core/rkvariable.cpp	2011-02-07 12:42:24 UTC (rev 3422)
+++ trunk/rkward/rkward/core/rkvariable.cpp	2011-02-08 09:14:53 UTC (rev 3423)
@@ -61,9 +61,11 @@
 	if (data) {
 		// quick and dirty approach! TODO: make more efficient
 		QStringList list;
-		for (int i=0; i < getLength (); ++i) {
-			list.append (getText (i));
-		}
+#if QT_VERSION >= 0x040700
+		list.reserve (getLength ());
+#endif
+		bool labelled = (new_type == DataCharacter);
+		for (int i=0; i < getLength (); ++i) list.append (getText (i, labelled));
 
 		// all pending changes are moot
 		discardUnsyncedChanges ();
@@ -87,11 +89,8 @@
 
 		// re-set all data
 		lockSyncing (true);
-		int i = 0;
-		for (QStringList::const_iterator it = list.constBegin (); it != list.constEnd (); ++it) {
-			setText (i, *it);
-			i++;
-		}
+		for (int i = list.size () - 1; i >= 0; --i) setText (i, list[i]);
+
 		if (sync) {
 			QString command = ".rk.set.vector.mode(" + getFullName () + ", ";
 			if (new_type == RObject::DataCharacter) command += "as.character";
@@ -474,19 +473,20 @@
 		if (getDataType () == DataCharacter) {
 			data->cell_strings[row] = text;
 		} else if (getDataType () == DataFactor) {
-			if (data->value_labels && data->value_labels->contains (text)) {
-				data->cell_doubles[row] = text.toInt ();
-			} else {
-				valid = false;
+			if (data->value_labels) {
+				QString realtext = data->value_labels->key (text);
+				if (realtext.isEmpty ()) valid = false;
+				else data->cell_doubles[row] = realtext.toInt ();
+			} else valid = false;
+			if (!valid) {	// setting by lavel failed. Try to set a numeric value, instead
+				data->cell_doubles[row] = text.toDouble (&valid);
 			}
 		} else if (getDataType () == DataLogical) {
 			if (text == "0" || text == "F" || text == "FALSE") data->cell_doubles[row] = 0;
 			else if (text == "1" || text == "T" || text == "TRUE") data->cell_doubles[row] = 1;
 			else valid = false;
 		} else {
-			bool ok;
-			data->cell_doubles[row] = text.toDouble (&ok);
-			if (!ok) valid = false;
+			data->cell_doubles[row] = text.toDouble (&valid);
 		}
 	}
 


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