10
$\begingroup$

I have some data that I want to display as rationals.
someData={<|"utilization fraction" -> 1, "power fraction" -> 1|>, <|"utilization fraction" -> 7/8, "power fraction" -> 224/225|>, <|"utilization fraction" -> 3/4, "power fraction" -> 48/49|>, <|"utilization fraction" -> 5/8, "power fraction" -> 160/169|>, <|"utilization fraction" -> 1/2, "power fraction" -> 8/9|>, <|"utilization fraction" -> 3/8, "power fraction" -> 96/121|>, <|"utilization fraction" -> 1/4, "power fraction" -> 16/25|>, <|"utilization fraction" -> 1/8, "power fraction" -> 32/81|>}; Dataset[someData]

When I display this as a dataset, some of the entries are displayed as rationals and others are converted to decimals. The underlying data only has rational numbers so why do some get converted and how do I stop this?

Resulting Dataset

I have tried various options on ItemSize in hopes that it would keep fractions but none are working. Is there some obscure option that will keep the entries as fractions?

For example,

Dataset[someData,ItemSize->{Full,3}]

doesn't change the view.

I am putting this into a report and my current (awful) solution is to redraw this as an Excel table and copy that.

$\endgroup$
3
  • $\begingroup$ It is interesting that Identity[] InputForm[] and # all yield what I wanted. Thanks to all for the suggestions! $\endgroup$
    – Mark R
    Commented Dec 29, 2021 at 1:24
  • $\begingroup$ I think you can ask WRI if this is a known bug. If it is, then please come back and update this question with the bugs tag. There is no apparent reason that the default formatting should switch every few rows without an explanation. Also I would request members to test it on various versions/platforms and add comments as necessary. $\endgroup$
    – Syed
    Commented Dec 29, 2021 at 6:55
  • $\begingroup$ Thanks for the suggestion @Syed. I've reached out to WRI and if they confirm that it is a bug, I'll update the tag as you suggest. $\endgroup$
    – Mark R
    Commented Dec 30, 2021 at 8:32

4 Answers 4

10
$\begingroup$

You may use the ItemDisplayFunction option of Dataset.

With someData as in OP, then

Dataset[someData,
 ItemDisplayFunction -> (Identity[#] &)
 ]

Mathematica graphics

You can also target certain columns or cells with ItemDisplayFunction.

Hope this helps.

$\endgroup$
7
$\begingroup$

I am not sure what changes rationals to decimals.

However, we may fix this by specifying an explicit "ItemDisplayFunction" choosing "InputForm":

Dataset[someData, ItemDisplayFunction -> (InputForm@# &)]

enter image description here

You may as well choose "StandardForm" or an other form you fancy.

$\endgroup$
1
  • $\begingroup$ An interesting observation: this shows the numbers using "/" rather than the horizontal dividing bar. Strangely, Identity[#] is currently showing a mixture of these values for new data. Meaning some use "/" and some use the horizontal dividing bar. $\endgroup$
    – Mark R
    Commented Dec 30, 2021 at 19:16
7
$\begingroup$

There is a "neat example" in the documentation page for ItemDisplayFunction. I extended it a bit for this question as I really like how the fractions are visualized this way:

Dataset[someData, ItemDisplayFunction -> (
    Labeled[
        Style[
            Spacer[{100 #, 15}],
            Background -> Cyan],
        #, Right] & )] 

enter image description here

$\endgroup$
1
  • $\begingroup$ This is an interesting way to visualize. Thanks for sending! $\endgroup$
    – Mark R
    Commented Dec 29, 2021 at 1:23
6
$\begingroup$

ds = Dataset[someData, ItemDisplayFunction -> (# &)]

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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