[rkward] rkward/plugins: Add previews to sort data and recode categorical plugins. Prevent dropping to vector on some existing plugins.

Thomas Friedrichsmeier thomas.friedrichsmeier at ruhr-uni-bochum.de
Fri Jan 29 13:10:46 UTC 2016


Git commit d03aeec46a15d7e02c0fe4e2d067f780b764b61a by Thomas Friedrichsmeier.
Committed on 29/01/2016 at 13:10.
Pushed by tfry into branch 'master'.

Add previews to sort data and recode categorical plugins. Prevent dropping to vector on some existing plugins.

M  +1    -1    rkward/plugins/00saveload/import/import_spss.js
M  +1    -1    rkward/plugins/00saveload/import/import_stata.js
M  +1    -1    rkward/plugins/00saveload/import/import_xls.js
M  +1    -1    rkward/plugins/00saveload/import/import_xls_xlconnect.js
M  +0    -2    rkward/plugins/00saveload/import/load_data.js
M  +13   -5    rkward/plugins/data/recode_categorical.js
M  +3    -0    rkward/plugins/data/recode_categorical.rkh
M  +1    -0    rkward/plugins/data/recode_categorical.xml
M  +16   -4    rkward/plugins/data/sort.js
M  +2    -0    rkward/plugins/data/sort.rkh
M  +1    -0    rkward/plugins/data/sort.xml

http://commits.kde.org/rkward/d03aeec46a15d7e02c0fe4e2d067f780b764b61a

diff --git a/rkward/plugins/00saveload/import/import_spss.js b/rkward/plugins/00saveload/import/import_spss.js
index c96bb2d..e43a718 100644
--- a/rkward/plugins/00saveload/import/import_spss.js
+++ b/rkward/plugins/00saveload/import/import_spss.js
@@ -45,7 +45,7 @@ function calculate (is_preview) {
 	}
 	echo ('\n');
 	if (is_preview) {
-		echo ('preview_data <- data[1:min(50,dim(data)[1]),1:min(50,dim(data)[2])]\n');
+		echo ('preview_data <- data[1:min(50,dim(data)[1]),1:min(50,dim(data)[2]),drop=FALSE]\n');
 	} else {
 		echo ('.GlobalEnv$' + object + ' <- data		'); comment ('assign to globalenv()');
 		if (getValue ("doedit") && data_frame) {
diff --git a/rkward/plugins/00saveload/import/import_stata.js b/rkward/plugins/00saveload/import/import_stata.js
index 8c8bd85..10d65aa 100644
--- a/rkward/plugins/00saveload/import/import_stata.js
+++ b/rkward/plugins/00saveload/import/import_stata.js
@@ -54,7 +54,7 @@ function calculate (is_preview) {
 	echo ('}\n');
 	echo ('\n');
 	if (is_preview) {
-		echo ('preview_data <- data[1:min(50,dim(data)[1]),1:min(50,dim(data)[2])]\n');
+		echo ('preview_data <- data[1:min(50,dim(data)[1]),1:min(50,dim(data)[2]),drop=FALSE]\n');
 	} else {
 		echo ('.GlobalEnv$' + object + ' <- data		'); comment ('assign to globalenv()');
 		if (getValue ("doedit") ) {
diff --git a/rkward/plugins/00saveload/import/import_xls.js b/rkward/plugins/00saveload/import/import_xls.js
index 572039d..717d5f6 100644
--- a/rkward/plugins/00saveload/import/import_xls.js
+++ b/rkward/plugins/00saveload/import/import_xls.js
@@ -27,7 +27,7 @@ function calculate (is_preview) {
 	      ', strip.white = ' + getValue("stripwhite") + ')\n');
 
 	if (is_preview) {
-		echo ('preview_data <- data[1:min(50,dim(data)[1]),1:min(50,dim(data)[2])]\n');
+		echo ('preview_data <- data[1:min(50,dim(data)[1]),1:min(50,dim(data)[2]),drop=FALSE]\n');
 	} else {
 		var object = getValue ("saveto");
 		echo ('.GlobalEnv$' + object + ' <- data		'); comment ('assign to globalenv()');
diff --git a/rkward/plugins/00saveload/import/import_xls_xlconnect.js b/rkward/plugins/00saveload/import/import_xls_xlconnect.js
index bb33e52..45a66b8 100644
--- a/rkward/plugins/00saveload/import/import_xls_xlconnect.js
+++ b/rkward/plugins/00saveload/import/import_xls_xlconnect.js
@@ -37,7 +37,7 @@ function calculate (is_preview) {
 	makeEncodingCall ('data');
 	echo ('\n');
 	if (is_preview) {
-		echo ('preview_data <- data[1:min(50,dim(data)[1]),1:min(50,dim(data)[2])]\n');
+		echo ('preview_data <- data[1:min(50,dim(data)[1]),1:min(50,dim(data)[2]),drop=FALSE]\n');
 	} else {
 		var object = getString ("saveto");
 		echo ('.GlobalEnv$' + object + ' <- data		'); comment ('assign to globalenv()');
diff --git a/rkward/plugins/00saveload/import/load_data.js b/rkward/plugins/00saveload/import/load_data.js
index a3c9477..32ea1d3 100644
--- a/rkward/plugins/00saveload/import/load_data.js
+++ b/rkward/plugins/00saveload/import/load_data.js
@@ -19,5 +19,3 @@ function calculate () {
 function printout () {
 	new Header (i18n ("Load data")).addFromUI ("file").add (i18n ("Import to environment"), envir).print ();
 }
-
-
diff --git a/rkward/plugins/data/recode_categorical.js b/rkward/plugins/data/recode_categorical.js
index 385a13d..f38910e 100644
--- a/rkward/plugins/data/recode_categorical.js
+++ b/rkward/plugins/data/recode_categorical.js
@@ -1,7 +1,11 @@
 var input;
 var output;
 
-function calculate () {
+function preview () {
+	calculate (true);
+}
+
+function calculate (is_preview) {
 	input = getString ("x");
 	output = input;
 	if (getBoolean ("saveto_other_object")) output = getString ("saveto");
@@ -69,10 +73,14 @@ function calculate () {
 		echo ('\nwarning (' + i18n ("Some input values were specified more than once: ") + ', ' + quote (dupes.join (', ')) + ')\n');
 	}
 
-	// Set data type and assign to output variable in GlobalEnv
-	echo ('.GlobalEnv$' + output + ' <- ');
-	if (is_factor) echo ('as.factor (recoded)\n');
-	else echo ('recoded\n');
+	// Set data type and assign to output variable in GlobalEnv / preview
+	var result = is_factor ? 'as.factor (recoded)' : 'recoded';
+	if (is_preview) {
+		echo ('temp <- data.frame ("Old values"=I(' + input + '), "New values"=I(' + result + '))\n');
+		echo ('preview_data <- temp[1:min(dim(temp)[1],500),]\n');
+	} else {
+		echo ('.GlobalEnv$' + output + ' <- ' + result + '\n');
+	}
 }
 
 function printout () {
diff --git a/rkward/plugins/data/recode_categorical.rkh b/rkward/plugins/data/recode_categorical.rkh
index e1b61fc..073799c 100644
--- a/rkward/plugins/data/recode_categorical.rkh
+++ b/rkward/plugins/data/recode_categorical.rkh
@@ -25,6 +25,9 @@
 		<setting id="datamode">
 			Storage mode of the recoded data.
 		</setting>
+		<setting id="preview">
+			Show a preview of the recoding, as a table with two columns for old and new values. The preview is limited to the first 500 rows of data, for performance reasons.
+		</setting>
 		<caption id="tab_values"/>
 		<setting id="set">
 			The upper half of this tab allows you to specify recodings for individual input values. To add more recodings, click the "add/+"-button. To
diff --git a/rkward/plugins/data/recode_categorical.xml b/rkward/plugins/data/recode_categorical.xml
index bc86dcb..69e11b3 100644
--- a/rkward/plugins/data/recode_categorical.xml
+++ b/rkward/plugins/data/recode_categorical.xml
@@ -39,6 +39,7 @@
 				<option value="logical" label="Logical"/>
 			</dropdown>
 			<stretch/>
+			<preview id="preview" label="Preview" mode="data"/>
 		</tab>
 		<tab label="Values" id="tab_values">
 			<row>
diff --git a/rkward/plugins/data/sort.js b/rkward/plugins/data/sort.js
index 01ab771..47a6123 100644
--- a/rkward/plugins/data/sort.js
+++ b/rkward/plugins/data/sort.js
@@ -1,3 +1,7 @@
+function preview () {
+	calculate (true);
+}
+
 function makeSortItem (objectname, reversed, mode, conversion_custom) {
 	var obj = objectname;
 	if (mode == 'character') obj = 'as.character (' + obj + ')';
@@ -12,7 +16,7 @@ function makeSortItem (objectname, reversed, mode, conversion_custom) {
 	return obj;
 }
 
-function calculate () {
+function calculate (is_preview) {
 	var object = getString ("object");
 	var is_data_frame = getBoolean ("sortby.enabled");
 
@@ -33,7 +37,15 @@ function calculate () {
 		sortby = makeSortItem (object, false, getString ("conversion"), getString ("conversion_custom"));
 	}
 
-	echo ('.GlobalEnv$' + saveto + ' <- ' + object + '[order (' + sortby + getString ("order") + ')');
-	if (is_data_frame) echo (',');
-	echo (']\n');
+	var sorted = object + '[order (' + sortby + getString ("order") + ')';
+	if (is_data_frame) sorted = sorted + ',';
+	sorted = sorted + ']';
+
+	if (is_preview) {
+		echo ('sorted <- ' + sorted + '\n');
+		echo ('sorted <- as.data.frame (sorted)\n');
+		echo ('preview_data <- sorted[1:min(dim(sorted)[1],200),1:min(dim(sorted)[2],50),drop=FALSE]\n');
+	} else {
+		echo ('.GlobalEnv$' + saveto + ' <- ' + sorted + '\n');
+	}
 }
diff --git a/rkward/plugins/data/sort.rkh b/rkward/plugins/data/sort.rkh
index 3872653..d86986a 100644
--- a/rkward/plugins/data/sort.rkh
+++ b/rkward/plugins/data/sort.rkh
@@ -13,6 +13,8 @@ save to a separate object.
 		<setting id="sortby">Only for data.frame objects. Select the column(s) to sort by.</setting>
 		<setting id="saveto_select">Choose "Same object" (the default) to replace the object to be sorted with the sorted variant. If, instead, you want to keep the unsorted object, choose "Another object" to save the sorted object, separately.</setting>
 		<setting id="saveto">Only, if you selected "Another object", above: Specify the object to save to.</setting>
+		<setting id="preview">Show a preview of the sorted data. The preview is limited to 200 rows and 50 columns of data, for performance reasons.
+		</setting>
 		<caption id="sort_order"/>
 		<setting id="order">Sort in ascending or descending order.</setting>
 		<setting id="conversion">Allows to apply conversions, which may affect sort order (when sorting data.frames, this option is available for each column to sort by).</setting>
diff --git a/rkward/plugins/data/sort.xml b/rkward/plugins/data/sort.xml
index bdbd9d8..fa24aaa 100644
--- a/rkward/plugins/data/sort.xml
+++ b/rkward/plugins/data/sort.xml
@@ -57,6 +57,7 @@
 					<saveobject id="saveto" label="Object to save to" initial="sorted"/>
 				</row>
 				<stretch/>
+				<preview id="preview" label="Preview" mode="data"/>
 			</tab>
 			<tab id="sort_order" label="Sort order">
 				<radio id="order" label="Sort order">



More information about the rkward-tracker mailing list