# Create list via checkboxes

I am trying to achieve the following: Three (dynamically updated) Checkboxes (a, b and c) can be toggled on or off individually. Depending on their status, a resulting list shall be set to {a,b,c}, {a,c}, {b,c}, etc.

I am currently using the following code:

{"a:" Checkbox[Dynamic[a]],
"b:" Checkbox[Dynamic[b]],
"c:" Checkbox[Dynamic[c]],
Dynamic[{If[a, "a",], If[b, "b",], If[c, "c",]}]}


But this yields, depending on the setting, a list of the kind {a,b,Null}, etc. - that is, if a checkbox is set to False, the list element is set to Null rather than left out entirely.

Is there any way to achieve my goal in a not-so-cumbersome way (i.e., avoiding something like a 8-cases Piecewise or Which function)? Thanks in advance!

well, I think this might be a duplicate, see link below, but here we go:

In your setup, you want to make sure that the If evaluates to something meaningful (as you can see, just leaving the 3rd argument blank yields Null).

Thus, it makes sense to use either Unevaluated[Sequence[]] or shorter, just ##&[]:

{"a:" Checkbox[Dynamic[a]], "b:" Checkbox[Dynamic[b]],
"c:" Checkbox[Dynamic[c]],
Dynamic[{If[a, "a", Unevaluated[Sequence[]]], If[b, "b", ## &[]],
If[c, "c", ## &[]]}]}


Then, if the If-clause evaluates to false, you are returned an unevaluated sequence, which will be used in further output (in your case, the list).

You might ask: why the Unevaluated? See Szabolcs explanation here: Why can't I use Sequence to perform a Select like task? (and note J.M.'s comment there, for the vanishing function)

As mentioned in the comments, you might e.g. use Pick for this task:

{"a:" Checkbox[Dynamic[a]], "b:" Checkbox[Dynamic[b]], "c:" Checkbox[Dynamic[c]],
Dynamic@Pick[{"a", "b", "c"}, {a, b, c}]}

• Great, thanks a lot! Should I delete my comments above, since they don't add anything to the question? Commented May 28, 2013 at 12:55
• well, why not, let's clean up a bit :) Commented May 28, 2013 at 12:59

It seemed that the question is asking how to display a list of selected strings. It seemed quite possible that underlying that, is how to pick variables in the same way, which takes a little more care. (You can substitute inside the Dynamic[resultingList = ...] the expression {a, b, c} or varList for labelList, if you want the list to be the variables.)

labelList = {"a", "b", "c"};
varList := {a, b, c};


# CheckboxBar

You can use CheckboxBar, but the check boxes have to appear together.

x = {};
{CheckboxBar[Dynamic[x], {1 -> "a", 2 -> "b", 3 -> "c"}],
Dynamic[resultingList = labelList[[Sort@x]]]}


# Checkbox

If you want to place the check boxes wherever, then you can use Parts of a List.

y = ConstantArray[False, 3];
{"a:" Checkbox[Dynamic[y[[1]]]], "b:" Checkbox[Dynamic[y[[2]]]],
"c:" Checkbox[Dynamic[y[[3]]]], Dynamic[resultingList = Pick[labelList, y]]}


More succinctly, and with Row to format the label-check-box pairs:

pickedVars = ConstantArray[False, 3];
{MapIndexed[Row[{#1, Checkbox[Dynamic[pickedVars[[First[#2]]]]]}] &,
{"a: ", "b: ", "c: "}],
Dynamic[resultingList = Pick[labelList, pickedVars]]}


Technically in your original code you're using Times to format them. It seems to work because the order that Mathematica puts the factors in happens to be the one you desire. You can also use Labeled instead of Row, as well as several other commands.

You can also wrap the whole thing with Row and get rid of the List braces (modifying the list structure a little first).

# TogglerBar

In case you're open to alternatives, TogglerBar could be used instead, but you may already know that.

z = {};
{TogglerBar[Dynamic[z], labelList], Dynamic[resultingList = Sort@z]}


To get the variables, you can use the following:

z2 = {};
{TogglerBar[Dynamic[z2], {1 -> "a", 2 -> "b", 3 -> "c"}],
Dynamic[resultingList = varList[[Sort@z2]]]}

• I actually ended up using ToggleBar and Sort myself, as you suggest, after I discovered those functions. Thanks for pointing out, though! Commented May 28, 2013 at 15:40