Error grouping data by csv column

Hi, I’m new to Observable. I’m facing an error where when I try to group data by a column from a .csv file, it shows this error.

“TypeError: undefined is not a function (near ‘…t of f…’)”

I’m implementing this in a .md file that’s part of the Observable Framework. The exact syntax is below:

const a = FileAttachment("./data/trimmed.csv").csv({typed: true});
Inputs.select(d3.group(a, d => d.Aggressor))

If I try the same operation in the observable notebook, it seems to work fine.

Sorry if this is a silly question, any help would be appreciated!

FileAttachment(...).csv() returns a Promise. Observable’s Runtime resolves Promises automatically for you, but only when they are referenced from other cells/blocks.

If you want to consume the value of the Promise in the same cell you have to await it:

const a = await FileAttachment("./data/trimmed.csv").csv({typed: true});
Inputs.select(d3.group(a, d => d.Aggressor))

or

const a = FileAttachment("./data/trimmed.csv").csv({typed: true});
Inputs.select(d3.group(await a, d => d.Aggressor))

Not at all! :slight_smile:

That worked! Thanks so much, this is an incredible tool!

1 Like

Just to add to what @mootari said, Framework also resolves promises across JavaScript blocks in the markdown file, so you could also do the equivalent of what happens in cells by creating different JavaScript blocks in the markdown file:

```js 
const a = FileAttachment("./data/trimmed.csv").csv({typed: true});
```

```js
Inputs.select(d3.group(a, d => d.Aggressor))
```
2 Likes

That makes sense! Thanks for sharing.