Mike Sheehy
Mike Sheehy

Reputation: 71

Netlogo Export/Tableau issues

Currently I'm playing around with exporting my data from Netlogo to a CSV file and then loading it into Tableau with the following code..

to write-result-to-file
  ; if nothing to write then  stop
  if empty? Result-File [stop]
  ; Open file
  file-open Result-File

  ; Write into the file
  file-print (word Days-passed "," num-susceptible "," num-infected "," num-recovered)
  ; Close file
  file-close
end

Where am running into trouble is when I load the data into tableau it isn't properly picking up the measures/dimensions. Is there a way in Netlogo to specify the headers of each of my rows/columns before they are exported to the CSV file?

Upvotes: 3

Views: 595

Answers (1)

Charles
Charles

Reputation: 4208

This question was asked and answered over on NetLogo Users. James Steiner's answer is copied below, with a few typos in the code corrected. It's really quite elegant.


You can print the headers to your results-file during setup!

You might want to make a subroutine to handle all writing to the file, so you don't have to repeat code:

to write-csv [ #filename #items ]
  ;; #items is a list of the data (or headers!) to write.
  if is-list? #items and not empty? #items
  [ file-open #filename
  ;; quote non-numeric items
  set #items map quote #items
  ;; print the items
  ;; if only one item, print it.
  ifelse length #items = 1 [ file-print first #items ]
  [file-print reduce [ (word ?1 "," ?2) ] #items]
  ;; close-up
  file-close
  ]
end

to-report quote [ #thing ]
  ifelse is-number? #thing
  [ report #thing ]
  [ report (word "\"" #thing "\"") ]
end

You would call it with

write-csv "myfilename.csv" ["label1" "label2" "label3"]

to write the column headers in your setup routine, and then

write-csv "myfilename.csv" [10.0 "sometext" 20.3]

to write a row of data - in this case a number, a string, and another number.

Upvotes: 3

Related Questions