R extension demo

R extension demo preview image

1 collaborator

Ahdekker Anthony Dekker (Author)



Tagged by Anthony Dekker about 10 years ago


Tagged by Anthony Dekker about 10 years ago

r extension 

Tagged by Anthony Dekker about 10 years ago


Tagged by Anthony Dekker about 10 years ago

Visible to everyone | Changeable by the author
Model was written in NetLogo 5.0.3 • Viewed 574 times • Downloaded 52 times • Run 0 times
Download the 'R extension demo' modelDownload this modelEmbed this model

Do you have questions or comments about this model? Ask them here! (You'll first need to log in.)


This program implements Conway's "Game of Life" together with a simulation of bacterial chemical-gradient-climbing, in order to demo the R extension for a tutorial at http://scientificgems.wordpress.com/2014/02/11/the-r-extension-for-netlogo-a-tutorial/


The "average-density" slider sets the initial average fraction of live cells for the "Game of Life."

The "Setup Life" or "Setup Bacteria" buttons initialise one or the other simulation.

The "Run" button runs the selected simulation.

The "Plot Life" button plots the "Game of Life" density over time, using the R extension. An input box allows editing of the default file name.

Comments and Questions


This model is associated with a tutorial at http://scientificgems.wordpress.com/2014/02/11/the-r-extension-for-netlogo-a-tutorial/

Posted about 10 years ago

Click to Run Model

extensions [ r ]

globals [
  is-life?   ;; distinguish between the two demos
  ;; LIFE
  cell-count ;; total number of cells
  total      ;; total number of live cells
  history    ;; list of totals
  window     ;; number of steps over which to analyse

patches-own [
  ;; LIFE
  alive?     ;; is cell alive?
  counter    ;; used to pass number of live neighbours between Phase 1 and Phase 2

  value      ;; simulated chemical concentration

turtles-own [
  hist       ;; history of concentration values

to rplot-graph  ;; R interface -- plot the LIFE history
  if (is-life?) [
    r:put "life.hist" history
    r:put "plotfile" life-plot-filename
    r:eval "png (filename = plotfile, width = 1600, height = 800, pointsize=18)"
    r:eval "par (mar=c(5,5,0.8,0.5))"
    r:eval "xs <- 0:(length(life.hist)-1)"
    r:eval "plot (life.hist ~ xs, type=\"l\", lwd=4, col=\"red\", xlab=\"Step\", ylab=\"Density\", ylim=c(0,max(life.hist)), cex.lab=2, cex.axis=1.5)"
    r:eval "dev.off()"

to-report calculate-slope [ lst ]  ;; R interface -- calculate slope for values in lst
  r:put "valu.hist" lst
  r:eval "valu.index <- 1:length(valu.hist)"
  r:eval "valu.lm <- lm(valu.hist ~ valu.index)"
  report r:get "valu.lm$coefficients[2]"

;; code for SETUP button -- Life

to setup-life
  set is-life? true
  set life-plot-filename "C:/NetLogo/Life_plot.png"
  set cell-count (count patches)
  ask patches [
    ifelse (random-float 1 < average-life-density) [birth] [death]
  set history (list (total / cell-count))

to death  ;; this procedure applies to a specific patch
  set alive? false
  set pcolor white

to birth  ;; this procedure applies to a specific patch
  set alive? true
  set pcolor blue
  set total (total + 1)

;; code for SETUP button -- Bacteria

to setup-bacteria
  set is-life? false
  set life-plot-filename "** NOT USED **"
  set window 4
  ;; create gradient
  repeat 3 [
    let x random-xcor
    let y random-ycor
    let v (0.1 + random-float 0.5)
    let r (1 + v * world-width / 2)
    ask patches [
      let d ((distancexy x y) / r)
      set value (value + v * exp (- d))
  let min-value (min ([value] of patches))
  let max-value (max ([value] of patches))
  ask patches [
    set pcolor (30 + (9.9 * (value - min-value) / (max-value - min-value)))
  ;; create some bacteria
  crt 20 [
    setxy random-xcor random-ycor
    set color red
    set size 6
    set hist (list value)


to go  ;; single simulation step for both demos
  if-else (is-life?) [
    ;; LIFE
    ;; First phase
    ask patches [ set counter (count neighbors with [alive?]) ]
    ;; Second phase
    set total 0
    ask patches [
      ifelse (counter = 3 or (counter = 2 and alive?))
      [ birth ]
      [ death ]
    set history (lput (total / cell-count) history)  ;; Record current density
  ] [
    ask turtles [
      fd 1
      set hist (lput value hist)
      if (length(hist) > window) [ set hist (bf hist) ]
      if (length(hist) = window) [
        if (calculate-slope hist <= 0) [
          set heading (random-float 360)
          set hist (list value)

There is only one version of this model, created about 10 years ago by Anthony Dekker.

Attached files

File Type Description Last updated
BacteriaDemoRview.png png Screenshot of "bacteria" mode about 10 years ago, by Anthony Dekker Download
R extension demo.png preview Preview for 'R extension demo' about 10 years ago, by Anthony Dekker Download

This model does not have any ancestors.

This model does not have any descendants.