[education/rkward] /: Add a generic import plugin based on the "rio" package

Thomas Friedrichsmeier null at kde.org
Sun Apr 24 21:09:37 BST 2022


Git commit 1350911b80ada1f66e769dc9c4cc437d55908b4e by Thomas Friedrichsmeier.
Committed on 24/04/2022 at 20:09.
Pushed by tfry into branch 'master'.

Add a generic import plugin based on the "rio" package

M  +1    -0    ChangeLog
A  +42   -0    rkward/plugins/00saveload/import/import_generic_rio.js
A  +32   -0    rkward/plugins/00saveload/import/import_generic_rio.rkh
A  +33   -0    rkward/plugins/00saveload/import/import_generic_rio.xml
M  +4    -1    rkward/plugins/import_export.pluginmap
M  +3    -3    tests/all_tests.R
M  +7    -0    tests/import_export_plugins.R
A  +3    -0    tests/import_export_plugins/import_generic_rio.messages.txt
A  +10   -0    tests/import_export_plugins/import_generic_rio.rkcommands.R
A  +120  -0    tests/import_export_plugins/import_generic_rio.rkout

https://invent.kde.org/education/rkward/commit/1350911b80ada1f66e769dc9c4cc437d55908b4e

diff --git a/ChangeLog b/ChangeLog
index 1714f9e8..607c8d2d 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+- Add "rio"-based generic import plugin
 - Fixed: Installation from dialog would fail for R packages requiring the rkward package
 - Allow separate analysis by grouping factors in Analysis->Descriptive Statistics
 - Rework help start page to serve as a dashboard for common tasks, replacing the old startup dialog
diff --git a/rkward/plugins/00saveload/import/import_generic_rio.js b/rkward/plugins/00saveload/import/import_generic_rio.js
new file mode 100644
index 00000000..39bb3394
--- /dev/null
+++ b/rkward/plugins/00saveload/import/import_generic_rio.js
@@ -0,0 +1,42 @@
+include("convert_encoding.js");
+
+function preprocess() {
+	echo('require(rio)\n');
+	makeEncodingPreprocessCode();
+}
+
+function preview() {
+	preprocess();
+	calculate(true);
+}
+
+function calculate(is_preview) {
+	var which = getString("which");
+	if (which != "") which = ', which=' + which;
+	var import_list = getBoolean("import_list");
+	var object = getValue("saveto");
+
+	if (import_list) {
+		echo('data <- import_list("' + getValue("file") + '")\n');
+	} else {
+		echo('data <- import("' + getValue("file") + '"' + which + ')\n');
+	}
+
+	makeEncodingCall('data');
+	if (is_preview) {
+		if (import_list) {
+			echo('data <- data[[1]]');
+		}
+		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")) {
+			echo('rk.edit(.GlobalEnv$' + object + ')\n');
+		}
+	}
+}
+
+function printout () {
+	new Header (i18n ("Genneric data import")).addFromUI ("file").addFromUI ("saveto").print ();
+}
+
diff --git a/rkward/plugins/00saveload/import/import_generic_rio.rkh b/rkward/plugins/00saveload/import/import_generic_rio.rkh
new file mode 100644
index 00000000..6de7eb8f
--- /dev/null
+++ b/rkward/plugins/00saveload/import/import_generic_rio.rkh
@@ -0,0 +1,32 @@
+<!DOCTYPE rkhelp>
+<document>
+	<snippets>
+		<include file="convert_encoding.xml"/>
+	</snippets>
+	<summary>
+Import a variety of formats. This is a generic approach based on the "rio" R package, which in turn depends on a variety of further packages.
+If there is a dedicated plugin for the format you wish to import, using that will usually be preferrable, as it will provide more specific options, and with less packages to install.
+	</summary>
+
+	<usage>
+Select the data file to import. An R object containing the data will be created. This will usually be a data.frame, or a list.
+	</usage>
+
+	<settings>
+		<caption id="tab_general"/>
+		<setting id="file">The filename of the file to import</setting>
+		<setting id="saveto">The name of an R object to store the imported data in. If you choose an existing symbol name, you will be prompted, whether to overwrite this object.</setting>
+		<setting id="doedit">Whether the object should be opened for editing after it was imported. Currently, this is only possible, if the object is saved as a data.frame (see next option).</setting>
+		<setting id="import_list">Some data files may contain more than one set of data (e.g. several sheets inside one excel file). Check this option to import all datasets as a list.</setting>
+		<setting id="which">For datasets containing more than one set of data, specify which set to import (this will usually be a name or a number).</setting>
+		<setting id="preview">Preview imported data. Only the first 50 rows and columns are shown, for performance reasons (and only the first dataset in case of multiple sets in the file).</setting>
+		<insert snippet="encoding_doc"/>
+	</settings>
+	<related>
+		<ul>
+			<li><link href="rkward://rhelp/rio/import"/></li>
+			<li><link href="rkward://rhelp/rio/import_list"/></li>
+			<li><link href="rkward://rhelp/iconv"/></li>
+		</ul>
+	</related>
+</document>
diff --git a/rkward/plugins/00saveload/import/import_generic_rio.xml b/rkward/plugins/00saveload/import/import_generic_rio.xml
new file mode 100644
index 00000000..29329ee1
--- /dev/null
+++ b/rkward/plugins/00saveload/import/import_generic_rio.xml
@@ -0,0 +1,33 @@
+<!DOCTYPE rkplugin>
+<document>
+	<code file="import_generic_rio.js" />
+	<help file="import_generic_rio.rkh" />
+	<snippets>
+		<include file="convert_encoding.xml"/>
+	</snippets>
+	<logic>
+		<external id="filename"/>
+		<connect governor="filename" client="file.selection"/>
+		<connect governor="import_all.state.not" clinet="which.enabled"/>
+		<insert snippet="encoding_logic"/>
+	</logic>
+	<dialog label="Generic data import">
+		<tabbook>
+			<tab id="tab_general" label="General">
+				<browser type="file" id="file" label="File name" filter="*"/>
+				<stretch/>
+				<row>
+					<saveobject id="saveto" initial="imported.data" label="Object to save to"/>
+					<checkbox id="doedit" value="1" value_unchecked="0" label="Edit Object" checked="true" />
+				</row>
+				<stretch/>
+				<frame label="If the file contains more than one dataset">
+					<checkbox id="import_list" checked="false" label="Import a list of all datasets"/>
+					<input label="Import specific sub-set" id="which"/>
+				</frame>
+				<preview id="preview" active="true" mode="data" label="Preview"/>
+			</tab>
+			<insert snippet="encoding_tab"/>
+		</tabbook>
+	</dialog>
+</document>
diff --git a/rkward/plugins/import_export.pluginmap b/rkward/plugins/import_export.pluginmap
index bb8a2740..30772565 100644
--- a/rkward/plugins/import_export.pluginmap
+++ b/rkward/plugins/import_export.pluginmap
@@ -25,7 +25,7 @@
 		<component type="standard" id="save_skeleton" file="save/skeleton/description.xml" label="Create package skeleton" />
 		<component type="standard" id="save_variables" file="save/write.xml" label="Export vector or matrix data" />
 		<component type="standard" id="save_csv" file="save/write_csv.xml" label="Export Table / CSV files" />
-    <component type="standard" id="save_rmarkdown" file="save/ExportRMarkdown.xml" label="Export RMarkdown" />
+		<component type="standard" id="save_rmarkdown" file="save/ExportRMarkdown.xml" label="Export RMarkdown" />
 
 		<component type="standard" id="import_spss" file="import/import_spss.xml" label="Import SPSS">
 			<attribute id="format" value="*.sav *.por" label="SPSS data files"/>
@@ -43,6 +43,8 @@
 			<attribute id="format" value="*.xls *.xlsx" label="Microsoft Excel files (Java)"/>
 		</component>
 
+		<component type="standard" id="import_generic_rio" file="import/import_generic_rio.xml" label="Generic import (rio-based)" />
+
 		<component type="standard" id="setworkdir" file="setworkdir.xml" label="Set Working Directory" />
 	</components>
 
@@ -55,6 +57,7 @@
 					<entry component="import_csv"/>
 					<entry component="import_xls"/>
 					<entry component="import_xls_xlconnect"/>
+					<entry component="import_generic_rio"/>
 				</menu>
 				<entry component="load_r_object" />
 				<entry component="load_source" />
diff --git a/tests/all_tests.R b/tests/all_tests.R
index 0a5d287d..6c8bdb51 100644
--- a/tests/all_tests.R
+++ b/tests/all_tests.R
@@ -2,10 +2,10 @@ require (rkwardtests)
 
 ## add your test suite files, to this vector:
 testsuites <- c (
-	"rkward_application_tests.R"
-	"import_export_plugins.R"
+	"rkward_application_tests.R",
+	"import_export_plugins.R",
 	"item_response_theory.R",
-	"analysis_plugins.R"
+	"analysis_plugins.R",
 	"distributions.R",
 	"plots.R",
 	"data_plugin_tests.R"
diff --git a/tests/import_export_plugins.R b/tests/import_export_plugins.R
index 690361af..331398ca 100644
--- a/tests/import_export_plugins.R
+++ b/tests/import_export_plugins.R
@@ -56,6 +56,13 @@ suite <- new ("RKTestSuite", id="import_export_plugins",
 			# WARNING: TODO: We don't use the value labels of the third
 			# variable, yet.
 		}, libraries=c("foreign"), files=c("../import_export_plugins_testfile.sav")),
+		new ("RKTest", id="import_generic_rio", call=function () {
+			rk.call.plugin ("rkward::import_generic_rio", do_locale_conversion.state="0", doedit.state="0", file.selection=file.path (getwd(), "import_export_plugins_testfile.sav"), saveto.objectname="my.rio.data", use_labels.state="1", submit.mode="submit")
+
+			# In order to check, whether the import was correct
+			rk.print (my.rio.data)
+			for (var in my.rio.data) rk.print (rk.get.description(var))
+		}, libraries=c("foreign"), files=c("../import_export_plugins_testfile.sav")),
 		new ("RKTest", id="import_stata", call=function () {
 			rk.call.plugin ("rkward::import_stata", convert_dates.state="1", convert_factors.state="1", convert_underscore.state="0", do_locale_conversion.state="1", doedit.state="0", encoding.string="ISO8859-1", file.selection=file.path (getwd(), "import_export_plugins_testfile.dta"), missing_type.state="0", saveto.objectname="my.stata.data", saveto.parent=".GlobalEnv", submit.mode="submit")
 
diff --git a/tests/import_export_plugins/import_generic_rio.messages.txt b/tests/import_export_plugins/import_generic_rio.messages.txt
new file mode 100644
index 00000000..f7858fc0
--- /dev/null
+++ b/tests/import_export_plugins/import_generic_rio.messages.txt
@@ -0,0 +1,3 @@
+Loading required package: rio
+The following rio suggested packages are not installed: 'arrow', 'feather', 'fst', 'hexView', 'pzfx', 'readODS', 'rmatio'
+Use 'install_formats()' to install them
diff --git a/tests/import_export_plugins/import_generic_rio.rkcommands.R b/tests/import_export_plugins/import_generic_rio.rkcommands.R
new file mode 100644
index 00000000..2b171c7c
--- /dev/null
+++ b/tests/import_export_plugins/import_generic_rio.rkcommands.R
@@ -0,0 +1,10 @@
+local({
+## Prepare
+require(rio)
+## Compute
+data <- import("PATH/import_export_plugins_testfile.sav")
+.GlobalEnv$my.rio.data <- data  # assign to globalenv()
+## Print result
+rk.header ("Genneric data import", parameters=list("File name"="PATH/import_export_plugins_testfile.sav",
+	"Object to save to"="my.rio.data"))
+})
diff --git a/tests/import_export_plugins/import_generic_rio.rkout b/tests/import_export_plugins/import_generic_rio.rkout
new file mode 100644
index 00000000..72c73242
--- /dev/null
+++ b/tests/import_export_plugins/import_generic_rio.rkout
@@ -0,0 +1,120 @@
+<h1>Genneric data import</h1>
+<h2>Parameters</h2>
+<ul><li>File name: PATH/import_export_plugins_testfile.sav</li>
+<li>Object to save to: my.rio.data</li>
+</ul>
+DATE<br />
+
+
+<p align= center >
+<table cellspacing=0 border=1>
+<caption align=bottom class=captiondataframe></caption>
+<tr><td>
+	<table border=0 class=dataframe>
+	<tbody> 
+	<tr class= firstline > 
+		<th>   </th>
+		<th>A_long_variable_name  </th>
+		<th>äöüßéÈ  </th>
+		<th>Value_labels</th> 
+	</tr> 
+<tr> 
+<td class=firstcolumn>1
+</td>
+<td class=cellinside> 1
+</td>
+<td class=cellinside>1.1
+</td>
+<td class=cellinside> 1
+</td></tr>
+ 
+<tr> 
+<td class=firstcolumn>2
+</td>
+<td class=cellinside> 
+</td>
+<td class=cellinside>1.2
+</td>
+<td class=cellinside> 2
+</td></tr>
+ 
+<tr> 
+<td class=firstcolumn>3
+</td>
+<td class=cellinside> 3
+</td>
+<td class=cellinside>1.3
+</td>
+<td class=cellinside> 3
+</td></tr>
+ 
+<tr> 
+<td class=firstcolumn>4
+</td>
+<td class=cellinside> 
+</td>
+<td class=cellinside>1.4
+</td>
+<td class=cellinside> 4
+</td></tr>
+ 
+<tr> 
+<td class=firstcolumn>5
+</td>
+<td class=cellinside> 5
+</td>
+<td class=cellinside>1.5
+</td>
+<td class=cellinside> 5
+</td></tr>
+ 
+<tr> 
+<td class=firstcolumn>6
+</td>
+<td class=cellinside> 6
+</td>
+<td class=cellinside> NA
+</td>
+<td class=cellinside> 5
+</td></tr>
+ 
+<tr> 
+<td class=firstcolumn>7
+</td>
+<td class=cellinside> 7
+</td>
+<td class=cellinside>1.6
+</td>
+<td class=cellinside> 
+</td></tr>
+ 
+<tr> 
+<td class=firstcolumn>8
+</td>
+<td class=cellinside> 7
+</td>
+<td class=cellinside>1.7
+</td>
+<td class=cellinside> 5
+</td></tr>
+ 
+<tr> 
+<td class=firstcolumn>9
+</td>
+<td class=cellinside> 8
+</td>
+<td class=cellinside>1.8
+</td>
+<td class=cellinside> 0
+</td></tr>
+ 
+	</tbody>
+</table>
+ </td></table>
+ <br>
+
+<p class='character'>var</p>
+
+<p class='character'>var</p>
+
+<p class='character'>var</p>


More information about the rkward-tracker mailing list