If a notebook containing
a = {1, 2};
Dynamic[a[[2]]]
(* 2 *)
is executed, closed, and then opened in a new session, it yields
along with the expected error message,
Part::partd: Part specification a[[2]] is longer than depth of object. >>
Instead, I would like Dynamic
not to change the answer from the previous session until the notebook is first executed again. Thereafter, in the new session it should behave as it normally would. How can this be accomplished programmatically? An answer that involves executing a = {1, 2}
is not useful, because it does not generalize to my actual problem, in which Dynamic
yields lengthy invalid results, unless the entire preceding portion of the notebook has executed.
Addendum
My thanks to Mike Honeychurch for reminding me to include several variants I had tried that also do not meet my goal.
DynamicModule[{}, Dynamic[a[[2]]]]
If[a[[1]] > 0, DynamicModule[{}, Dynamic[a[[2]]]]]
DynamicModule[{}, If[a[[1]] > 0, Dynamic[a[[2]]]]]
DynamicModule[{}, Dynamic[Refresh[a[[2]], TrackedSymbols -> {a}]]]
Dynamic[If[ValueQ[a], a[[2]], {}]]
gives{}
instead of error, is that good enough? $\endgroup$DynamicModule[{}, Dynamic@a[[2]], SaveDefinitions -> True]
$\endgroup$