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?