3Liz

Open your GIS

QGIS Processing R 3

Tue 22 June 2021

fr, en

QGIS Processing R plugin version 3.0

The QGIS-processing-R plugin, let's you write R scripts to be processed by/in QGIS

The QGIS-processing-R plugin was developed after some refactoring of teh qgis processing in QGIS 3. North Road adapted and reused some of the QGIS 2 code to make this happen in QGIS 3.

A new major version was released. This version contains fixes developed by 3liz for Ifremer's Sextant platform.

Since version 2.0.0 of QGIS-processing-R, we've fixed mostly output related bugs :

The major addenda to version 3.0.0 are based around description of the algorithms:

Better Scripts documentation

Since version 3.16 of qgis, it is possible to define help for the parameter of an algorithm. This feature is available for plugins providing algorithm, python scripts and processing models.

Version 3.0.0 of the R processing qgis plugin, parameter help can be defined in the help file for said scripts. The help file is a json file. The name of the parameter is the key to define its help.

Let's look at an example for an algorithm whose parameters and polyg and RPLOTS, the help file would contain the following keys:

{
  "ALG_DESC": "Test help.",  // Algorithm's description
  "ALG_CREATOR": "Me",  // Algorithm's creator
  "polyg": "A polygon layer", // polyg parameter help
  "RPLOTS": "Plot output", // RPLOTS parameter help
  "ALG_HELP_CREATOR": "Me2" // Algorithm help's creator
}

The help for parameter will appear in the form of a tooltip on the input field in the window of the algorithm. It will be used as a description for the parameters in the algorithm published as an OGC Web Processing Service WPS with py-qgis-wps.

Defining the algorithm title

The title of a R script algorithm is defined like so by the plugin:

  1. The first possible title is based on the name of the R script file _ are replaced by spaces. The file name also serves as an ID for the algorithm.
  2. The second possible title is read from the metadata (lines starting with ## ) name, as above the _ are replaced by spaces. The name metadata serves as an ID for the algorithm.
  3. THe third and last possibility is the display_name metadata entry. The ID will either be deducted from the file name or the name metadata.

With version 3.0.0 of the R processing qgis plugin, you can clearly identify the algorithm tile and its ID.

Advanced parameter description

Parameter description in a R based algorithm rely on metadata. These lines start with ##.

Parameter description use the structure developed by Victor Olaya in the QGIS 2 of the processing module. A parameter is explained with the following metadata sequence param_name=[optional] param_type [default_value/values/from_variable]. This structure relies on the asScritCode / fromScriptcode from the processing class parameters (all the children classes of QgsProcessingParameterDefinition).

Entries can look like this

The R script test_algorithm_2.rsx let you test the plugin and will properly analyse parameter descriptions. It also contains other parameter examples.

There is one issue with this compact description of parameters : it's incomplete. For instance, you can't add a description, or an help entry.

To fix this, we decided to follow what other processing modules for QGIS were particularly : GDAL, GRASS7, SAGA and Orpheo Toolbox.

Parameters are described as such QgsProcessingParameter|name|description|other_parameters_separated_by_pipe. other_parameter_separated_by_pipe contains the other initialization parameter for the algorithm.

Entries can look like this:

The only issue with this technique is that you eed now all the classes for Processing parameters (all the children classes of QgsProcessingParameterDefinition)

To conclude

All these features will let you build algorithms, that will be way easier to use either on the desktop, command line with qgis_process or using the web with WPS and py-qgis-wps.

What we did to have R scripts in QGIS-processing-R could be adapted to other programming languages like Julia.

René-Luc D'Hont and Ludovic Hirlimann