[education/rkward] /: Fix command history R functions

Thomas Friedrichsmeier null at kde.org
Sat May 18 16:06:41 BST 2024


Git commit f7cac74466f0db0fa5af4732b70bd993a58e5169 by Thomas Friedrichsmeier.
Committed on 18/05/2024 at 15:06.
Pushed by tfry into branch 'master'.

Fix command history R functions

M  +3    -0    ChangeLog
M  +14   -0    rkward/autotests/core_test.cpp
M  +2    -1    rkward/rbackend/rkrinterface.cpp
M  +3    -3    rkward/rbackend/rpackages/rkward/R/internal.R

https://invent.kde.org/education/rkward/-/commit/f7cac74466f0db0fa5af4732b70bd993a58e5169

diff --git a/ChangeLog b/ChangeLog
index 700201712..e2387eeb8 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,6 @@
+- Fixed: R functions loadhistory(), savehistory(), and timestamp() broken
+- Changed: R shared library is now loaded, dynamically, rather than linked at compile time.
+           This allows more flexibility on which R version to use at runtime, esp. in the context of AppImage binaries
 - Fixed: References to the package cairoDevice (removed on CRAN) have been replaced with Cairo
 - Fixed: Creating new output window from the UI would sometimes show empty window
 
diff --git a/rkward/autotests/core_test.cpp b/rkward/autotests/core_test.cpp
index c72be779e..6ce30f4f3 100644
--- a/rkward/autotests/core_test.cpp
+++ b/rkward/autotests/core_test.cpp
@@ -19,6 +19,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
 
 #include "../debug.h"
 #include "../rkward.h"
+#include "../rkconsole.h"
 #include "../version.h"
 #include "../agents/rkquitagent.h"
 #include "../rbackend/rksessionvars.h"
@@ -270,6 +271,7 @@ private Q_SLOTS:
 		RInterface::whenAllFinished(this, [](RCommand*) {
 			QCOMPARE(RObjectList::getGlobalEnv()->findObject("x")->getLength(), 1);
 		});
+		cleanGlobalenv();
 	}
 
 	void parseErrorTest() {
@@ -373,6 +375,18 @@ private Q_SLOTS:
 		waitForAllFinished();  // priority_command_done must remain in scope until done
 	}
 
+	void RKConsoleHistoryTest() {
+#		define UNIQUE_STRING "unique_command_string"
+		auto console = RKConsole::mainConsole();
+		console->pipeUserCommand("if (FALSE) " UNIQUE_STRING "()");
+		runCommandWithTimeout(new RCommand("local({x <- tempfile(); savehistory(x); readLines(x)})", RCommand::GetStringVector | RCommand::App), nullptr, [](RCommand *command) {
+			QCOMPARE(command->stringVector().filter(UNIQUE_STRING).size(), 1);
+		});
+		console->pipeUserCommand("timestamp(prefix=\"" UNIQUE_STRING "\"");
+		waitForAllFinished();
+		QCOMPARE(console->commandHistory().filter(UNIQUE_STRING).size(), 2);
+	}
+
 	void RKDeviceTest() {
 		// Well, this test is sort of lame but should at least catch major breakage in RK() device
 		runCommandAsync(new RCommand("demo(graphics, ask=FALSE)", RCommand::User), nullptr, [](RCommand *command) {
diff --git a/rkward/rbackend/rkrinterface.cpp b/rkward/rbackend/rkrinterface.cpp
index 6eabda935..1cf057277 100644
--- a/rkward/rbackend/rkrinterface.cpp
+++ b/rkward/rbackend/rkrinterface.cpp
@@ -582,6 +582,7 @@ void RInterface::pauseProcessing (bool pause) {
 GenericRRequestResult RInterface::processRCallRequest (const QString &call, const QVariant &args, RCommandChain *in_chain) {
 	RK_TRACE (RBACKEND);
 
+	// TODO: All these calls should be mapped to an enum directly in the backend, for efficiency, and to avoid typo-bugs
 	if (call == "sync") {
 		QVariantList al = args.toList();  // added, removed, changed
 		RObjectList::getGlobalEnv()->updateFromR(in_chain, al.value(0).toStringList(), al.value(1).toStringList());
@@ -607,7 +608,7 @@ GenericRRequestResult RInterface::processRCallRequest (const QString &call, cons
 		return GenericRRequestResult();
 	}
 
-	// NOTE: all requests below pass all arguments as single stringlist (although that may worth changing)
+	// NOTE: all requests below pass their arguments as single stringlist (although that may be worth changing)
 	QStringList arglist = args.toStringList();
 	if (false) {  // syntax dummy
 #ifndef DISABLE_RKWINDOWCATCHER
diff --git a/rkward/rbackend/rpackages/rkward/R/internal.R b/rkward/rbackend/rpackages/rkward/R/internal.R
index e4eef247b..395cd3365 100755
--- a/rkward/rbackend/rpackages/rkward/R/internal.R
+++ b/rkward/rbackend/rpackages/rkward/R/internal.R
@@ -328,18 +328,18 @@ assign(".rk.shadow.envs", new.env(parent=emptyenv()), envir=.rk.variables)
 	## History manipulation function (overloads for functions by the same name in package utils)
 	rk.replace.function ("loadhistory",  as.environment ("package:utils"),
 		function (file = ".Rhistory") {
-			invisible(.rk.call("commandHistory", c("set", readLines(file))))
+			invisible(rkward:::.rk.call("commandHistory", c("set", readLines(file))))
 		}, copy.formals = FALSE)
 
 	rk.replace.function ("savehistory",  as.environment ("package:utils"),
 		function (file = ".Rhistory") {
-			invisible(writeLines(.rk.call("commandHistory", "get"), file))
+			invisible(writeLines(rkward:::.rk.call("commandHistory", "get"), file))
 		}, copy.formals = FALSE)
 
 	rk.replace.function ("timestamp",  as.environment ("package:utils"),
 		function (stamp = date(), prefix = "##------ ", suffix = " ------##", quiet = FALSE) {
 			stamp <- paste(prefix, stamp, suffix, sep = "")
-			.rk.call("commandHistory", c("append", stamp))
+			rkward:::.rk.call("commandHistory", c("append", stamp))
 			if (!quiet) cat(stamp, sep = "\n")
 			invisible(stamp)
 		}, copy.formals = FALSE)


More information about the rkward-tracker mailing list