[education/rkward] /: Crosstabs N to N: Simplify labels, add option to control table layout

Thomas Friedrichsmeier null at kde.org
Mon May 23 16:19:51 BST 2022


Git commit f6a7464e7fdebb2b0263117038a9b5e5045ef13a by Thomas Friedrichsmeier.
Committed on 21/05/2022 at 11:09.
Pushed by tfry into branch 'master'.

Crosstabs N to N: Simplify labels, add option to control table layout

M  +2    -0    ChangeLog
M  +7    -3    rkward/plugins/analysis/crosstab_multi.js
M  +1    -0    rkward/plugins/analysis/crosstab_multi.rkh
M  +1    -0    rkward/plugins/analysis/crosstab_multi.xml
M  +2    -0    tests/all_tests.R
M  +3    -4    tests/analysis_plugins/crosstab_multi.rkcommands.R
M  +60   -64   tests/analysis_plugins/crosstab_multi.rkout

https://invent.kde.org/education/rkward/commit/f6a7464e7fdebb2b0263117038a9b5e5045ef13a

diff --git a/ChangeLog b/ChangeLog
index d3228980..51bd3188 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,5 @@
+- Crosstabs N to N: Simplify labels, add option to control table layout
+
 --- Version 0.7.4 - May-30-2022
 - Support "step into" and "finish current" modes in the R debugger window
 - When directly upgrading from very old versions (pre 0.6.3, currently), discard existing config settings
diff --git a/rkward/plugins/analysis/crosstab_multi.js b/rkward/plugins/analysis/crosstab_multi.js
index 494d4c1e..65a4ebf9 100644
--- a/rkward/plugins/analysis/crosstab_multi.js
+++ b/rkward/plugins/analysis/crosstab_multi.js
@@ -1,19 +1,23 @@
 function preprocess () {
 	var vars = trim (getValue ("x")).replace (/\n/g, ",");
 
-	echo ('data <- data.frame (' + vars + ', check.names=FALSE)\n');
-	echo ('datadescription <- paste (rk.get.description (' + vars + '), collapse=", ");\n');
+	echo ('data <- data.frame (rk.list(' + vars + '), check.names=FALSE)\n');
 }
 
 function calculate () {
 	echo ('result <- ftable (data');
+
+	var rows = getValue("rows");
+	if (rows == 0) echo(', row.vars=c()');
+	else echo(', row.vars=1:min(' + rows + ', length(data))');
+
 	if (!getValue ("exclude_nas.state")) echo (", exclude=NULL");
 	echo (');\n');
 }
 
 function printout(is_preview) {
 	if (!is_preview) {
-		new Header (i18n ("Crosstabs (n to n)")).add (i18n ("Variables"), noquote ('datadescription')).print ();
+		new Header (i18n ("Crosstabs (n to n)")).add (i18n ("Variables"), noquote ('paste(names(data), collapse=", ")')).print ();
 		echo ('\n');
 	}
 	echo ('rk.print (result)\n');
diff --git a/rkward/plugins/analysis/crosstab_multi.rkh b/rkward/plugins/analysis/crosstab_multi.rkh
index 84e6f228..04d84112 100644
--- a/rkward/plugins/analysis/crosstab_multi.rkh
+++ b/rkward/plugins/analysis/crosstab_multi.rkh
@@ -13,6 +13,7 @@ For the case of two variables, or when the occurrences of values should be count
 	<settings>
 		<setting id="x">Select a data frame or more than one variables from a data frame or with the same length</setting>
 		<setting id="exclude_nas">Should NA (and NaN) values be excluded (default)? If this option is not checked, missing values are listed in the table just like regular values.</setting>
+		<setting id="rows">Control the "width" of the printed table: Categories of the first n selected variables are listed as rows, the remaining ones as columns.</setting>
 	</settings>
 	<related>
 		<ul>
diff --git a/rkward/plugins/analysis/crosstab_multi.xml b/rkward/plugins/analysis/crosstab_multi.xml
index 402c226c..f6796bf2 100644
--- a/rkward/plugins/analysis/crosstab_multi.xml
+++ b/rkward/plugins/analysis/crosstab_multi.xml
@@ -9,6 +9,7 @@
 			<column>
 				<varslot min_vars="1" multi="true" id="x" source="vars" required="true" label="variables:"/>
 				<checkbox id="exclude_nas" label="Exclude missing values" checked="true"/>
+				<spinbox id="rows" type="integer" min="0" initial="1" label="Row variables"/>
 			</column>
 		</row>
 		<preview id="preview" mode="output"/>
diff --git a/tests/all_tests.R b/tests/all_tests.R
index 6c8bdb51..c4ebdecd 100644
--- a/tests/all_tests.R
+++ b/tests/all_tests.R
@@ -12,3 +12,5 @@ testsuites <- c (
 )
 
 rktest.makeplugintests (testsuites=testsuites, outfile="make_plugintests.txt")
+
+#rktest.makeplugintests (testsuites="analysis_plugins.R", outfile="make_plugintests.txt", test.id="crosstab_multi")
diff --git a/tests/analysis_plugins/crosstab_multi.rkcommands.R b/tests/analysis_plugins/crosstab_multi.rkcommands.R
index 3093d39f..72dd358a 100644
--- a/tests/analysis_plugins/crosstab_multi.rkcommands.R
+++ b/tests/analysis_plugins/crosstab_multi.rkcommands.R
@@ -1,11 +1,10 @@
 local({
 ## Prepare
-data <- data.frame (test_table[["A"]],test_table[["B"]],test_table[["C"]],test_table[["D"]], check.names=FALSE)
-datadescription <- paste (rk.get.description (test_table[["A"]],test_table[["B"]],test_table[["C"]],test_table[["D"]]), collapse=", ");
+data <- data.frame (rk.list(test_table[["A"]],test_table[["B"]],test_table[["C"]],test_table[["D"]]), check.names=FALSE)
 ## Compute
-result <- ftable (data);
+result <- ftable (data, row.vars=1:min(1, length(data)));
 ## Print result
-rk.header ("Crosstabs (n to n)", parameters=list("Variables"=datadescription))
+rk.header ("Crosstabs (n to n)", parameters=list("Variables"=paste(names(data), collapse=", ")))
 
 rk.print (result)
 })
diff --git a/tests/analysis_plugins/crosstab_multi.rkout b/tests/analysis_plugins/crosstab_multi.rkout
index 9f70f44b..bc5f6193 100644
--- a/tests/analysis_plugins/crosstab_multi.rkout
+++ b/tests/analysis_plugins/crosstab_multi.rkout
@@ -10,75 +10,71 @@ DATE<br />
 <tr><td>
 	<table border=0 class=dataframe>
 	<tbody> <tr> 
-<td class=cellinside>                 </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>test_table[["D"]]</td>
-<td class=cellinside>1                </td>
-<td class=cellinside>2                </td> </tr>
+<td class=cellinside> </td>
+<td class=cellinside>B</td>
+<td class=cellinside>1</td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside>2</td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td> </tr>
  <tr> 
-<td class=cellinside>test_table[["A"]]</td>
-<td class=cellinside>test_table[["B"]]</td>
-<td class=cellinside>test_table[["C"]]</td>
-<td class=cellinside>                 </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>                 </td> </tr>
+<td class=cellinside> </td>
+<td class=cellinside>C</td>
+<td class=cellinside>1</td>
+<td class=cellinside> </td>
+<td class=cellinside>2</td>
+<td class=cellinside> </td>
+<td class=cellinside>1</td>
+<td class=cellinside> </td>
+<td class=cellinside>2</td>
+<td class=cellinside> </td> </tr>
  <tr> 
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td> </tr>
+<td class=cellinside> </td>
+<td class=cellinside>D</td>
+<td class=cellinside>1</td>
+<td class=cellinside>2</td>
+<td class=cellinside>1</td>
+<td class=cellinside>2</td>
+<td class=cellinside>1</td>
+<td class=cellinside>2</td>
+<td class=cellinside>1</td>
+<td class=cellinside>2</td> </tr>
  <tr> 
-<td class=cellinside>                 </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>2                </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td> </tr>
+<td class=cellinside>A</td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td>
+<td class=cellinside> </td> </tr>
  <tr> 
-<td class=cellinside>                 </td>
-<td class=cellinside>2                </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td> </tr>
+<td class=cellinside>1</td>
+<td class=cellinside> </td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td> </tr>
  <tr> 
-<td class=cellinside>                 </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>2                </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td> </tr>
- <tr> 
-<td class=cellinside>2                </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>0                </td>
-<td class=cellinside>1                </td> </tr>
- <tr> 
-<td class=cellinside>                 </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>2                </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td> </tr>
- <tr> 
-<td class=cellinside>                 </td>
-<td class=cellinside>2                </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td> </tr>
- <tr> 
-<td class=cellinside>                 </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>2                </td>
-<td class=cellinside>                 </td>
-<td class=cellinside>1                </td>
-<td class=cellinside>1                </td> </tr>
+<td class=cellinside>2</td>
+<td class=cellinside> </td>
+<td class=cellinside>0</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td>
+<td class=cellinside>1</td> </tr>
  
 	</tbody>
 </table>



More information about the rkward-tracker mailing list