Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save yjunechoe/1c8cf5e35eaa6dd439c9802cd4396ce7 to your computer and use it in GitHub Desktop.
Save yjunechoe/1c8cf5e35eaa6dd439c9802cd4396ce7 to your computer and use it in GitHub Desktop.
Example of interactive grouped summary in Quarto using Arquero
---
title: "Interactive aggregation"
format: html
editor: visual
---
## Define data
```{r}
ojs_define(mydata = palmerpenguins::penguins)
```
## View data in JS
```{ojs}
import { aq, op } from '@uwdata/arquero'
mytable = aq.table(mydata)
mytable.view()
```
## Interactivity
Input:
```{ojs}
viewof groupingvars = Inputs.checkbox(
["species", "island", "sex"]
)
```
Output:
```{ojs}
mytable
.groupby(groupingvars)
.filter(d => d.body_mass_g > 0)
.rollup({
count: op.count(),
avg_mass: op.average('body_mass_g')
})
.view()
```
@yjunechoe
Copy link
Author

ezgif-4-295903d2ec

@yjunechoe
Copy link
Author

yjunechoe commented Jun 22, 2023

## Interactivity2

Input:

```{ojs}
viewof counter = Inputs.button([
  ["Increment", value => value + 1],
  ["Decrement", value => value - 1],
  ["Reset", () => 0]
], {label: "Counter", value: 0})
```

Output:

```{ojs}
aq.addFunction('mySquare', x => x * x);

mytable
  .params({
    counter: counter
  })
  .groupby(groupingvars)
  .filter(d => d.body_mass_g > 0)
  .rollup({
    count: op.count(),
    count2: (d, $) => op.count() * $.counter,
    count3: (d, $) => op.count() * op.mySquare($.counter),
    avg_mass: op.average('body_mass_g')
   })
  .view()
```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment