Apologies if this has been asked before, I was wondering how I can filter my dataset using multiple selection options. A user would select several of these and the data plotted would only be where all the conditions match. I’ve been able to do this by “hardcoding” my expectation, but wondering if there is a way to do this more elegantly.
Thanks a lot! I’ll try this out. Sorry, one quick question, I assume this is for functions that you’ve defined that can be used but when I put this in my .md file I get the following error:
I’d recommend to put your const a declaration into a separate fenced code block so that the Runtime will resolve the Promise for you. I didn’t need to do that for penguins because Framework already takes care of the loading and resolving for built-in datasets.
The example is tailored to checkboxes where each input returns an array of values. Selects only return a single value, so you will have to adjust the filter callbacks accordingly:
const filters = Object.entries(choices).map(
([name, value]) => Array.isArray(value)
? value.length ? d => value.some(v => v === d[name]) : () => true
: value !== undefined ? d => value === d[name] : () => true
);
Is there a much of a reason to use Inputs.form in Observable Framework? I feel that’s mostly a workaround for notebooks where each cell can only expose a single value.
In Framework you can call view multiple times to declare separate reactive values within the same code block. And the advantage is that these can update independently (rather than with Inputs.form where the entire form updates, potentially causing more downstream evaluation than necessary).
So why not do this:
const species = view(Inputs.checkbox(penguins.map((d) => d.species), {label: "Species", unique: true}));
const island = view(Inputs.checkbox(penguins.map((d) => d.island), {label: "Island", unique: true}));
const sex = view(Inputs.checkbox(penguins.map((d) => d.sex), {label: "Sex", unique: true}));
This also uses the unique option of Inputs.checkbox so you don’t have to do that yourself. (And I prefer to inline the array.map instead of writing the prop helper.)
Ah, sure. But that’s a pretty rare use case where you don’t know the set of inputs/dynamic variables statically, and I don’t recommend such indirection in most cases.