# eval d3.js projection from a string?

**URL:** <https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750>\
**Category:** Help\
**Created:** [June 28, 2022, 12:19pm UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750 "2022-06-28T12:19:40Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [June 28, 2022, 12:19pm UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/1 "2022-06-28T12:19:40Z")

</div>

I don’t know if it’s possible, but I don’t understand why eval(“d3.geoStereographic()”) returns the message “ReferenceError: d3 is not defined”. Why is d3 not defined? Is there any other way to evaluate this string?

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [June 28, 2022, 12:34pm UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/2 "2022-06-28T12:34:08Z")

</div>

In observable you mean? you’d need to define the d3 reference in the same cell:

```auto
{d3; return eval("d3.geoStereographic()")}

```

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [June 28, 2022, 12:40pm UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/3 "2022-06-28T12:40:44Z")

</div>

Thanks Fil

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [June 28, 2022, 3:21pm UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/4 "2022-06-28T15:21:55Z")

</div>

And outside Observable?  
Actually, I try to find a solution to run bertin.js with a serialized config file.  
See [Untitled / neocarto / Observable](https://observablehq.com/d/aedd0ce5df9b3545)  
But, I’m not sure what relevant method to use to retrieve the projection and the geojson layers. Any help is welcome.

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [June 28, 2022, 3:39pm UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/5 "2022-06-28T15:39:59Z")

</div>

try this:

```auto
config = {
  d3;

  let json = JSON.parse(str);

  // replace projection
  if (json.params.projection.substring(0, 6) === "d3.geo") {
    const projection = eval(json.params.projection);
    if (typeof projection !== "function") {
      throw new Error(`projection ${json.params.projection} is not available`);
    }
    json.params.projection = projection
  }

  // replace geojson

  //json.layers[0].geojson = eval(json.layers[0].geojson);
  json.layers[0].geojson = world;

  return json;
}

```

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [June 29, 2022, 7:24am UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/6 "2022-06-29T07:24:18Z")

</div>

Perfect. Thank you very much.

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [June 30, 2022, 9:20am UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/7 "2022-06-30T09:20:08Z")

</div>

Well… It’s me again. Actually, I don’t really understand how all of this works.

If I write an ES6 module like this :

```auto
import * as d3selection from "d3-selection";
import * as d3geo from "d3-geo";
import * as d3geoprojection from "d3-geo-projection";
const d3 = Object.assign({}, d3selection, d3geo, d3geoprojection);

export function test(str) {
  d3;
  const fn = eval(str);
  return fn;
}

```

It does not work anymore.

If I try to execute

```auto
test("d3.geoBertin1953()")

```

an error is returned : _TypeError: d3.geoBertin1953 is not a function_

But why? 🤷

---

<div class="post-metadata">

**Author:** ![Fil](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/fil/32/207_2.png) [@Fil](https://talk.observablehq.com/u/Fil)\
**Post date:** [June 30, 2022, 9:35am UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/8 "2022-06-30T09:35:13Z")

</div>

Mentioning d3 in a cell is specific to Observable’s runtime.

Also, please read [https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global\_Objects/eval#never\_use\_eval](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/eval#never_use_eval)!  
maybe you’ll find the right way to do this with window.Function instead? (Note that in some environments even the use of Function is discouraged.)

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [June 30, 2022, 9:55am UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/9 "2022-06-30T09:55:53Z")

</div>

Ok. Thank you. I think I’ll give up my (bad) idea.

---

<div class="post-metadata">

**Author:** ![mootari](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/mootari/32/581_2.png) [@mootari](https://talk.observablehq.com/u/mootari)\
**Post date:** [July 1, 2022, 8:22am UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/10 "2022-07-01T08:22:48Z")

</div>

I recommend to use dependency injection via a wrapper:

```javascript
// function anonymous(d3) { return (d3.geoBertin1953()) }
createProjection = new Function('d3', `return (${json.params.projection})`)

```

This produces the function

```javascript
// createProjection.toString()
function anonymous(d3
) {
return (d3.geoBertin1953())
}

```

which you can then invoke to pass in `d3`:

```javascript
projection = createProjection(d3)

```

---

<div class="post-metadata">

**Author:** ![neocarto](https://yyz2.discourse-cdn.com/flex030/user_avatar/talk.observablehq.com/neocarto/32/6554_2.png) [@neocarto](https://talk.observablehq.com/u/neocarto)\
**Post date:** [July 1, 2022, 1:58pm UTC](https://talk.observablehq.com/t/eval-d3-js-projection-from-a-string/6750/11 "2022-07-01T13:58:32Z")

</div>

Great!
