({{, (RefLink[Initialization, paclet:ref/Initialization])
is an option for (RefLink[Dynamic, paclet:ref/Dynamic]), (RefLink[DynamicModule, paclet:ref/DynamicModule]), (RefLink[Manipulate, paclet:ref/Manipulate]), and related constructs that specifies an expression to be evaluated when the construct is first displayed. }})

OpenersNotesSection(Spacer1)Details

The typical usage is Initialization:>expr.

Initialization expressions defined in particular cells in a notebook are evaluated when those cells are first made visible, and not necessarily when the notebook is first opened.

Initialization expressions are guaranteed to be evaluated exactly once before the body of the main function is resolved.

An object that is already initialized can be initialized again if it is deleted, then restored as a result of using Undo.

OpenersPrimaryExamplesSection(Spacer1)Examples  (9)

OpenersExampleSection0(Spacer1)Basic Examples  (3)

Specify an expression to be evaluated before displaying (RefLink[Dynamic, paclet:ref/Dynamic]):

Dynamic[c[1],Initialization:>(c[x_]:={x})]

(RefLink[DynamicModule, paclet:ref/DynamicModule]):

DynamicModule[{c},Dynamic[c[1]],Initialization:>(c[x_]:={x})]

(RefLink[Manipulate, paclet:ref/Manipulate]):

Manipulate[c[n],{n,0,1},Initialization:>(c[x_]:={x})]

OpenersExampleSection1(Spacer1)Properties & Relations  (3)

Initialization is guaranteed to only happen once:

DynamicModule[{x=0},Table[DynamicModule[{y},Dynamic[{y,x}],Initialization:>(y=x++)],10]]

Initialization for outer constructs occurs before initialization for inner constructs:

DynamicModule[{x},Dynamic[x,Initialization:>(x="inner")],Initialization:>(x="outer")]

Each time a (RefLink[Dynamic, paclet:ref/Dynamic]) destroys and recreates an initializing construct, the initialization will run again:

DynamicModule[{inner},Dynamic[{Row[{"outer: ",RandomColor[]}],Dynamic[Row[{"inner: ", inner}],Initialization:>(inner=RandomColor[])]},UpdateInterval->1]]

OpenersExampleSection2(Spacer1)Possible Issues  (3)

Loading a package in the initialization does not allow you to use unqualified symbols from that package:

DynamicModule[{x},Dynamic[x],Initialization:>(Needs["PrimalityProving`"];x=ProvablePrimeQ[1093])]

Doing this properly requires loading the package in the Input cell and loading it in an outer initialization:

Needs["PrimalityProving`"];
DynamicModule[{},DynamicModule[{x},Dynamic[x],Initialization:>(x=ProvablePrimeQ[1093])],Initialization:>(Needs["PrimalityProving`"])]

Another way to wrap package loading is to use a one-shot dynamic from (RefLink[CellDynamicExpression, paclet:ref/CellDynamicExpression]):

Needs["PrimalityProving`"];
ExpressionCell[DynamicModule[{x},Dynamic[x],Initialization:>(x=ProvablePrimeQ[1093])],"Output",CellDynamicExpression->Dynamic[Refresh[Needs["PrimalityProving`"],None]]]

({{DocumentationFooterIconsRelatedFunction.png, ({{See Also}, {(RefLink[Deinitialization, paclet:ref/Deinitialization])  ▪  (RefLink[InitializationCell, paclet:ref/InitializationCell])  ▪  (RefLink[CellDynamicExpression, paclet:ref/CellDynamicExpression])}})}})

({{DocumentationFooterIconsRelatedGuide.png, ({{Related Guides}, {▪(RefLinkPlain[Channel-Based Communication, paclet:guide/Channel-BasedCommunication])}, {▪(RefLinkPlain[Options & Styling for Interactive Manipulation, paclet:guide/OptionsAndStylingForInteractiveManipulation])}})}})