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.


1 Answer 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, #] &];) &], 

Column[{tb2, Dynamic @ selected}]

enter image description here

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.