4

I would like to define a simple function to automatically show all rows in a Dataset. Based on Taliesin Beynon's answer, I tried the following trivial snippet:

ClearAll[ShowDataset]
ShowDataset[{assoc__}, query__] := 
    Block[{Dataset`$ElisionThreshold = Length[{assoc}]*4},
        Dataset[Query[query]@{assoc}]
    ]

However, this doesn't work; the number of rows showed is still 16. On the other hand, Block does what it is supposed to do, because if I adjust the code to read

ClearAll[ShowDataset]
ShowDataset[{assoc__}, query__] := 
    Block[{Dataset`$ElisionThreshold = Length[{assoc}]*4},
    Dataset[Query[query]@{assoc}];
    Dataset`$ElisionThreshold
    ]

It shows the correct number of rows. Is this expected behaviour? Is there another way to temporarily and automatically set Dataset`$ElisionThreshold?

J. M.'s missing motivation
  • 124,525
  • 11
  • 401
  • 574
freddieknets
  • 1,085
  • 8
  • 16
  • It may be helpful if you included a sample dataset, or even better, if you demonstrated your problem using one of the sample datasets provided by ExampleData["Datasets"]. Additionally, when you say that your second code snippet showed "the correct number of rows", did you mean to say that it showed the correct value of Dataset`$ElisionThreshold? – MarcoB Oct 21 '15 at 15:17

2 Answers2

11

The resulting Dataset expression is typeset by the front-end after evaluation is complete. This means that the Block expression has already been exited before typesetting occurs. The value of Dataset`$ElisionThreshold is no longer being overridden as the dataset is being rendered.

One way to work around this is to use Print to force the typesetting to occur during evaluation:

ClearAll[ShowDataset2]
ShowDataset2[{assoc__}, query__] := 
    Block[{Dataset`$ElisionThreshold = Length[{assoc}]*4},
        Print @ Dataset[Query[query]@{assoc}]
    ]

This simple work-around comes at a cost: the return value of ShowDataset2 is now Null instead of the dataset.

Usage:

$data = ExampleData[{"Dataset", "Titanic"}] // Normal;

ShowDataset2[$data, All]

dataset screenshot

Beware that the front-end sometimes has difficulty rendering large box structures such as the one in the example above. I saw intermittent rendering glitches as a scrolled through my test notebook (using version 10.3 on Windows 7x64, emphasis on "intermittent").

Update for Version 11

Version 11 introduced a new style of dataset formatting. The variable Dataset`$ElisionThreshold is not used in the new style -- Dataset`$DatasetTargetRowCount is used instead. Alternatively, Dataset`$ElisionThreshold can still be used if we also set Dataset`$UseNewDatasetFormatting = False.

All of this remains undocumented and unsupported behaviour.

WReach
  • 68,832
  • 4
  • 164
  • 269
4

You could use an output form:

Unprotect[$OutputForms];
AppendTo[$OutputForms, ShowDataset];
Protect[$OutputForms];

ShowDataset /: MakeBoxes[ShowDataset[ds_Dataset],StandardForm]:=Block[
    {Dataset`$DatasetTargetRowCount = Length[ds]},

    With[{boxes = MakeBoxes[ds]},
        InterpretationBox[
            boxes,
            ds
        ]
    ]
]

For example:

data = Dataset @ RandomReal[1, {25, 2}]

enter image description here

ShowDataset[data]

enter image description here

Head[%]

Dataset

I used an InterpretationBox so that you can copy/paste the output.

Carl Woll
  • 130,679
  • 6
  • 243
  • 355