Function Repository Resource:

EvaluatePreviousCell

Source Notebook

Evaluate the contents of the previous cell

Contributed by: Asem Wardak (University of Sydney)

ResourceFunction["EvaluatePreviousCell"][]

evaluates the cell directly above the currently evaluating cell.

ResourceFunction["EvaluatePreviousCell"][n]

evaluates the nth previous cell.

Details and Options

ResourceFunction["EvaluatePreviousCell"] allows the usage of objects that have been previously evaluated in the notebook without the time or space overhead typically seen when interacting with complex objects.
Use ResourceFunction["EvaluatePreviousCell"] to refer to complex objects persistently and efficiently across kernel sessions without the need for recomputation or external files.
ResourceFunction["EvaluatePreviousCell"] takes the same options as PreviousCell.

Examples

Basic Examples (2) 

Refer to an object that took substantial resources to compute:

In[1]:=
Prime[2^50] // AbsoluteTiming
Out[1]=
In[2]:=
ResourceFunction["EvaluatePreviousCell"][] // AbsoluteTiming
Out[2]=

Use a graphical object that typically slows down the user interface when interacted with (e.g. over a slow remote connection):

In[3]:=
$HistoryLength = 0; Graphics3D@
 Table[Polygon@Table[RandomReal[10, 3], 3], 100000]
Out[3]=
In[4]:=
StringTemplate["`` MB"]@
 N[ByteCount@ResourceFunction["EvaluatePreviousCell"][]/2^20]
Out[4]=

Scope (1) 

Refer to an expression not immediately previous to the current one:

In[5]:=
ResourceFunction["EvaluatePreviousCell"][3]
Out[5]=

Options (1) 

Ignore text cells when counting:

In[6]:=
ResourceFunction["EvaluatePreviousCell"][CellStyle -> "Output"] === ResourceFunction["EvaluatePreviousCell"][3] === ResourceFunction["EvaluatePreviousCell"][GeneratedCell -> True]
Out[6]=

Possible Issues (4) 

Text cells do not evaluate if their contents are not evaluable:

In[7]:=
ResourceFunction["EvaluatePreviousCell"][]
Out[7]=

Referring to the input cell of an input-output cell group reevaluates the input, leading to repeated computations:

In[8]:=
RandomGraph[{5, 5}]
Out[8]=
In[9]:=
ResourceFunction["EvaluatePreviousCell"][2]
Out[9]=

Refer to the generated output cell instead, which is not evaluated any further:

In[10]:=
RandomGraph[{5, 5}]
Out[10]=
In[11]:=
ResourceFunction["EvaluatePreviousCell"][GeneratedCell -> True]
Out[11]=

If n refers to a nonexistent cell, a ToExpression::notstrbox is produced:

In[12]:=
ResourceFunction["EvaluatePreviousCell"][100] // Head
Out[12]=

If the cell being referred to contains an evaluable instance of EvaluatePreviousCell, recursion errors may occur:

In[13]:=
ResourceFunction["EvaluatePreviousCell"][]
In[14]:=
ResourceFunction["EvaluatePreviousCell"][]
Out[14]=

Avoid reevaluating EvaluatePreviousCell by matching only output cells:

In[15]:=
Table[ResourceFunction["EvaluatePreviousCell"][n, CellStyle -> "Output"], {n, 13}]
Out[15]=

Neat Examples (1) 

Use the binary representation of expressions to store and retrieve persistent data within the notebook:

In[16]:=
BinaryPrevious = BinaryDeserialize@*ResourceFunction["EvaluatePreviousCell"];
In[17]:=
RandomReal[1, {1000, 2}] // BinarySerialize
Out[17]=
In[18]:=
ListPlot[BinaryPrevious[], PlotMarkers -> "OpenMarkers"]
Out[18]=

Publisher

Asem Wardak

Version History

  • 1.0.0 – 26 October 2020

Related Resources

Author Notes

Future implementations of related functions should consider:

Evaluating arbitrarily placed cells
Reading the contents of previous text cells
Support for evaluating cell groups

License Information