Function Repository Resource:

NiceGrid

Source Notebook

Nicely format data in various structures into a grid format

Contributed by: Andrew Steinacher

ResourceFunction["NiceGrid"][data]

formats data in a grid.

ResourceFunction["NiceGrid"][data,cols]

formats data in a grid with column headers cols.

ResourceFunction["NiceGrid"][data,cols,rows]

formats data in a grid with both column and row headers.

Details and Options

ResourceFunction["NiceGrid"] can handle lists, lists of lists, associations, lists of associations and nested associations.
The following options are accepted:
"MaxNumber"50maximum number of rows to display before eliding them
"MissingItem"Tooltip["-",Missing["Absent"]]expression used in place of missing grid items
"NumberColumn"Falsewhether to add a numbered column

Examples

Basic Examples (2) 

Show array data in a grid preloaded with helpful options:

In[1]:=
ResourceFunction["NiceGrid"][Partition[Range[9], 3]]
Out[1]=

Add row and column headers:

In[2]:=
ResourceFunction["NiceGrid"][
 Partition[Range[9], 3], {"C1", "C2", "C3"}, {"R1", "R2", "R3"}]
Out[2]=

Scope (3) 

Easily display the contents of an Association in a grid with row headers:

In[3]:=
ResourceFunction["NiceGrid"][<|"A" -> 1, "B" -> 2, "C" -> 3|>]
Out[3]=

Show a list of associations in a grid with column headers:

In[4]:=
ResourceFunction[
 "NiceGrid"][{<|"A" -> 1, "B" -> 2, "C" -> 3|>, <|"A" -> 4, "B" -> 5, "C" -> 6|>}]
Out[4]=

Show nested associations in a grid with both row and column headers:

In[5]:=
ResourceFunction[
 "NiceGrid"][<|"row1" -> <|"A" -> 1, "B" -> 2, "C" -> 3|>, "row2" -> <|"A" -> 4, "B" -> 5, "C" -> 6|>|>]
Out[5]=

Options (8) 

MaxNumber (2) 

Use "MaxNumber" to limit the number of rows shown:

In[6]:=
ResourceFunction["NiceGrid"][Partition[Range[100], UpTo[7]], "MaxNumber" -> 10]
Out[6]=

By default, "MaxNumber" is set to 50 to avoid overwhelming the Wolfram System and to show approximately one screen height of common text rows:

In[7]:=
ResourceFunction["NiceGrid"][Range[1000]]
Out[7]=

MissingItem (1) 

Use "MissingItem" to specify how grid elements that correspond to missing key-value pairs are displayed:

In[8]:=
ResourceFunction[
 "NiceGrid"][{<|"b" -> 2, "a" -> 1|>, <|"a" -> 3|>, <|"c" -> 9, "b" -> 6, "a" -> 5|>}, "MissingItem" -> "MISSING"]
Out[8]=

NumberColumn (5) 

Add a number column to easily count rows:

In[9]:=
ResourceFunction["NiceGrid"][{"A", "B", "C", "D", "E"}, "NumberColumn" -> True]
Out[9]=

The number column can be used with "MaxNumber":

In[10]:=
ResourceFunction["NiceGrid"][ToUpperCase@Alphabet[], "NumberColumn" -> True, "MaxNumber" -> 5]
Out[10]=

The number column gets a header if column headers are specified:

In[11]:=
ResourceFunction[
 "NiceGrid"][{#!, #!!} & /@ Range[5], {"single", "double"}, "NumberColumn" -> True]
Out[11]=

A number column can be added if row headers are not specified implicitly or explicitly:

In[12]:=
ResourceFunction[
 "NiceGrid"][{<|"b" -> 2, "a" -> 1|>, <|"a" -> 3|>, <|"c" -> 9, "b" -> 6, "a" -> 5|>}, "NumberColumn" -> True]
Out[12]=

If row headers are specified, the option is ignored:

In[13]:=
ResourceFunction[
 "NiceGrid"][<|"A" -> {1, 2, 3}, "B" -> {4, 5, 6}, "C" -> {7, 8, 9}|>,
  "NumberColumn" -> True]
Out[13]=

Applications (1) 

Import a file and view approximately a screenfull of its contents:

In[14]:=
timeSeries = Import["ExampleData/financialtimeseries.csv"];
ResourceFunction["NiceGrid"][timeSeries]
Out[15]=

Properties and Relations (3) 

Options for Grid are supported:

In[16]:=
ResourceFunction["NiceGrid"][
 <|
  "A" -> <|"a" -> "lorem", "b" -> "ipsum"|>,
  "B" -> <|"a" -> "doror", "b" -> "sit"|>,
  "C" -> <|"a" -> "amet", "b" -> "consectetur"|>
  |>,
 Alignment -> Left, Spacings -> 2
 ]
Out[16]=

Dataset allows for dynamically navigating data with useful features like scrollbars:

In[17]:=
data = AssociationThread[
   Alphabet[] -> (StringRepeat[#, 50] &) /@ Alphabet[]];
In[18]:=
Dataset[data]
Out[18]=

NiceGrid shows data more simply in a static form, typically with tighter rows and columns:

In[19]:=
ResourceFunction["NiceGrid"][data]
Out[19]=

Dataset requires dynamic updating to display; NiceGrid does not:

In[20]:=
CurrentValue[CellObject, DynamicUpdating] = False;
Rasterize /@ {Dataset[{1, 2, 3}], ResourceFunction["NiceGrid"][{1, 2, 3}]}
CurrentValue[CellObject, DynamicUpdating] = Automatic;
Out[21]=

Possible Issues (2) 

Nonuniform data can lead to undesirable formatting:

In[22]:=
data = <|"A" -> <|"a" -> 1, "b" -> 2|>, "B" -> <|"a" -> 3, "b" -> 4|>,
    "C" -> <|"a" -> 5, "b" -> 6, "c" -> 3|>|>;
ResourceFunction["NiceGrid"][data]
Out[23]=

Normalize the data to get better formatting:

In[24]:=
ResourceFunction["NiceGrid"][
 AssociationThread[Keys[data] -> KeyUnion[Values[data], f]]]
Out[24]=

Nested data may have undesirable formatting:

In[25]:=
nestedData = <|
   "A" -> <|"i" -> "one", "ii" -> "two"|>,
   "B" -> <|"iii" -> "three", "iv" -> "four", "v" -> "five"|>,
   "C" -> <|"vi" -> "six", "vii" -> "seven", "viii" -> "eight"|>
   |>;
ResourceFunction["NiceGrid"][nestedData]
Out[26]=

Using NiceGrid at different levels may give better results:

In[27]:=
ResourceFunction["NiceGrid"][
 ResourceFunction["NiceGrid"] /@ nestedData]
Out[27]=

Neat Examples (1) 

Gather a large dataset with images and show a small portion of it:

In[28]:=
data = EntityValue[
   EntityClass["FoodType", "CommonOutsideColor" -> "Green"],
   {"Image", "ApproximateShape", "CommonInsideColor", "DefaultServingSizeMass"},
   "EntityPropertyAssociation"
   ];
ResourceFunction["NiceGrid"][data, "MaxNumber" -> 5]
Out[28]=

Publisher

Andrew Steinacher

Version History

  • 2.0.0 – 10 June 2020
  • 1.0.0 – 13 September 2019

Related Resources

Author Notes

Non-uniform data can cause undesired behavior. The code could be made more robust to this, but as a first pass, it’s pretty good. The use of RuleDelayed in Association or lists of rules may get evaluated… the code could also be made more robust to this, but this case is not too common. Grid option translation is not perfect, and is very tricky to get completely correct.

License Information