I am creating an R script in QGIS and I am facing an issue with an if statement
. First, I declare the input from the user
##Which_S2_bands_do_you_want_to_use=selection 2 3 4 5 6 7 8 11 12;2 3 4 8 11 12
Later I use it in the following condition:
if (Which_S2_bands_do_you_want_to_use == "2 3 4 5 6 7 8 11 12") {
bands<-c("B((0[2348]_10m)|(((0[567])|(1[12])|(8A))_20m)).jp2$")}
And use it in the following code:
S2<-list.files(S2, recursive = TRUE, full.names = TRUE, pattern=bands)
When I run it, I get the following error:
Error in list.files(S2, recursive = TRUE, full.names = TRUE, pattern = bands) :
object 'bands' not found
Execution halted
It seems the if statment
is not been run. Any idea what could be the reason?
Maybe another approach to use the input from the user as a condition to change a variable in the script?
1 Answer 1
With the new R processing provider, when you give the user selection options, the options are returned as integers starting with 0 (Python indexing format - not R's which would start with 1!).
This should work:
if (Which_S2_bands_do_you_want_to_use == 0) {
bands<-c("B((0[2348]_10m)|(((0[567])|(1[12])|(8A))_20m)).jp2$")}
Note that your original code should have worked in QGIS 2.18 and the old R provider!
-
Good to know, indeed it is working now. Just for reference, I am using QGIS v. 2.18.18 and R 3.5.2GCGM– GCGM2019年02月28日 08:26:27 +00:00Commented Feb 28, 2019 at 8:26
-
Which R provider version are you using, though?Simbamangu– Simbamangu2019年02月28日 09:02:54 +00:00Commented Feb 28, 2019 at 9:02
-
Working on Ubuntu, from
Processing - Options - Providers - R
I cannot see the version been used. Having only R 3.5.2 installed and that the log file printsR execution console output R version 3.5.2 (2018年12月20日) -- "Eggshell Igloo"
I would assume this is the R version QGIS is runningGCGM– GCGM2019年02月28日 09:08:27 +00:00Commented Feb 28, 2019 at 9:08
selection
parameter type returns an integer corresponding to the position of the selection. See this answer: gis.stackexchange.com/a/220078/70980. So if the user selects the first option it returns 0, the second option returns 1, and so on. You can verify this by adding a print statement with your parameter variable at the beginning of your script.