6
$\begingroup$

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|>}]

enter image description here

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.

$\endgroup$
  • $\begingroup$ does type =TypeSystem`Vector[TypeSystem`Struct[{"a", "b"}, {TypeSystem`Atom[Integer], TypeSystem`Atom[Integer]}], 1]; Dataset[{<|"a" -> 1, "b" -> 2|>},type] work? $\endgroup$ – kglr Aug 25 at 7:24
  • $\begingroup$ @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. ... $\endgroup$ – Michael Seifert Aug 25 at 14:26
  • $\begingroup$ (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.) $\endgroup$ – Michael Seifert Aug 25 at 14:27
  • 1
    $\begingroup$ @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. $\endgroup$ – WReach Aug 25 at 20:11
  • 1
    $\begingroup$ @MichaelSeifert: To get multiple rows to display, change the outer length declaration in the first suggestion from 1 to TypeSystem`AnyLength. That is: type=TypeSystem`Vector[..., TypeSystem`AnyLength];. $\endgroup$ – WReach Aug 25 at 20:12
2
$\begingroup$

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

enter image description here

ds = ds2;

enter image description here

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.