7
$\begingroup$

This might be another known issue in Dataset but not able to find workaround.

Dataset converts $\pi$ to decimal point. But keeps $\{\pi\}$ as exact.

Is there a way to tell Dataset to keep $\pi$ as exact without having to put the value in a list? The strange thing is that it keeps 1/3 exact, but not $\pi$.

MWE V 14.3 on Linux

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

enter image description here

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

enter image description here

Another example

o = Association[{"p1" -> 6980579422424269/2251799813685248}];
Dataset[o]

enter image description here

Note added: This is all done in code. Not interactive. Program does not know where and when to add Defer@ to what entries when making Dataset.

$\endgroup$
10
  • 1
    $\begingroup$ @Nasser, I recommend reporting that as a bug. $\endgroup$ Commented Oct 18 at 17:46
  • 2
    $\begingroup$ @azerbajdzan this is all done in code. Not interactive. Program does not know where and when to add Defer@ to what entries when making Dataset. Unless it does it to everything. $\endgroup$ Commented Oct 18 at 17:52
  • 3
    $\begingroup$ The issue seems related to the way the dataset displays the data. Internally the data is stored as expected Dataset[Association[{"p1" -> 1/3, "p2" -> Pi}]] // Normal gives <|"p1" -> 1/3, "p2" -> \[Pi]|>. Using ItemDisplayFunction -> (StandardForm[#] &)] as proposed by @user1066 one can see the data as initially defined (Pi). $\endgroup$ Commented Oct 18 at 18:05
  • 3
    $\begingroup$ I still think this qualifies as a bug. Dataset is displaying something that it is not. $\endgroup$ Commented Oct 18 at 20:15
  • 1
    $\begingroup$ If you look at FullForm[Dataset[o]] you see that Pi has not been converted. It is only the display of the data set that is incorrect $\endgroup$ Commented Oct 19 at 10:04

3 Answers 3

7
$\begingroup$

You can work round this apparent bug by specifying the ItemDisplayFunction, e.g.

Dataset[o, ItemDisplayFunction -> (#1 &)]

This might have other undesirable effects!

$\endgroup$
5
$\begingroup$

Implementing @azerbajdzan's idea (Defer@...) with @Nasser's ("Unless it does it [Defer@...] to everything"):

Update:

Even easier than the original, and answers How to make Dataset display empty list as {} and not as blank?, too.

ds = Dataset[
   Association[{"p1" -> 1/3, "p2" -> 2 Pi + Sqrt[2], 
     "p3" -> 6980579422424269/2251799813685248, "p4" -> {}}]];
ds[All, Defer]
with exact and braces

Original:

myDisplayFunction[ds_Dataset] := 
   ds[All, Replace[x_?NumericQ :> Defer@x]];

ds = Dataset@Association[{
     "p1" -> 1/3,
     "p2" -> 2 Pi + Sqrt[2], 
     "p3" -> 6980579422424269/2251799813685248}];
myDisplayFunction[ds]
exact numbers
$\endgroup$
3
  • $\begingroup$ Thanks for the worksarounds. I found ItemDisplayFunction -> (#1 &) makes, for some cases, long display to show all, while Defer does not. Not sure why. If you are interested and want to look at it, I put the notebook here and below will post screen shot also. i.sstatic.net/Aq4IxX8J.png $\endgroup$ Commented Oct 19 at 18:01
  • $\begingroup$ ... on the other hand, your trick of using ds[All,Defer] does display empty list as {} while ItemDisplayFunction -> (#1 &) shows blank. I would have used your trick, except many items in Association are wide and they get truncated for some reason but not when using ItemDisplayFunction -> (#1 &) i.sstatic.net/VeDbsdth.png If you think this should be separate question as different issue, may be I could post this later. $\endgroup$ Commented Oct 19 at 18:30
  • 1
    $\begingroup$ @Nasser Yep, as I mentioned, Dataset[] doesn't try to give an exact display, but it seems to create a UI that is likely to be easy for the user to scan on the screen of a computer. Consequently it hides long expression with a clickable "...". The thresholds for hiding are controlled by TypeSystem`PackageScope`$ElisionByteLimit, TypeSystem`PackageScope`$ElisionLeafLimit, TypeSystem`PackageScope`$ElisionLengthLimit. You can set them high to get everything to show. $\endgroup$ Commented Oct 19 at 18:42
3
$\begingroup$

Typesetting is generally hackable (e.g. $\TeX$ or Makeboxes[]), although that does not mean it is safe to hack it. Numeric expressions in a Dataset[] display that are not numbers are typeset by abox[], which is not protected. We can use a boolean flag $showTypesetExact so that the output is not numericized and overwrite DownValues[TypeSystem`NestedGrid`PackagePrivate`abox][[15]] thus:

TypeSystem`NestedGrid`PackagePrivate`abox[
  TypeSystem`NestedGrid`PackagePrivate`other_] := 
 If[NumericQ[TypeSystem`NestedGrid`PackagePrivate`other] && 
     !TrueQ[$showTypesetExact], (* new!!! *)
  TextString[N[TypeSystem`NestedGrid`PackagePrivate`other]], 
  Block[{$SummaryBoxDataSizeLimit = \[Infinity]}, 
   StyleBox[
    MakeBoxes[TypeSystem`NestedGrid`PackagePrivate`other] /. 
     TypeSystem`NestedGrid`PackagePrivate`boxes : 
       TypeSystem`NestedGrid`PackagePrivate`summaryBoxesPattern :> 
      RuleCondition[
       TypeSystem`NestedGrid`PackagePrivate`simplifySummaryBoxes[
        TypeSystem`NestedGrid`PackagePrivate`boxes], True], 
    FontFamily -> 
     Dynamic[CurrentValue[{"StyleHints", "CodeFont"}], 
      ImageSizeCache -> {198.89999999999998, {0.23399999999999999, 
         13.767}}], ShowStringCharacters -> True]]]

$showTypesetExact = True;
ds = Dataset[Association[{"p1" -> 1/3, "p2" -> 2 Pi + Sqrt[2]}]]
exact pi

Note that the Dataset[] typesetting functionality expects boxes from abox[]. It often uses TextString, which has its own ideas about numericizing exact expressions.

Pi + Sqrt[2] // TextString
2 Pi // TextString
Pi // TextString
6980579422424269/2251799813685248 // TextString
(*
"4.55581"
"6.28319"
"Pi"
"3.1"
*)

One might want to judiciously choose when to show the exact expression and when not. The typesetting choices of Dataset[] reflect a goal of having the displaying summarize the data (approximately, in some cases), which is at odds with the OP's goals. Mathematica's goal here is similar to that of Root[] object typesetting. From that viewpoint, the numericized display of pi seems more likely to be a design choice rather than an unintentional bug.

This reverts to the old behavior:

$showTypesetExact = False;
ds = Dataset[Association[{"p1" -> 1/3, "p2" -> 2 Pi + Sqrt[2]}]]
numericized

Note the value of $showTypesetExact is used at the moment of typesetting the Dataset[] expression, which happens after the kernel is done computing the Dataset[] and has sent the result to the front end. If you wish to temporarily change the value of $showTypesetExact, Makeboxes[] has to be called while the change is in effect to see the desired output. Examples:

This works:

$showTypesetExact = True;
Dataset[Association[{"p1" -> 1/3, "p2" -> 2 Pi + Sqrt[2]}]]
$showTypesetExact = False;

This fails:

$showTypesetExact = False;
Block[{$showTypesetExact = True},
 Dataset[Association[{"p1" -> 1/3, "p2" -> 2 Pi + Sqrt[2]}]]
 ]
$\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.