I want to create a list of values, True or False, and to update those values with a Checkbox. But the number of elements needs to be arbitrary.

It should look something like the following code which works perfectly fine:


But, of course, it is not practical to write out every iteration of these things. This is the reason that Table exists. However, when I put everything into tables, it does not work.


The code above results in this error message:

Part::pspec: Part specification i is neither an integer nor a list of integers. >>

I do not understand why Table and Dynamic are in conflict with one another. It is as if Dynamic reaches the i before Table and replace it with an iterative value and then Dynamic has no idea what to do with it. But why is this? Or is the problem something else altogether?

  • $\begingroup$ Sounds like a job for Module or With; using these will allow you to limit the context of your counting variable. $\endgroup$ – IPoiler Jul 29 '15 at 15:19

Compare the output of this:

Table[ Dynamic[i], {i,3}]  (* gives {i,i,i} *)

with this:

Table[ With[{i=i},Dynamic[i]], {i,3}]  (* gives {1,2,3} *)

Now you can use that:

a = Range[3];
Table[ With[{i=i}, Dynamic[ a[[i]] ] ], {i,3}]  (* gives {1,2,3} *)


a+=1;   (* changes output above to {2,3,4} *)

Addendum to address question in the comments below: You can look at the Attributes of a function (in this case Dynamic). If you see that it has a HoldFirst (or HoldRest, or HoldAll) attribute, then that tells you some of its arguments will not be evaluated. For example, consider:

Attributes[foo] = {HoldFirst};
Table[ foo[i], {i,3} ]   (* gives {foo[i], foo[i], foo[i] *)
Table[ With[{i=i},foo[i]], {i,3}]  (* gives {foo[1], foo[2], foo[3] *)
  • $\begingroup$ Thank you so much. That answers the question. Out of curiosity, precisely what was the problem? How can I avoid this in the future? Thanks again! $\endgroup$ – cbward Jul 29 '15 at 15:51
  • $\begingroup$ I added some info in the answer. Also this post is useful; if you read it you will likely avoid many common programming conundrums: mathematica.stackexchange.com/questions/18393/… $\endgroup$ – Arnoud Buzing Jul 29 '15 at 16:26

One possibility is to use MapIndexed:

n = 3; dyn = Table[False, {i, n}];
A = Dynamic@Transpose[{Range[n], dyn}]
MapIndexed[Checkbox[Dynamic[dyn[[#2[[1]]]]]] &, Range[n]]
  • $\begingroup$ similarly Table[ Checkbox[Dynamic[dyn[[#]]]] &@i , {i, 3}] $\endgroup$ – george2079 Jul 29 '15 at 19:21
  • $\begingroup$ @george2079 Yes. Or, of course, just Map[Checkbox[Dynamic[dyn[[#]]]] &, Range[n]] $\endgroup$ – Rolf Mertig Jul 29 '15 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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