2

I am testing the capabilities to run my R script in QGIS. I am facing a basic question.

I am defining a dropdown menu with all the parameters for a specific variable:

##test1=group
##Sensor?=selection S1;S2;S1 AND S2

enter image description here

Then I use this basic code to test if I can use the selection of the user within an if statement

if (S1){
print("It is working")
}

My script is not working and is reporting the following error. Any idea how this type of variables can be used in a if statement. Shoud I use a reguluar boolean variable. If so, how should it be implemented?

Sensor?=0
Error: unexpected '=' in "Sensor?="
Execution halted
Converting outputs
Loading resulting layers
Algorithm [Unnamed algorithm] finished
3

Just found the solution,

##test1=group
##Sensor=selection S1;S2;S1 AND S2

if (Sensor == "S1"){
print("It is working")
}

I think there is little information or non-unified place to find documentation about the implementation of R scripts in QGIS. If you find a good guide, please feel free to add it on the comments.

--- EDIT ---

Based on another post (Using if statement in R script within QGIS)

##test1=group
##Sensor=selection S1;S2;S1 AND S2

if (Sensor == 0){
print("It is working")
}

works as well taking into account the python indexing rule

  • 2
    Actually posting an answer to your own question so others can use it. Hero. – TeddyTedTed Feb 26 at 10:18
  • 2
    @TeddyTedTed, I think it happens to me as well. Sometimes the solution is much simpler than you expected. But when you are lost you are normally trying to implement any possible approaches to achieve the result that you are striving for. And then at some point, you see that miserable answer hidden in your "Learn by falling, falling, ... , falling, trying, and achieving" method. And IMHO it is always better when you find solution/answering your own question by yourself. – Taras Feb 26 at 11:52
  • 3
    @Taras I definitely agree, very satisfying to finally figure something out as well. My previous comment was meant as praise for GCGM posting an answer to their own question, sorry for any confusion. – TeddyTedTed Feb 26 at 12:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.