gama-platform / gama.experimental

Home of experimental plugins for GAMA
13 stars 6 forks source link

R_eval can't execute a R script in a whole #34

Open AurelienPeter opened 1 year ago

AurelienPeter commented 1 year ago

Describe the bug When GAMA runs a R script the lines are executed line by line. A condition taking several lines is not considered as a whole, as each line is independant. Thus even if the condition is not fulfilled the lines will be executed. Maybe there is a way to execute several lines at once?

To Reproduce Steps to reproduce the behavior: Write script.R:

value = 0
if(F){
  value = 1
}
print(paste("value:",value))
if(T){
  value = 2
}
print(paste("value:",value))

Run this model:

model R_in_Gama

global skills:[RSkill]{
    file Rcode <- text_file("myscript.R");
    init {
        // Try to use R tasks
        do startR;
        // Loop that takes each line of the R script and execute it.
        loop s over: Rcode.contents{
            unknown a <- R_eval(s);
            write a;
        }
    }
}

experiment Test_R_Task type: gui {
}

Output in GAMA:

[0.0]
[NULL ]
[1.0]
[NULL ]
R>[1]
R> "ÿþCurrent value: 1ÿþ"
R>

Current value: 1
[NULL ]
[2.0]
[NULL ]
R>[1]
R> "ÿþCurrent value: 2ÿþ"
R>

Current value: 2

Expected behavior The value should not become 1 as the condition is false. The if statement is placed in the previous line and is not considered in the next line which is executed.

Desktop: