sr:set
sr:set variable-name value
Sets a variable in the R session with the given name to the given NetLogo value. NetLogo objects will be converted to R objects as expected.
Note that lists in NetLogo are converted into lists in R if the elements are of different types. If all the elements of a NetLogo list are of the identical number, boolean, or string type then the data will be automatically converted into a vector in R.
sr:set "x" 42
sr:run "print(x)" ;; prints `[1] 42` to the command center
sr:set "y" [1 2 3]
sr:run "print(typeof(y))" ;; prints `[1] "double"` to the command center
sr:run "print(typeof(list(y)))" ;; prints `[1] "list"` to the command center
sr:run "print(y)" ;; prints `[1] 1 2 3` to the command center
show sr:runresult "y" ;; reports [1 2 3]
Agents are converted into lists with named elements for each agent variable.
Agentsets are converted into a list of the above lists. If you want to convert agents to a data frame, see sr:set-agent-data-frame
. If you want to use sr:set
and do the conversion manually, try the following:
my_data_frame <- as.data.frame(do.call(rbind, <agentset-list-of-lists>))
For example:
breed [goats goat]
goats-own [energy ]
create-goats 2 [ set color 75 ]
ask goat 0 [ set energy 42 set xcor 5]
ask goat 1 [ set energy -42 set xcor -5]
sr:set "goat" goat 0
sr:run "print(typeof(goat))" ;; prints `[1] "list"` to the command center
sr:run "print(goat)"
;; Should output:
;; $WHO
;; [1] 0
;;
;; $COLOR
;; [1] 75
;; (etc.)
sr:set "goats_list_of_lists" goats
sr:run "goats_data_frame <- as.data.frame(do.call(rbind, goats_list_of_lists))"
sr:run "print(goats_data_frame)"
;; Should output:
;; WHO COLOR HEADING XCOR YCOR SHAPE LABEL LABEL-COLOR BREED HIDDEN? SIZE
;; 1 0 75 82 5 0 default 9.9 GOATS FALSE 1
;; 2 1 75 200 -5 0 default 9.9 GOATS FALSE 1
;; PEN-SIZE PEN-MODE ENERGY
;; 1 1 up 42
;; 2 1 up -42
;;
Agents with variables containing references to agentsets will have those variables converted into the string representation of that agentset.
Take me to the full Simple R Extension Dictionary