4

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?

underdark
84.9k22 gold badges237 silver badges418 bronze badges
asked Feb 27, 2019 at 15:04
1
  • 2
    I think that the 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. Commented Feb 27, 2019 at 16:01

1 Answer 1

3

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!

answered Feb 27, 2019 at 19:31
3
  • Good to know, indeed it is working now. Just for reference, I am using QGIS v. 2.18.18 and R 3.5.2 Commented Feb 28, 2019 at 8:26
  • Which R provider version are you using, though? Commented 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 prints R execution console output R version 3.5.2 (2018年12月20日) -- "Eggshell Igloo" I would assume this is the R version QGIS is running Commented Feb 28, 2019 at 9:08

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.