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 :
- [Bugfix] CSV Output Table destination was not in results
- [BUGFIX] Processing R algorithms do not have enough outputs
- [Bugfix] Support file destination and output
- Folder destination path added in input commands
- Fix return output file destination value
The major addenda to version 3.0.0 are based around description of the algorithms:
- Documentation of the parameters from the help file
- New metadata entry
display_name
for the name of the algorithm - Usage of the description string as a parameter 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:
- 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. - The second possible title is read from the metadata (lines starting with
##
)name
, as above the_
are replaced by spaces. Thename
metadata serves as an ID for the algorithm. - THe third and last possibility is the
display_name
metadata entry. The ID will either be deducted from the file name or thename
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
##Layer=vector
there will be aLayer
parameter and it will be a vector layer##Size=number 10
there will be aSize
parameter, it will be a numeric value and it's default value will be10
##X=Field Layer
There will be a variable namedX
whose field name will be taken from theLayer
parameter
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:
##QgsProcessingParameterFeatureSource|INPUT|Vector layer
There will be anINPUT
variable.INPUT
is a vector layer and it's description isVector Layer
.##QgsProcessingParameterNumber|SIZE|Aggregation Size|QgsProcessingParameterNumber.Integer|10
A numeric variable namedSize
, whose description will beAggregation Size
a and default value will be10
.##QgsProcessingParameterField|FIELDS|Attributes|None|INPUT|-1|False|False
Specifies that there is aFIELDS
variable whose name will be taken from theINPUT
vector parameter, and it's description isAttribute
.
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