Skip to content

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

@AurelienPeter

Description

@AurelienPeter

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:

  • OS: Windows 10
  • GAMA version: 1.9.2

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Todo

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions