Node.js d3 package
The d3 package has data access functionality. In this case, we will read from a tab-separated file and compute an average. Note the use of the underscore variable name for lodash. Variable names starting with an underscore are assumed to be private, although, in this case, it is just a play on the name of the package we are using, lodash, or underscore. Also, lodash is a widely used utility package.
The script we use is as follows:
var fs = require("fs"); var d3 = require("d3"); var _ = require("lodash"); //read in the animals file fs.readFile("data/animals.tsv", "utf8", function(error, data) { data = d3.tsv.parse(data); //display on screen console.log(JSON.stringify(data)); //compute the maximum weight var maxWeight = d3.max(data, function(d) { return d.avg_weight; }); //display the max on screen console.log(maxWeight); });
This assumes we have previously loaded the fs and d3 packages using npm, as described in the previous script.
For this example, I created...