Message Boards Message Boards

0
|
4222 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

Extracting the formatting options from an existing Dataset

I'm trying to write a ResourceFunction tentatively called ReformatDataset that would take a Dataset and add formatting options to it, with the option of preserving options already present. Exercise of this latter option requires one to extract the formatting options from a Dataset. And it is here that my troubles begin. I have code that does this in a TOTALLY HACKY way that has run into some issues already. Here's the idea: Basically convert the Dataset to a String, use RegularExpressions to fiddle with it that way, and then convert the manipulated string back into an expression. I know. Awful.

ExistingOptions[ds_Dataset] := 
 Normal@KeyDrop[
   ToExpression@
    StringCases[ToString@ds, RegularExpression["<\\|ID.+\\|>"]][[1]], 
   ID]

And then ...

Options[ReformatDataset] = 
  Join[Options[Dataset], {"PreserveOptions" -> False}];
ReformatDataset[ds_Dataset, opts : OptionsPattern[]] := 
 If[OptionValue["PreserveOptions"], 
  Dataset[Normal[ds], 
   Sequence @@ FilterRules[{opts}, Options[Dataset]], 
   ExistingOptions[ds]], Dataset[Normal[ds], opts]]

But surely there is a better way of getting the existing options out. I've poked about the Dataset` context to see if there are any undocumented functions but couldn't find anything. Anyone have ideas?

POSTED BY: Seth Chandler
3 Replies
Posted 3 years ago

Hi Seth,

How about using Cases on ToBoxes@dataset. Looks like the options are stored in rule of the form "Meta" -> Association, where the association has the ID and options.

POSTED BY: Updating Name

Thanks for the idea. Unfortunately, I can not get that to work. For some reason Cases is not penetrating the TagBoxes and the like that occur when you use ToBoxes. The code below, for example, returns an empty list even though if you actually look at the "ToBoxes" expression the "Meta" stuff is there with exactly what I want.

Cases[ToBoxes[Dataset[{{4}},Background->Blue]],HoldPattern["Meta"->_],DirectedInfinity[1]]

Further help would still be useful to me. Really, there should be something in at least the Dataset` context to get this stuff out, but I am hopeful that someone is savvy enough to figure out a good way of getting the formatting out.

POSTED BY: Seth Chandler
Posted 3 years ago

Hi Seth,

My bad, I should have tried it before suggesting. You are right, I tried several different ways to match the pattern but they all failed, strange. Maybe worth posting this question on MSE. This hack works, but is likely quite brittle.

ToBoxes[Dataset[{{4}}, Background -> Blue]][[1, 1, 1, 1, 1]]["Meta"]
POSTED BY: Rohit Namjoshi
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract