TransWikia.com

Select from list using a TogglerBar to specify one or several criteria

Mathematica Asked by George Wolfe on June 15, 2021

Simple version of my problem. Create a list of 50 integers. Create a TogglerBar to select one or more members of the list. Use Select to select sublists from the larger list. I can’t figure out how to do this. I tried Member@, ContainsAny, Counting the length of the Intersection of n with {1,2,3,4,5}, but none of them worked.

list = RandomChoice[{1, 2, 3, 4, 5}, 50]

tb = TogglerBar[Dynamic[n], {1, 2, 3, 4, 5}];

Click cells in tb and this try to use the list of n as selection criteria.

Select[list, n==?]

Actual problem is a list of lists with selection criteria being a category value in the list.

One Answer

SeedRandom[1]
list = RandomChoice[Range@5, 50];

ClearAll[n, tb]

tb = TogglerBar[Dynamic[n], {1, 2, 3, 4, 5}];

Use MemberQ[n, #]& as the criterion function and wrap Select with Dynamic:

Column[{tb, Dynamic @ Select[list, MemberQ[n, #] &]}]

enter image description here

We get the same result using

Column[{tb, Dynamic @ Cases[list, Alternatives @@ n]

Alternatively, use a variable (selected) to keep track of selected elements and use the second argument of Dynamic in TogglerBar to update selected:

ClearAll[n, tb2, selected]

selected = {};

tb2 = TogglerBar[Dynamic[n, 
         (n = #; selected = Select[list, MemberQ[n, #] &];) &], 
       Range[5]];

Column[{tb2, Dynamic @ selected}]

enter image description here

Correct answer by kglr on June 15, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP