4
$\begingroup$

I use Dataset to display Association. For debugging mainly. But if a field value is empty list {}, it display as blank. This can be confusing as it looks also same as if field was empty string.

Is there an option to make Dataset display {} as {}?

MWE

o = Association[{"ode" -> y'[x] == x, "ic" -> {}}];
InputForm[o]

(* <|"ode" -> Derivative[1][y][x] == x, "ic" -> {}|> *)

Dataset[o]

enter image description here

Which is same as if Association was

o=Association[{"ode"->y'[x]==x,"ic"->""}];
Dataset[o]

enter image description here

I looked at Dataset themes and itemstyle, but not able so far to find option to do this.

V 14.3 on Linux

$\endgroup$
13
  • 1
    $\begingroup$ Or Defer@{} is maybe a better choice if you want to copy data from displayed dataset so that it does not contain HoldForm. $\endgroup$ Commented Oct 18 at 9:39
  • 2
    $\begingroup$ @azerbajdzan I cannot copy from the display of a Dataset[] on my system (MacOS ARM, V14.3.0). I tried double-clicking, but I can't select anything. Does it work for you? Or is there another way to copy the displayed data? $\endgroup$ Commented Oct 18 at 13:41
  • 1
    $\begingroup$ @MichaelE2 Right click and select copy data to clipboard or copy position to clipboard. $\endgroup$ Commented Oct 18 at 14:10
  • 1
    $\begingroup$ @azerbajdzan Thanks, it works. However, I wonder if this is a bug: Dataset@Association[{"ode" -> y'[x] == x, "ic" :> {Nothing}}] -- you can't right-click copy the displayed {} (which is typeset in a different way than Defer@{}, too). $\endgroup$ Commented Oct 18 at 15:09
  • 1
    $\begingroup$ In any case, my spelunking suggests to me that the "elision" of empty lists in Dataset[] displays is probably hard-coded. If there is a way, I've been barking up the wrong tree. $\endgroup$ Commented Oct 18 at 15:19

2 Answers 2

4
$\begingroup$

See How to prevent Dataset from converting Pi to decimal point? for this method, based on @azerbajdzan's comment re Defer@...:

o = Association[{"ode" -> y'[x] == x, "ic" -> {}}];
ds = Dataset[o];
ds[All, Defer]

For a 2D dataset, use All twice:

ds = Dataset[Association@{"a" -> o, "b" -> o}];
ds[All, All, Defer]
$\endgroup$
1
$\begingroup$

(1) ReplaceAll

One simple workaround,

o = Association[{"ode" -> y'[x] == x, "ic" -> {}}];

Dataset[o] /. {} -> {""}

dataset 1 or d1.png

If one wishes to preserve Item formatting (if present)

Block[{Dataset}, 
  Dataset[o, ItemSize -> 20, ItemStyle -> Red] /. {} -> {""}]

dataset 2 or d2.png

An identical result may be obtained by wrapping the Dataset in a (formatted) Dataset

 Dataset[o] // Dataset[# /. {} -> {""}, ItemSize -> 20, ItemStyle -> Red] &

This workaround may be adapted to give a solution to the related question, How to prevent Dataset from converting Pi to decimal point?

oo = Association[{"p1" -> 1/3, "p2" -> Pi}];

Dataset[oo] /. Pi -> "\[Pi]"

Dataset 4 or d4.png

% // Dataset[#, ItemSize -> 10] &

Dataset 4 or d4.png


A third related question is Why are empty lists not consistently formatted in a Dataset?

ds1 = Dataset[Association["a" -> {}, "b" -> {}, "c" -> {}]];

ds1x = ds1 /. {} -> {""};

Grid[{{ds1, ds1x}}, Spacings -> 3]

Grid of ds1.png and ds1x.png

(2) HoldAll

Another approach it to wrap all values with HoldForm (or StandardForm)

x2 = Query[All, HoldForm][Dataset[o]];

x3 = Dataset[HoldForm /@ o, ItemSize -> 10];

Grid[{{Dataset[o], x2, x3}}, Spacings -> 1]

Grid of three items, o, x2 and x3

A problem with this workaround it that all values are wrapped with HoldForm, and this may not be always desirable.

(In the following example, note the inverted commas around the value for ic2)

ox = Association[{"ode" -> y'[x] == x, "ic" -> {}, "ic2" -> "Hello"}];

x1 = Dataset[ox];

x2 = Dataset[HoldForm /@ ox];

Grid[{{x1, x2}}, Spacings -> 3]

grid of two items, x1 and x2


A better workaround may be the following:

modfn[x_] := x;
modfn[{}] := HoldForm[{}]
modfn[Pi] := HoldForm[Pi]

x1 = Query[{All -> modfn}][Dataset[ox]];

x2 = Dataset[modfn /@ ox, ItemSize -> 10];

Grid[{{Dataset[ox], x1, x2}}, Spacings -> 1]

Grid of modfn, ox, x1 and x2

And

Dataset[Query[{All -> modfn}][ds1], ItemSize -> 5]

image of modfn result for ds1

(3) Observation

Block[{N}, ds = (Dataset[<|"a" -> Pi, 
  "b" -> {}, "c" -> 4, "d" -> E|>]); Print[ds]; ds]

Screenshot of Block output

Furthermore,

Block[{N}, ds = (Dataset[<|"a" -> Pi, "b" -> {}, 
  "c" -> 4, "d" -> E, "f" -> 4.2|>]); Print[ds]; ds]

produces the following error in the Print[ds] output:

CompiledFunction::cfsa: Argument N[MachinePrecision] at 
  position 2 should be a machine-size real number.

This seems to suggest that Dataset compiles, and that compilation is somehow connected to the observed unusual and unexpected behaviour in all three related questions?

$\endgroup$

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.