Groups keyboard shortcuts have been updated
Dismiss
See shortcuts

Executing R commands through Netlogo - simpleR extension question

20 views
Skip to first unread message

G. Papad.

unread,
Apr 3, 2025, 9:08:01 AMApr 3
to netlogo-users
Dear all,

First, apologies for the long question.

After stumbling upon many issues with the bundled r extension, I installed simpleR very easily and got the examples running (Windows 11, Netlogo 6.4.0, R 4.4.3).

However, I haven't quite managed to perform what I had in mind, hence I wanted to ask the community's help.

In a nutshell, I want turtles to run a command in R in every tick and then store the output (from R) into an attribute of theirs. The problem is that the needed R command comes with an R library. Hence, my question is whether this is possible. I.e load the library from within Netlogo and then run the required command, using simpleR's syntax.

A simple example, only for illustrating the general idea, (similar to the actual implementation where a library would be loaded, a function from that library would be needed, and it would take numerical as well as text inputs) would be something like the following R script:

# --- R script starts here ---
# Load library which has the needed command
library(forecast)
# Define y values. In the actual implementation, this would be retrieved from Netlogo's agents
y <- c(2, 3, 5, 7, 8, 9, 9, 12, 3, 4, 6, 12, 3, 8, 11, 10, 19, 20, 18)
# Fit a model. In the actual implementation, 'bic' would be taken from a global variable in Netlogo e.g. my_ic
fit <- auto.arima(y, ic = 'bic')
# Define the needed output to be transmitted to Netlogo
out_for_netlogo <- fit$loglik
# --- R script ends here ---

First, I did put the following in the setup button:

to setup
sr:setup
sr:run "library(forecast)"
end

which gives the following message in the Command Center: "Warning from R environment:  package 'forecast' was built under R version 4.4.3".

If I interpret it correctly, this indicates that it is properly executed.

However, when I put it like this:

to setup
(sr:run "library(forecast)" "fit <- auto.arima(c(2, 3, 5, 7, 8, 9, 9, 12, 3, 4, 6, 12, 3, 8, 11, 10, 19, 20, 18), ic = 'bic')")
end

I only get the previous message without any other output or error message. Moreover, when I run the following from the Command Center: 

sr:run  "fit <- auto.arima(c(2, 3, 5, 7, 8, 9, 9, 12, 3, 4, 6, 12, 3, 8, 11, 10, 19, 20, 18), ic = 'bic')"

I get this error:

Extension exception: could not find function "auto.arima"
error while observer running SR:RUN
  called by Command Center

Do you think that what I want to do is feasible with the simpleR extension's commands? Or should I consider some other approach?

As an alternative, I thought of using CSV files to transmit information from Netlogo to R and vice versa, e.g. save the needed input (for R) from Netlogo in a CSV file, then call the respective RScript, perform the calculations, store the output in another CSV and finally, load it in Netlogo for the simulation to continue. Though tedious and likely inefficient, this approach stops at its tracks before even beginning.

While the "setup" button works ok-ish (includes sr:setup and sr:run "library(forecast)") any attempt (sr:run "source('C:/my path/my_r_script.R')" in the Command Center or in the "go" button) to run the relevant R script gives the following error:

Error in sr.send_error(sock, e$message, e$message) :
could not find function "sr.send_error"
Calls: sr.start_server ... doTryCatch -> tryCatchList ->tryCatchOne -> <Anonymous>
Execution halted

And Netlogo freezes.

I would be very grateful for any help, advice or direction where to look at.

Cheers,
Georgios
Reply all
Reply to author
Forward
0 new messages