4. Non-spatial outputs

When we think about processing tools, we cannot forget that many of the outcomes are not necessarily spatially referenced. Therefore, the Processing R Provider plugin also has input and output parameters of non-spatial variables. In this entry, we will focus on the output parameters that are not spatial objects.

Recalling the list we saw in the first reading, we can say that the non-spatial output parameters are many: table, string, number, folder and file. But to these we must also add the graphics (RPLOTS) and the console (R_CONSOLE_OUTPUT). Let's start by looking at some particularities of this group of parameters.

An R script allows you to return a table to the QGIS project via the table argument. This output requires that after executing the whole script, the Table object is an object of class "data.frame". The output parameter line would be:

1##Table=output table
2Table <- data.frame(a = 1, b = 2)
r

In the case of these two parameters, there is no output returned to the layers panel in QGIS. The output is a python list of class QgsList. The values returned by these outputs can be reused in other outputs or tools.

1##String=output string
2##Number=output number
r

After executing the whole script, the String object should be of class "character" while the Number object should be of class "numeric". An example of such outputs can be seen in the script named "Min_Max", included in the plugin installation. In the image below you will see an example of using this script in the modeler.

Model for generating a uniform-valued raster based on the minimum and maximum of a numeric field in a vector layer[^1]

Model for generating a uniform-valued raster based on the minimum and maximum of a numeric field in a vector layer[^1]

[^1] Gaona, G. (2021, October 19). Serie: Flujo de trabajo con R y QGIS. Parte 3 [Blog]. Asociación QGIS España. https://www.qgis.es/post/2021-10-19-serie-flujo-de-trabajo-con-r-y-qgis-parte-3/

These two are special output cases. They allow the export of graphics and console to an html file. Although image (graphics) or text (console) files are also available in some sense. The specification differs from a typical output parameter, because these are more like behavioral parameters that generate outputs.

1##output_plots_to_html
2> mean(x)
r

The plots depend on the output_plots_to_html parameter, while the console logging depends on at least one line of the script body starts with the > character. This will generate the interfaces to save the corresponding outputs. In the case of the output to the console, it is necessary that the R line of code executes the print.*() method in one of its forms.

These last two output parameters behave similarly to the input parameters. That is, they return to the R session the paths to the files or directories defined through the graphical interface. These can be reused within the script as any text object containing a path.

The specification of these parameters is as follows:

1##Work_dir=Output folder
2##Stat_file=Output file csv
3
4setwd(Work_dir)
5...
6write.csv2(Summary_statistics, Stat_file, row.names = FALSE, na ="")
r

The practice of this reading consists of adding header lines or modifying the body of the script to display output parameters of the tools made with R. Let's start!

  1. Edit the script "Cálculo de afectación" and add the missing header line to display the graph that is in the script code.
  2. Edit the "Mínimo y Máximo" script so that it prints in the console log the results of the three lines of code.
  3. Review and comment to the rest of the participants on the script header "Making a ggplot2 interactively".
🤞 Hint

The content below has been intentionally hidden. Unfold it only if you feel you cannot perform the exercise on your own.

Click to display the help content. 1. Copy and paste the following header line into the script:
```r
#output_plots_to_html
```
  1. Copy and replace the body of the script with the following lines:

    1> (Min <- min(Layer[[Field]]))
    2> (Max <- max(Layer[[Field]]))
    3> (Summary <- paste(Min, "to", Max, sep = " "))
    
    r
  2. The script has no output. You can comment on that!

Translations: