I'd recommend building your functions in an R file then sourcing that file within NetLogo. For example, I have a file called 'example_r.R' that just serves to store Foo
:
Foo <- function(value) {
return(abs(value))
}
I can then source()
the file with r:eval
and all contained functions will be available:
extensions [r]
to setup
ca
ask n-of 5 patches [ sprout 1 ]
r:eval "source('C:/example_r.R')"
reset-ticks
end
to go
ask turtles [
rt random 60 - 30
fd 1
r:put "xcor" xcor
r:eval "out <- Foo(xcor)"
show ( word "My xcor is " round xcor " but my absolute xcor is: " round r:get "out" )
]
tick
end
After setup
and go
:
(turtle 4): "My xcor is 7 but my absolute xcor is: 7"
(turtle 3): "My xcor is 2 but my absolute xcor is: 2"
(turtle 1): "My xcor is -3 but my absolute xcor is: 3"
(turtle 2): "My xcor is -3 but my absolute xcor is: 3"
(turtle 0): "My xcor is -15 but my absolute xcor is: 15"