[kde-doc-english] [rkward] /: Allow row-wise extraction of values from matrix-element

Thomas Friedrichsmeier thomas.friedrichsmeier at ruhr-uni-bochum.de
Thu Jan 14 08:42:46 UTC 2016


Git commit f2c168f2c22be434a6b4003102c63424a0536c13 by Thomas Friedrichsmeier.
Committed on 14/01/2016 at 08:42.
Pushed by tfry into branch 'master'.

Allow row-wise extraction of values from matrix-element

M  +1    -0    ChangeLog
M  +4    -0    doc/rkwardplugins/index.docbook
M  +27   -8    rkward/plugin/rkmatrixinput.cpp
M  +2    -1    rkward/plugin/rkmatrixinput.h

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

diff --git a/ChangeLog b/ChangeLog
index a4e601e..97b7504 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,4 @@
+- Add ability to extract <matrix> values row-wise in plugins
 - Add convenience JS-function "makeOption()" for use in plugins
 - Fixed: Numerical (display) precision setting was not honored in data editor
 - Fix several window activation quirks in "Focus follows mouse" mode
diff --git a/doc/rkwardplugins/index.docbook b/doc/rkwardplugins/index.docbook
index bb5a9bf..b37deba 100644
--- a/doc/rkwardplugins/index.docbook
+++ b/doc/rkwardplugins/index.docbook
@@ -3902,6 +3902,10 @@ Child elements <true>, <false>, <case>, and <default> ta
 			<listitem><para>The data from a single column (0 for leftmost column). <function>getValue()</function>/<function>getString()</function> returns this as a single string, separated by "\n". However, the recommended way to get this is using <function>getList()</function>, which returns this column as an array of strings.</para></listitem>
 		</varlistentry>
 		<varlistentry>
+			<term>row.0,row.1,row.2...</term>
+			<listitem><para>The data from a single row (0 for topmost row). <function>getValue()</function>/<function>getString()</function> returns this as a single string, separated by "\n". However, the recommended way to get this is using <function>getList()</function>, which returns this column as an array of strings.</para></listitem>
+		</varlistentry>
+		<varlistentry>
 			<term>cbind</term>
 			<listitem><para>Data in a format suitable for pasting to R, wrapped in a cbind statement (string; read-only).</para></listitem>
 		</varlistentry>
diff --git a/rkward/plugin/rkmatrixinput.cpp b/rkward/plugin/rkmatrixinput.cpp
index 1ff8e35..434544b 100644
--- a/rkward/plugin/rkmatrixinput.cpp
+++ b/rkward/plugin/rkmatrixinput.cpp
@@ -2,7 +2,7 @@
                           rkmatrixinput  -  description
                              -------------------
     begin                : Tue Oct 09 2012
-    copyright            : (C) 2012, 2015 by Thomas Friedrichsmeier
+    copyright            : (C) 2012-2016 by Thomas Friedrichsmeier
     email                : thomas.friedrichsmeier at kdemail.net
  ***************************************************************************/
 
@@ -128,6 +128,12 @@ QVariant RKMatrixInput::value (const QString& modifier) {
 			ret.append ("\tc (" + makeColumnString (i, ", ") + ')');
 		}
 		return QString ("cbind (\n" + ret.join (",\n") + "\n)");
+	} else if (modifier.startsWith ("row.")) {
+		bool ok;
+		int row = modifier.mid (4).toInt (&ok);
+		if ((row >= 0) && ok) {
+			return (rowStrings (row));
+		}
 	}
 
 	bool ok;
@@ -212,6 +218,12 @@ void RKMatrixInput::updateColumn (int column) {
 	updateAll ();
 }
 
+QString pasteableValue (const QString &in, bool string) {
+	if (string) return (RObject::rQuote (in));
+	else if (in.isEmpty ()) return ("NA");
+	else return in;
+}
+
 QString RKMatrixInput::makeColumnString (int column, const QString& sep, bool r_pasteable) {
 	RK_TRACE (PLUGIN);
 
@@ -224,13 +236,20 @@ QString RKMatrixInput::makeColumnString (int column, const QString& sep, bool r_
 	for (int i = 0; i < row_count->intValue (); ++i) {
 		if (i > 0) ret.append (sep);
 		const QString val = storage.value (i);
-		if (r_pasteable) {
-			if (mode == String) ret.append (RObject::rQuote (val));
-			else if (val.isEmpty ()) ret.append ("NA");
-			else ret.append (val);
-		} else {
-			ret.append (val);
-		}
+		if (r_pasteable) ret.append (pasteableValue (val, mode == String));
+		else ret.append (val);
+	}
+	return ret;
+}
+
+QStringList RKMatrixInput::rowStrings (int row) {
+	RK_TRACE (PLUGIN);
+
+	QStringList ret;
+	ret.reserve (column_count->intValue ());
+	for (int i = 0; i < column_count->intValue (); ++i) {
+		if (i < columns.size ()) ret.append (columns[i].storage.value (row));
+		else ret.append (QString ());
 	}
 	return ret;
 }
diff --git a/rkward/plugin/rkmatrixinput.h b/rkward/plugin/rkmatrixinput.h
index 541883e..4f1bdad 100644
--- a/rkward/plugin/rkmatrixinput.h
+++ b/rkward/plugin/rkmatrixinput.h
@@ -2,7 +2,7 @@
                           rkmatrixinput  -  description
                              -------------------
     begin                : Tue Oct 09 2012
-    copyright            : (C) 2012 by Thomas Friedrichsmeier
+    copyright            : (C) 2012-2016 by Thomas Friedrichsmeier
     email                : thomas.friedrichsmeier at kdemail.net
  ***************************************************************************/
 
@@ -78,6 +78,7 @@ private:
 	void updateColumn (int column);
 	bool expandStorageForColumn (int column);
 	QString makeColumnString (int column, const QString& sep, bool r_pasteable = true);
+	QStringList rowStrings (int row);
 	bool isColumnValid (int column);
 
 	bool is_valid;


More information about the kde-doc-english mailing list