# How can I make a dataset with one element display with a header row?

I'm building a virtual lab exercise in Mathematica. As part of it, I'm using Dynamic to show the students' lab dataset that they store in memory using a ClickPane. However, I'm running into a quirk of Mathematica's default formatting that isn't ideal.

Specifically: Mathematica displays Datasets differently when they contain one element (MM 12.0):

ds1 = Dataset[{<|"a" -> 0.5, "b" -> 2.0|>}]
ds2 = Dataset[{<|"a" -> 0.5, "b" -> 2.0|>, <|"a" -> 1.0, "b" -> 3.14159|>}]


Is there a way to get Mathematica to display a one-element dataset like ds1 with the keys as a header row and the values as a data row, rather than in columns?

Ideally this would be deployable inside a Dynamic block, so that the displayed data tables would smoothly grow as the students "collected more data". In other words, if we did code such as

Dynamic[ (* something involving a variable ds *) ]
ds = ds1;


and then

ds = ds2;


the Dynamic display would in both cases show a two-column table with a header row followed by one row for each data point.

• does type =TypeSystemVector[TypeSystemStruct[{"a", "b"}, {TypeSystemAtom[Integer], TypeSystemAtom[Integer]}], 1]; Dataset[{<|"a" -> 1, "b" -> 2|>},type] work? – kglr Aug 25 '20 at 7:24
• @kglr : It displays a data set with one element correctly. However, there a few problems: (a) the front end colors type in the second command in red, as though it's a syntax error. (b) When deployed as Dynamic[Dataset[ds, type]] (as in my second-to-last code block above), if ds contains multiple elements, the table only shows the first data point in the table and then displays n total > in a row below that. I can't figure out how to display the remaining elements. ... – Michael Seifert Aug 25 '20 at 14:26
• (c) The code fails if the data points are machine-precision numbers, which the students' data generally will be. (Mea culpa for not mentioning this initially; I've edited my question to reflect this.) – Michael Seifert Aug 25 '20 at 14:27
• @kglr +1 for those clever workarounds in V12.0, but users of V12.1 ought to note that they will no longer work in that version. I haven't tried V12.1.1. The formatting heuristics change almost every release, sometimes significantly :( The LiveStreams promise more explicit control in future versions. – WReach Aug 25 '20 at 20:11
• @MichaelSeifert: To get multiple rows to display, change the outer length declaration in the first suggestion from 1 to TypeSystemAnyLength. That is: type=TypeSystemVector[..., TypeSystemAnyLength];. – WReach Aug 25 '20 at 20:12

At some point it is worth writing own UI from scratch:

ClearAll[DatasetTable];
DatasetTable[ds : Verbatim[Dataset][{KeyValuePattern[{}] ..}, ___]] :=
Module[{union = KeyUnion@Normal[ds], keys, values}
, keys = Keys@First@union
; values = Values@union
; Grid[
Prepend[keys]@values
, Frame -> All, FrameStyle -> GrayLevel@.9
, Alignment -> Left
, Background -> {{}, {GrayLevel@.95}}
, Spacings -> {2, 1}
]
]

ds = ds1;
Dynamic @ DatasetTable @ ds


ds = ds2;
`