3
$\begingroup$

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?

$\endgroup$
  • $\begingroup$ 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? $\endgroup$ – MarcoB Oct 21 '15 at 15:17
8
$\begingroup$

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.

$\endgroup$
  • $\begingroup$ It doesn't work in 11.2 $\endgroup$ – yode Dec 1 '17 at 2:50
  • $\begingroup$ @yode In version 11, we must use Dataset`$DatasetTargetRowCount instead of Dataset`$ElisionThreshold. I have updated the response. $\endgroup$ – WReach Dec 1 '17 at 4:02
  • $\begingroup$ Ok, your magic works again. :) $\endgroup$ – yode Dec 1 '17 at 6:00
2
$\begingroup$

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.

$\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.