# Parts[] of DynamicModule output

I gave a partial answer to a question, Can Manipulate controls be generated programmatically based on a (non-manipulated) variable?, which has led me to questions of my own.

Background:

The following code produces a pair of identical Sliders, where setting a value of 1 shows up in the other.

   {Slider[Dynamic[d], {0, 1, 0.01}],
Dynamic[d],
Dynamic[Plot[Sin[1 + d x], {x, -10 \[Pi], 10 \[Pi]}]]
} & /@ Range[2]


e.g.,

Wrapping the code in a DynamicModule gives you something different, the ability to set the Sliders independently (a possibly useful thing to do):

dm = DynamicModule[{d},
{Slider[Dynamic[d], {0, 1, 0.01}],
Dynamic[d],
Dynamic[Plot[Sin[1 + d x], {x, -10 \[Pi], 10 \[Pi]}]]
}] & /@ Range[2]

{"Length", Length[dm]}
{"Dimensions", Dimensions[dm]}
TreeForm[dm]


I've added the Length, Dimensions, and TreeForm to better understand what the code actually produces.

Let's explore this a bit more:

dm[[1]]
Length[%]


Note, I set the Slider to 0.69 after executing the code.

And now I try to extract the Parts of the expression:

dm[[1, 1]]
dm[[1, 2]]
dm[[1, 3]]


This seems a bit strange. I would have expected that I would get the 3 Parts of dm[[1]] in order, e.g:

Slider
Value
Plot


So, some questions:

1. Can someone explain this? The Parts of dm[[1]] don't appear to behave as one would expect.

2. Does a way exist to access the current state of the 2 Plots in dm, and for instance Show them together?

3. Does the TreeForm give us any insight into how to do access Parts of such expressions?

I realize that these questions might resemble something akin to arcane chess problems, with not much real world application, but I hope that answers could give some additional working insight into these structures.

• It looks to me like you are getting the three parts of the first DynamicModule: List[d], another List, and a DelayedRule. – Josh Bishop Oct 30 '20 at 18:42

## 1 Answer

1- DynamicModule does not disappear like Module. After initial evaluation of content it stays inert and will be used when it is sent as an output to produce DynamicModuleBox which governs scoping within your gui.

dm[[1]] // InputForm

DynamicModule[
{d},
{Slider[Dynamic[d], {0, 1, 0.01}], Dynamic[d], Dynamic[Plot[Sin[1 + d*x], {x, -10*Pi, 10*Pi}]]},
DynamicModuleValues :> {}
]


This is dm[[1]] and it has two arguments and one option which means you will get length 3.

2- dm is just an expression as any other, Table[dm, {10}] contains 10 independent copies of it. Each generated GUI will be independent. If you want to access values from inside most often you will need to do this from inside e.g.:

DynamicModule[{x}, Slider @ Dynamic[x, (notScopedX = x = # )&]


Now you can use Dynamic @ notScopedX somewhere else.

3- Sure it does, just like the InputForm for example. Your tree form says you can access the first slider with dm[[1, 2, 1]]. But this is a copy of what was used to generate GUI earlier/later.

Let me know if anything is not clear.