1

I need to use the R extension in NetLogo to do some network calculations. I am creating the network in NetLogo, exporting it to a text file, having R read the text file and construct a graph and calculate properties, then getting the calculation results. The export, read, calculate and get are being controlled by NetLogo through the R extension.

However, NetLogo and R have different default working directories. The problem I have about changing directories in R breaking the connection to the extensions (see R extension breaks connection to extensions directory in NetLogo) is affecting my attempts to use BehaviorSpace on the model.

My new approach is to not change the R working directory, but simply to provide the full path to R of the exported file.

  r:clear
  let dir pathdir:get-model
  r:eval "library(igraph)"

  ; read network in R (avoid bug of R change working directory)
  let runstring (word "r:eval \"gg <- read_graph(file = \"" dir "\\netlogo.gml\", format = \"gml\")\"")
  print runstring
  run runstring

This produces the correct string to run, output from print statement:

r:eval "gg <- read_graph(file = "C:\Users\Jen\Desktop\Intervention Effect\netlogo.gml", format = "gml")"

But I get an error on the run runstring that this nonstandard character is not allowed. Debugging by putting my constructed string into the run command directly, I have realised it is because I am now in a string environment and have to escape ('\') all my backslashes and quotes. That is, the command that would work if directly typed or included in the NetLogo code, will not work if it is provided as a string to be run.

I haven't yet been able to construct a string to put into the line run runstring that works, even by hand. This means I don't know what the string looks like that I am trying to create. Having identified the appropriate target string, I will need code to take the variable 'dir', convert it to a string, add the various \ characters to the dir, add the various \ characters to the quotes for the rest of the command, and join it so that it runs.

Can anyone provide some bits of this to get me further along?

Still struggling with this

I am now trying to work backwards. Find a string that works and then create it.

If I hard code the run command as follows, NetLogo closes. Even though if I copy the text between the quotes and enter it directly into R, it does what is expected.

let Rstring "gg <- read_graph(file = 'C:\\Users\\Jen\\Desktop\\Intervention Effect\\Networks\\netlogo.gml', format = 'gml')"
r:eval Rstring
Community
  • 1
  • 1
JenB
  • 17,620
  • 2
  • 17
  • 45
  • Have you tried forward instead of backward slashes? When I remember correctly, R only accept doubled backslashes and single forward slashes for paths. Because you could become problems by mixing doubled backward slashes and the quotes I would try forward slashes. – Jan C. Thiele Jan 23 '16 at 21:26
  • thanks, i will have a try. I am retrieving the path using the pathdir extension, which of course uses backward slashes. I will do a substitute and see what happens. – JenB Jan 23 '16 at 21:30
  • My thought was that if you're on a unixey system like OSX, you could use symbolic links at the OS level to trick NetLogo and R, but I see you're using Windows. However, it looks like [Windows has 'em too](http://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/) now (not surprising, after all these years). – Mars Apr 28 '16 at 02:40

1 Answers1

2

The pathdir option ended up working. Here is example code for anyone who has a similar problem in the future.

let filename (word "Networks/netlogo" behaviorspace-run-number ".gml")
export-simple-gml filename

r:clearLocal
let dir pathdir:get-model
set filename (word dir "/" filename)
r:put "fn" filename
r:eval "gg <- read_graph(file = fn, format = 'gml')"
r:eval "V(gg)$name <- V(gg)$id"          ; gml uses 'id', but igraph uses 'name'

I have a separate procedure for the actual export, which constructs a simplified gml file because the igraph import of gml format files is somewhat crippled. That's the procedure called within the code above, and the relevant piece is:

to export-simple-gml [ FN ]
  carefully [ file-close-all ] [ ]
  carefully [ file-delete FN ] [ ]
  file-open FN
  file-print <line to write to file>
  ...
end
JenB
  • 17,620
  • 2
  • 17
  • 45