[rkward] rkward: Add option to adjust system path. This will often be needed for adding pandoc, for instance.

Thomas Friedrichsmeier null at kde.org
Fri Sep 28 21:28:37 BST 2018


Git commit bfa781da301a33d03f7ebe85e9e0b71c85f5ec82 by Thomas Friedrichsmeier.
Committed on 28/09/2018 at 20:19.
Pushed by tfry into branch 'master'.

Add option to adjust system path. This will often be needed for adding pandoc, for instance.

Note: Commit includes noise from roxygenization.

M  +1    -0    rkward/rbackend/rpackages/rkward/NAMESPACE
M  +22   -0    rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R
M  +48   -0    rkward/settings/rksettingsmoduler.cpp
M  +4    -0    rkward/settings/rksettingsmoduler.h

https://commits.kde.org/rkward/bfa781da301a33d03f7ebe85e9e0b71c85f5ec82

diff --git a/rkward/rbackend/rpackages/rkward/NAMESPACE b/rkward/rbackend/rpackages/rkward/NAMESPACE
index cb6394fa..724ba454 100644
--- a/rkward/rbackend/rpackages/rkward/NAMESPACE
+++ b/rkward/rbackend/rpackages/rkward/NAMESPACE
@@ -53,6 +53,7 @@ export(quartz)
 export(quit)
 export(require)
 export(rk.askYesNo)
+export(rk.adjust.system.path)
 export(rk.assign.preview.data)
 export(rk.call.plugin)
 export(rk.clear.plot.history)
diff --git a/rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R b/rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R
index d4863cb9..d39dad33 100644
--- a/rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R
+++ b/rkward/rbackend/rpackages/rkward/R/rk.utility-functions.R
@@ -177,3 +177,25 @@
 "rk.switch.frontend.language" <- function (LANG="C") {
    .rk.do.plain.call ("switchLanguage", as.character (LANG))
 }
+
+#' Add one or more paths to the filesystem search path used in this session
+#'
+#' Add the given path to to the "PATH" environment variable of the running R session. This
+#' can be useful to make sure external binaries are found by Sys.which. Paths are normalized
+#' before being added, and duplicates are stripped from the path.
+#'
+#' @param add Paths to add. May be missing, in which case the path will no be touched.
+#'
+#' @return A vector of the directories in the file sytem path after the adjustment
+#'
+#' @export
+"rk.adjust.system.path" <- function (add) {
+	if (!missing (add)) {
+		oldpath <- unlist (strsplit(Sys.getenv("PATH"), .Platform$path.sep))
+		newpath <- paste (unlist (unique (c (oldpath, normalizePath (add)))), collapse=.Platform$path.sep)
+		Sys.setenv("PATH"=newpath)
+	}
+
+	# return
+	unlist (strsplit(Sys.getenv("PATH"), .Platform$path.sep))
+}
diff --git a/rkward/settings/rksettingsmoduler.cpp b/rkward/settings/rksettingsmoduler.cpp
index 95b23266..17e12a2c 100755
--- a/rkward/settings/rksettingsmoduler.cpp
+++ b/rkward/settings/rksettingsmoduler.cpp
@@ -57,6 +57,7 @@ bool RKSettingsModuleR::options_checkbounds;
 QString RKSettingsModuleR::options_editor;
 QString RKSettingsModuleR::options_pager;
 QString RKSettingsModuleR::options_further;
+QStringList RKSettingsModuleR::options_addpaths;
 // static constants
 QString RKSettingsModuleR::builtin_editor = "<rkward>";
 // session constants
@@ -207,6 +208,12 @@ RKSettingsModuleR::RKSettingsModuleR (RKSettings *gui, QWidget *parent) : RKSett
 	grid->addWidget (further_input, ++row, 0, 1, 2);
 
 	main_vbox->addStretch ();
+
+	addpaths_selector = new MultiStringSelector (i18n ("Addition search paths for utilities used by R"), this);
+	addpaths_selector->setValues (options_addpaths);
+	connect (addpaths_selector, &MultiStringSelector::listChanged, this, &RKSettingsModuleR::settingChanged);
+	connect (addpaths_selector, &MultiStringSelector::getNewStrings, this, &RKSettingsModuleR::addPaths);
+	main_vbox->addWidget (addpaths_selector);
 }
 
 RKSettingsModuleR::~RKSettingsModuleR() {
@@ -239,6 +246,13 @@ void RKSettingsModuleR::applyChanges () {
 	options_editor = editor_input->currentText ();
 	options_pager = pager_input->currentText ();
 	options_further = further_input->toPlainText ();
+	// normalize system paths before adding
+	QStringList paths = addpaths_selector->getValues ();
+	options_addpaths.clear ();
+	for (int i = 0; i < paths.count (); ++i) {
+		QString path = QDir::cleanPath (paths[i]);
+		if (!options_addpaths.contains (path)) options_addpaths.append (path);
+	}
 
 // apply run time options in R
 	QStringList commands = makeRRunTimeOptionCommands ();
@@ -247,6 +261,31 @@ void RKSettingsModuleR::applyChanges () {
 	}
 }
 
+void RKSettingsModuleR::addPaths(QStringList* string_list) {
+	RK_TRACE (SETTINGS);
+
+	QDialog dialog (this);
+	dialog.setWindowTitle (i18n ("Add System Path Directory"));
+	QVBoxLayout *layout = new QVBoxLayout (&dialog);
+	QLabel *label = new QLabel (i18n ("Specify or select directory to add to the system file path of the running R session"));
+	label->setWordWrap (true);
+	layout->addWidget (label);
+
+	KUrlRequester *req = new KUrlRequester ();
+	req->setMode (KFile::Directory);
+	layout->addWidget (req);
+
+	QDialogButtonBox *buttons = new QDialogButtonBox (QDialogButtonBox::Ok | QDialogButtonBox::Cancel);
+	buttons->button(QDialogButtonBox::Ok)->setText (i18nc ("Add directory to list", "Add"));
+	connect (buttons, &QDialogButtonBox::accepted, &dialog, &QDialog::accept);
+	connect (buttons, &QDialogButtonBox::rejected, &dialog, &QDialog::reject);
+	layout->addWidget (buttons);
+
+	if (dialog.exec () == QDialog::Accepted) {
+		if (!req->text ().isEmpty ()) (*string_list).append (req->text ());
+	}
+}
+
 //static
 QStringList RKSettingsModuleR::makeRRunTimeOptionCommands () {
 	RK_TRACE (SETTINGS);
@@ -271,6 +310,13 @@ QStringList RKSettingsModuleR::makeRRunTimeOptionCommands () {
 	if (options_pager == builtin_editor) list.append ("options (pager=rk.show.files)\n");
 	else list.append ("options (pager=\"" + options_pager + "\")\n");
 	if (!options_further.isEmpty ()) list.append (options_further + '\n');
+	if (!options_addpaths.isEmpty ()) {
+		QString command = "rk.adjust.system.path (add=c(";
+		foreach (const QString &p, options_addpaths) {
+			command.append (RObject::rQuote (p));
+		}
+		list.append (command + "))\n");
+	}
 
 #ifdef __GNUC__
 #	warning TODO make the following options configurable
@@ -306,6 +352,7 @@ void RKSettingsModuleR::saveSettings (KConfig *config) {
 	cg.writeEntry ("editor", options_editor);
 	cg.writeEntry ("pager", options_pager);
 	cg.writeEntry ("further init commands", options_further);
+	cg.writeEntry ("addsyspaths", options_addpaths);
 }
 
 void RKSettingsModuleR::loadSettings (KConfig *config) {
@@ -325,6 +372,7 @@ void RKSettingsModuleR::loadSettings (KConfig *config) {
 	options_editor = cg.readEntry ("editor", builtin_editor);
 	options_pager = cg.readEntry ("pager", builtin_editor);
 	options_further = cg.readEntry ("further init commands", QString ());
+	options_addpaths = cg.readEntry ("addsyspaths", QStringList ());
 }
 
 //#################################################
diff --git a/rkward/settings/rksettingsmoduler.h b/rkward/settings/rksettingsmoduler.h
index d3db9c16..e3f6f367 100644
--- a/rkward/settings/rksettingsmoduler.h
+++ b/rkward/settings/rksettingsmoduler.h
@@ -58,6 +58,8 @@ public:
 	static int getDefaultWidth () { return options_width; };
 public slots:
 	void settingChanged ();
+private slots:
+	void addPaths (QStringList *string_list);
 private:
 	QLineEdit *outdec_input;
 	QSpinBox *width_input;
@@ -72,6 +74,7 @@ private:
 	QComboBox *editor_input;
 	QComboBox *pager_input;
 	QTextEdit *further_input;
+	MultiStringSelector *addpaths_selector;
 
 	static QString options_outdec;
 	static int options_width;
@@ -86,6 +89,7 @@ private:
 	static QString options_editor;
 	static QString options_pager;
 	static QString options_further;
+	static QStringList options_addpaths;
 
 // constants
 	static QString builtin_editor;



More information about the rkward-tracker mailing list