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

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.

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, #] &]}]


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}]


• Very nice! I don't know as much as I need to about using Dynamic. Thanks. Commented Feb 16, 2021 at 5:15