Quirk
Quirk

Reputation: 175

Requirejs not loading library

I'm using a data analytics tool (KNIME) that allows for the visualisation of data, but only by exposing the javascript layer (i.e. I can't build an html file), which therefore means I need to load external libraries through Requirejs.

I typically only use the d3.js library which is fairly straightforward to load through the require() function. However, when I try to load the regression-js library the regression object is returned as undefined.

regression-js Github page for reference - https://github.com/Tom-Alexander/regression-js

How can I load these two libraries so I can visualise my data properly?

I've tried messing around with shim() and define() but I'm not sure where I'm going wrong.

require.config({
    paths: {
        d3src: 'https://d3js.org',
    },
    map: {
        '*': {
            'd3': 'd3src/d3.v5.min', //loads fine
            'd3-color': 'd3src/d3-color.v1.min', //loads fine
            'd3-interpolate': 'd3src/d3-interpolate.v1.min', //loads fine
            'd3-scale-chromatic': 'd3/src/d3-scale-chromatic.v1.min', //loads fine
            'regression': 'https://cdnjs.cloudflare.com/ajax/libs/regression/1.4.0/regression.min.js' //does not seem to load properly
        }
    }
});

require(['d3', 'd3-color', 'd3-interpolate','regression'], function(d3,regression) { //am I not calling the regression object correctly here?

//ideally this code should return something

const result = regression.linear([[0, 1], [32, 67], [12, 79]]);
// Uncaught TypeError: regression.linear is not a function

const gradient = result.equation[0];
const yIntercept = result.equation[1];
console.log(result,gradient,yIntercept);

});

In the code provided the console.log() should return an object, followed by two floats.

Upvotes: 1

Views: 316

Answers (1)

Castro Roy
Castro Roy

Reputation: 7803

The version you are trying to load(1.4.0) is quite old and it does not support requirejs. If you still need that version, you will need to load it in a script tag then use the global object regression.

If you can use the latest version 2.0.1, then you can go like this:

require.config({
    paths: {
        d3src: 'https://d3js.org',
        regressionsrc: 'https://cdn.jsdelivr.net/npm/[email protected]/dist'
    },
    map: {
        '*': {
            'd3': 'd3src/d3.v5.min', //loads fine
            'd3-color': 'd3src/d3-color.v1.min', //loads fine
            'd3-interpolate': 'd3src/d3-interpolate.v1.min', //loads fine
            'd3-scale-chromatic': 'd3/src/d3-scale-chromatic.v1.min', //loads fine
            'regression': 'regressionsrc/regression.min' //loads fine
        }
    }
});

require(
  ['d3', 'd3-color', 'd3-interpolate','regression'], 
  function(d3,d3Color, d3Interpolate, regression) {

    const result = regression.linear([[0, 1], [32, 67], [12, 79]]);

    const gradient = result.equation[0];
    const yIntercept = result.equation[1];
    console.log(result,gradient,yIntercept);

});

Load it from https://www.jsdelivr.com/package/npm/regression instead of cdnjs.com

Or use npm install regression and load it from your own server or locally if developing.

Hope it helps

Upvotes: 1

Related Questions