[rkward-cvs] SF.net SVN: rkward-code:[4821] trunk/rkward/rkward/plugins/data

tfry at users.sf.net tfry at users.sf.net
Mon Sep 15 12:41:24 UTC 2014


Revision: 4821
          http://sourceforge.net/p/rkward/code/4821
Author:   tfry
Date:     2014-09-15 12:41:23 +0000 (Mon, 15 Sep 2014)
Log Message:
-----------
More bits for the 'Recode categorical data' plugin

Modified Paths:
--------------
    trunk/rkward/rkward/plugins/data/level_select.xml
    trunk/rkward/rkward/plugins/data/recode_categorical.js
    trunk/rkward/rkward/plugins/data/recode_categorical.xml

Modified: trunk/rkward/rkward/plugins/data/level_select.xml
===================================================================
--- trunk/rkward/rkward/plugins/data/level_select.xml	2014-09-15 07:59:13 UTC (rev 4820)
+++ trunk/rkward/rkward/plugins/data/level_select.xml	2014-09-15 12:41:23 UTC (rev 4821)
@@ -37,6 +37,7 @@
 					code = "local ({\n";
 					code += "\tx <- {" + expression + "}\n";
 					code += "\tif (length (x) > " + limit + ") x <- c (x[1:" + limit + "], \"____LIMIT____\")\n";
+					code += "\tif (is.character (x)) x <- dQuote (x)\n";
 					code += "\tx\n";
 					code += "})";
 
@@ -45,6 +46,10 @@
 
 				commandFinished = function (result, id) {
 					if (id != last_command_id) return;  // another result is about to arrive
+					if (typeof (result) == "undefined") {
+						gui.setListValue ("selector.available", Array ("ERROR"));
+						return;
+					}
 					gui.setValue ("selector.enabled", 1);
 					limit = gui.getValue ("limit");
 					gui.setValue ("limitnote.visible", result.length > limit ? 1 : 0);

Modified: trunk/rkward/rkward/plugins/data/recode_categorical.js
===================================================================
--- trunk/rkward/rkward/plugins/data/recode_categorical.js	2014-09-15 07:59:13 UTC (rev 4820)
+++ trunk/rkward/rkward/plugins/data/recode_categorical.js	2014-09-15 12:41:23 UTC (rev 4821)
@@ -0,0 +1,54 @@
+function calculate () {
+	var input = getString ("x");
+	var output = input;
+	if (getBoolean ("saveto_other_object")) output = getString ("saveto");
+	var datamode = getString ("datamode");
+	var quote_new_values = (datamode == "character" || datamode == "factor");
+
+	// initialize output vector to defaults
+	// TODO
+
+	// Make replacements
+	var old_values = getList ("set.values");
+	var new_values_types = getList ("set.new_value_types");
+	var new_values_strings = getList ("set.new_value_strings");
+
+	var dupes = Array ();
+	var all_old_values = Array ();
+	for (var i = 0; i < old_values.length; ++i) {
+		var old_values_it = old_values[i].split ('\n');
+
+		// Check for duplicate old values, as the UI does not currently support this!
+		for (var j = 0; j < old_values_it.length; ++j) {
+			if (all_old_values.indexOf (old_values_it[j]) > -1) {
+				dupes.push (old_values_it[j]);
+			} else {
+				all_old_values.push (old_values_it[j]);
+			}
+		}
+
+		var old_index;
+		if (old_values_it.length > 1) {
+			old_index = ' %in% c(' + old_values_it.join (',') + ')';
+		} else {
+			old_index = ' == ' + all_old_values;
+		}
+
+		var replacement = "NA";
+		if (new_values_types[i] != "na") {
+			replacement = new_values_strings[i];
+			if (quote_new_values) replacement = quote (replacement);
+		}
+
+		echo (output + '[' + input + old_index + '] <- ' + replacement + '\n');
+	}
+
+	// Produce warnings (or errors?) TODO
+	if (dupes.length > 0) {
+		echo ("Dupes: " + dupes.join (", "));
+	}
+
+	// Drop NAs TODO
+	// Set data type TODO
+	// Assign to output variable in GlobalEnv TODO
+}

Modified: trunk/rkward/rkward/plugins/data/recode_categorical.xml
===================================================================
--- trunk/rkward/rkward/plugins/data/recode_categorical.xml	2014-09-15 07:59:13 UTC (rev 4820)
+++ trunk/rkward/rkward/plugins/data/recode_categorical.xml	2014-09-15 12:41:23 UTC (rev 4821)
@@ -3,44 +3,99 @@
 	<code file="recode_categorical.js" />
 	<help file="recode_categorical.rkh" />
 	<logic>
-		<connect governor="x.available" client="levels.variable"/>
+		<connect governor="x.available" client="set.contents.levels.variable"/>
+		
+		<convert id="saveto_other_object" mode="equals" sources="saveto_select.string" standard="other"/>
+		<connect governor="saveto_other_object" client="saveto.enabled"/>
+
+		<convert id="other_values_custom" mode="equals" sources="other.string" standard="custom"/>
+		<connect governor="other_values_custom" client="other_custom.enabled"/>
+
+		<script><![CDATA[
+			gui.addChangeCommand ("datamode", "update ()");
+
+			update = function () {
+				var mode = gui.getValue ("datamode.string");
+				gui.setValue ("set.contents.quotation_note.visible", mode == "factor" || mode == "character");
+			}
+		]]></script>
 	</logic>
 	<dialog label="Recode categorical data"><tabbook>
 		<tab label="Input and output variables">
-				<varselector id="vars"/>
-				<varslot id="x" source="vars" label="Select variable to recode"/>
-			<text>TODO where to store output</text>
+			<varselector id="vars"/>
+			<varslot id="x" source="vars" label="Select variable to recode" num_dimensions="1"/>
+			<row>
+				<radio id="saveto_select" label="Save to">
+					<option value="same" label="Same object"/>
+					<option value="other" label="Other:" checked="true"/>
+				</radio>
+				<saveobject id="saveto" label="Object to save to" initial="recoded"/>
+			</row>
 			<dropdown id="datamode" label="Data type after recoding">
 				<option value="character" label="Character"/>
 				<option value="factor" label="Factor" checked="true"/>
 				<option value="numeric" label="Numeric"/>
 				<option value="logical" label="Logical"/>
 			</dropdown>
+			<stretch/>
 		</tab>
 		<tab label="Values">
 			<row>
-				<embed id="levels" component="rkward::level_select"/>
 				<column>
 					<optionset id="set" min_rows="1">
+						<logic>
+							<convert id="new_value_na" mode="equals" sources="new_value.string" standard="na"/>
+							<connect client="new_value_custom.enabled" governor="new_value_na.not"/>
+							<switch condition="new_value.string" id="new_value_string">
+								<case standard="na" fixed_value="_NA_" />
+								<default dynamic_value="new_value_custom.text" />
+							</switch>
+						</logic>
+						<optioncolumn id="values" connect="values.available" label="Old value(s)"/>
+						<optioncolumn id="new_values_labelled" connect="new_value_string" label="New value(s)"/>
+						<optioncolumn id="new_value_types" connect="new_value.string"/>
+						<optioncolumn id="new_value_strings" connect="new_value_custom.text"/>
 						<content>
 							<frame>
 								<optiondisplay index="true"/>
-								<valueslot multi="true" min_vars="1" id="values" label="Values to recode"/>
-								<input id="other_custom" label="Value"/>
+								<row>
+									<column>
+										<text type="normal">Old values</text>
+										<!-- TODO Allow selecting NAs in input -->
+										<embed id="levels" component="rkward::level_select"/>
+									</column>
+									<column>
+										<row>
+											<valueslot multi="true" min_vars="1" id="values" label="Values to recode" source="levels.selector"/>
+											<column>
+												<radio id="new_value" label="New value">
+													<option value="na" label="NA"/>
+													<option value="custom" label="Value:" checked="true"/>
+												</radio>
+												<input id="new_value_custom" label=""/>
+												<text id="quotation_note" type="normal">(Value will be quoted, automatically)</text>
+												<stretch/>
+											</column>
+										</row>
+									</column>
+								</row>
 							</frame>
 						</content>
 					</optionset>
-
-					<radio id="other" label="Any other values">	<!-- TODO Should this be including original NAs? -->
-						<option value="copy" label="Copy" id="copy_others"/>
-						<option value="na" label="NA"/>
-						<option value="custom" label="Custom value:"/>
-					</radio>
+				</column>
+			</row>
+			<row>
+				<radio id="other" label="Any other values">	<!-- TODO Should this be including original NAs? -->
+					<option value="copy" label="Copy" id="copy_others"/>
+					<option value="na" label="NA"/>
+					<option value="custom" label="Custom value:"/>
+				</radio>
+				<column>
 					<input id="other_custom" label="Value"/>
-
-					<checkbox id="drop" label="Drop NAs in recoded variable"/>
+					<stretch/>
 				</column>
 			</row>
+			<checkbox id="drop" label="Drop NAs in recoded variable"/>
 		</tab>
 	</tabbook></dialog>
 </document>
\ No newline at end of file





More information about the rkward-tracker mailing list