[education/rkward] /: Add rk.with.progress for progress feedback on lenghty operations in plugins

Thomas Friedrichsmeier null at kde.org
Thu Aug 8 20:55:21 BST 2024


Git commit 1e22d785ca261e9e912e7de2880835e83cbe77db by Thomas Friedrichsmeier.
Committed on 08/08/2024 at 19:55.
Pushed by tfry into branch 'master'.

Add rk.with.progress for progress feedback on lenghty operations in plugins

M  +1    -1    VERSION.cmake
M  +7    -0    rkward/rbackend/rkrinterface.cpp
M  +3    -2    rkward/rbackend/rpackages/rkward/DESCRIPTION
M  +1    -0    rkward/rbackend/rpackages/rkward/NAMESPACE
A  +56   -0    rkward/rbackend/rpackages/rkward/R/rk.with.progress.R
M  +1    -1    rkward/rbackend/rpackages/rkward/man/DeviceOverrides.Rd
A  +41   -0    rkward/rbackend/rpackages/rkward/man/rk.with.progress.Rd
M  +0    -7    rkward/rbackend/rpackages/rkwardtests/man/rkwardtests-package.Rd

https://invent.kde.org/education/rkward/-/commit/1e22d785ca261e9e912e7de2880835e83cbe77db

diff --git a/VERSION.cmake b/VERSION.cmake
index 64f36e67c..5248b8f99 100644
--- a/VERSION.cmake
+++ b/VERSION.cmake
@@ -1,3 +1,3 @@
 # DO NOT CHANGE THIS FILE MANUALLY!
 # It will be overwritten by scripts/set_dist_version.sh
-SET(RKVERSION_NUMBER 0.8.0z+0.8.1+devel1)
+SET(RKVERSION_NUMBER 0.8.0z+0.8.1+devel2)
diff --git a/rkward/rbackend/rkrinterface.cpp b/rkward/rbackend/rkrinterface.cpp
index dde6812fc..562658f64 100644
--- a/rkward/rbackend/rkrinterface.cpp
+++ b/rkward/rbackend/rkrinterface.cpp
@@ -37,6 +37,7 @@ SPDX-License-Identifier: GPL-2.0-or-later
 #include "../misc/rkcommonfunctions.h"
 #include "../misc/rkmessagecatalog.h"
 #include "../misc/rkoutputdirectory.h"
+#include "../misc/rkprogresscontrol.h"
 #include "rksessionvars.h"
 #include "../windows/rkwindowcatcher.h"
 
@@ -800,6 +801,12 @@ GenericRRequestResult RInterface::processRCallRequest (const QString &call, cons
 		RKDebugHandler::instance ()->endDebug();
 	} else if (call == "switchLanguage") {
 		RKMessageCatalog::switchLanguage(arglist.value(0));
+	} else if (call == "with.progress") {
+		auto dialog = new RKProgressControl(this, arglist.value(0), QString(), RKProgressControl::CancellableProgress | RKProgressControl::OutputShownByDefault);
+		auto command = new RCommand("rkward:::.rk.with.progress.eval()", RCommand::App);
+		dialog->addRCommand(command, true);
+		issueCommand(command, in_chain);
+		dialog->doNonModal(true);
 	} else {
 		return GenericRRequestResult::makeError(i18n("Error: unrecognized request '%1'", call));
 	}
diff --git a/rkward/rbackend/rpackages/rkward/DESCRIPTION b/rkward/rbackend/rpackages/rkward/DESCRIPTION
index adba1ce85..e84aa9447 100755
--- a/rkward/rbackend/rpackages/rkward/DESCRIPTION
+++ b/rkward/rbackend/rpackages/rkward/DESCRIPTION
@@ -14,8 +14,8 @@ Encoding: UTF-8
 LazyLoad: yes
 Authors at R: c(person(given="Thomas", family="Friedrichsmeier", email="thomas.friedrichsmeier at kdemail.net", role=c("aut")), person(given="the RKWard", family="team",
                     email="rkward-devel at kde.org", role=c("cre","aut")))
-Version: 0.8.0
-Date: 2024-07-25
+Version: 0.8.1
+Date: 2024-08-02
 RoxygenNote: 7.3.2
 Collate: 
     'base_overrides.R'
@@ -39,6 +39,7 @@ Collate:
     'rk.sessionInfo.R'
     'rk.sync-functions.R'
     'rk.utility-functions.R'
+    'rk.with.progress.R'
     'rk.workspace-functions.R'
     'rkward-package.R'
     'ver.R'
diff --git a/rkward/rbackend/rpackages/rkward/NAMESPACE b/rkward/rbackend/rpackages/rkward/NAMESPACE
index de67aaafc..a139ecb86 100644
--- a/rkward/rbackend/rpackages/rkward/NAMESPACE
+++ b/rkward/rbackend/rpackages/rkward/NAMESPACE
@@ -122,6 +122,7 @@ export(rk.sync.global)
 export(rk.tempdir)
 export(rk.toggle.plot.history)
 export(rk.verify.plot.hist.limits)
+export(rk.with.progress)
 export(rk.without.plot.history)
 export(setwd)
 export(win.graph)
diff --git a/rkward/rbackend/rpackages/rkward/R/rk.with.progress.R b/rkward/rbackend/rpackages/rkward/R/rk.with.progress.R
new file mode 100644
index 000000000..17757c746
--- /dev/null
+++ b/rkward/rbackend/rpackages/rkward/R/rk.with.progress.R
@@ -0,0 +1,56 @@
+# - This file is part of the RKWard project (https://rkward.kde.org).
+# SPDX-FileCopyrightText: by Thomas Friedrichsmeier <thomas.friedrichsmeier at kdemail.net>
+# SPDX-FileContributor: The RKWard Team <rkward-devel at kde.org>
+# SPDX-License-Identifier: GPL-2.0-or-later
+#' Run a command with an associated progress dialog
+#'
+#' \code{rk.with.progress()} evaluates the given R expression, while showing a UI dialog to monitor the progress, and allowing the user
+#' to cancel the operation.
+#'
+#' @param expr Command to run
+#' @param text Short user-visible explanation of what is being done. E.g. "Downloading XY..."
+#'
+#' @return Returns the result of evaluating the expression. The result is always returned invisibly.
+#'
+#' @note This function should be used sparingly. For code designed to be run in the R Console window, or as part of other R functions, it
+#'        should be omitted. Rather it should only be used for top-level operations where progress is not immediately visible. A prime use case
+#'        may be a plugin that performs a large download.
+#' 
+#' @author Thomas Friedrichsmeier \email{rkward-devel@@kde.org}
+#' 
+#' @keywords utilities
+#'
+#' @examples
+#' \dontrun{
+#' rk.with.progress({
+#'    for (i in 1:80) {
+#'       cat('=')
+#'       Sys.sleep(.1)
+#'    }
+#'    cat('\n')
+#' }, "Waiting for nothing...")
+#' }
+#'
+#' @export
+rk.with.progress <- function(expr, text="") {
+	.rk.variables$with.progress.ret <- NULL
+	.rk.variables$with.progress.expr <- substitute(expr)
+	.rk.variables$with.progress.env <- parent.frame()
+	.rk.call.nested("with.progress", text)
+	.rk.variables$with.progress.expr <- NULL
+	.rk.variables$with.progress.env <- NULL
+	ret <- .rk.variables$with.progress.ret
+	if (!is.null(.rk.variables$with.progress.err)) stop(.rk.variables$with.progress.err)
+	.rk.variables$with.progress.ret <- NULL
+	invisible(ret)
+}
+
+# Used internally for rk.with.progress
+.rk.with.progress.eval <- function() {
+	.rk.variables$with.progress.err <- NULL
+	.rk.variables$with.progress.ret <- tryCatch(
+		expr=eval(.rk.variables$with.progress.expr, .rk.variables$with.progress.env),
+		error=function(e) { .rk.variables$with.progress.err <- e }
+	)
+	invisible(NULL)
+}
diff --git a/rkward/rbackend/rpackages/rkward/man/DeviceOverrides.Rd b/rkward/rbackend/rpackages/rkward/man/DeviceOverrides.Rd
index ee47975ab..261550396 100644
--- a/rkward/rbackend/rpackages/rkward/man/DeviceOverrides.Rd
+++ b/rkward/rbackend/rpackages/rkward/man/DeviceOverrides.Rd
@@ -131,7 +131,7 @@ quartz(
     \code{"nbcairo"} or \code{"dbcairo"}.  Only the first will be
     available if the system was compiled without support for
     cairographics.  The default is \code{"cairo"} where \R was built
-    using \code{pangocairo} (often not the case on macOS), otherwise
+    using \code{pangocairo} (so not usually on macOS), otherwise
     \code{"Xlib"}.}
 
 \item{antialias}{for cairo types, the type of anti-aliasing (if any)
diff --git a/rkward/rbackend/rpackages/rkward/man/rk.with.progress.Rd b/rkward/rbackend/rpackages/rkward/man/rk.with.progress.Rd
new file mode 100644
index 000000000..a664902ab
--- /dev/null
+++ b/rkward/rbackend/rpackages/rkward/man/rk.with.progress.Rd
@@ -0,0 +1,41 @@
+% Generated by roxygen2: do not edit by hand
+% Please edit documentation in R/rk.with.progress.R
+\name{rk.with.progress}
+\alias{rk.with.progress}
+\title{Run a command with an associated progress dialog}
+\usage{
+rk.with.progress(expr, text = "")
+}
+\arguments{
+\item{expr}{Command to run}
+
+\item{text}{Short user-visible explanation of what is being done. E.g. "Downloading XY..."}
+}
+\value{
+Returns the result of evaluating the expression. The result is always returned invisibly.
+}
+\description{
+\code{rk.with.progress()} evaluates the given R expression, while showing a UI dialog to monitor the progress, and allowing the user
+to cancel the operation.
+}
+\note{
+This function should be used sparingly. For code designed to be run in the R Console window, or as part of other R functions, it
+       should be omitted. Rather it should only be used for top-level operations where progress is not immediately visible. A prime use case
+       may be a plugin that performs a large download.
+}
+\examples{
+\dontrun{
+rk.with.progress({
+   for (i in 1:80) {
+      cat('=')
+      Sys.sleep(.1)
+   }
+   cat('\n')
+}, "Waiting for nothing...")
+}
+
+}
+\author{
+Thomas Friedrichsmeier \email{rkward-devel at kde.org}
+}
+\keyword{utilities}
diff --git a/rkward/rbackend/rpackages/rkwardtests/man/rkwardtests-package.Rd b/rkward/rbackend/rpackages/rkwardtests/man/rkwardtests-package.Rd
index f158731dd..28531fd7d 100644
--- a/rkward/rbackend/rpackages/rkwardtests/man/rkwardtests-package.Rd
+++ b/rkward/rbackend/rpackages/rkwardtests/man/rkwardtests-package.Rd
@@ -22,13 +22,6 @@ License: \tab GPL (>= 2)\cr
 LazyLoad: \tab yes\cr
 URL: \tab https://rkward.kde.org\cr
 }
-}
-\seealso{
-Useful links:
-\itemize{
-  \item \url{https://rkward.kde.org}
-}
-
 }
 \author{
 Thomas Friedrichsmeier, Meik Michalke



More information about the rkward-tracker mailing list