Message Boards Message Boards

Using FilterRules to pass Options from a wrapper function to a Button

Posted 2 years ago

For the moderators please have patience. I did look through other previously answered posts similar to this but none worked and of course I made several adjustments with my own code and nothing worked. I even tried ArgumentsOptions and CheckArguments but the docs are very poorly written. It's difficult to determine if they go in the definitions or if an end-user should wrap his functions in them.

Below is the standard vanilla method shown in the docs for using FilterRules. They use Plot I use Button.

Options[fn1] = {"background" -> Automatic, "color" -> Black, 
   "appearance" -> Automatic, , "label" -> "Echo Chamber", 
   "size" -> {90, 30}};
fn1[opts : OptionsPattern[]] := With[{
    bg = OptionValue["background"],
    col = OptionValue["color"],
    app = OptionValue["appearance"],
    lab = OptionValue["label"],
    siz = OptionValue["size"]
    },
   action[] := 
    Echo["an \"echo chamber\" is one who often repeats unoriginal \
ideas."];
   Button[lab, action[], ImageSize -> siz, BaseStyle -> {col}, 
    Appearance -> app, Background -> bg, 
    FilterRules[{opts}, Options[Button]]]
   ];
(* generates 3: OptionValue: Unknown option Evaluator for fn1 *)
(* generates 2: OptionValue: Null is not a valid *)
(* but does render a working button *)
fn1[Evaluator -> Automatic]

(* generates 2: OptionValue: Null is not a valid *)
(* but does render a working button *)
fn1[]

Then I tried a slight variation and that failed too.

Options[fn2] = {"background" -> Automatic, "color" -> Black, 
   "appearance" -> Automatic, , "label" -> "Echo Chamber", 
   "size" -> {90, 30}};
fn2[opts : OptionsPattern[]] := With[{
    ops = FilterRules[{opts}, Options[Button]],
    bg = OptionValue["background"],
    col = OptionValue["color"],
    app = OptionValue["appearance"],
    lab = OptionValue["label"],
    siz = OptionValue["size"]
    },
   action[] := 
    Echo["an \"echo chamber\" is one who often repeats unoriginal \
ideas."];
   Button[lab, action[], ImageSize -> siz, BaseStyle -> {col}, 
    Appearance -> app, Background -> bg, 
    If[Length[ops] == 0, Sequence @@ ops, ops]]
   ];
(* generates 3: OptionValue: Unknown option Evaluator for fn2 *)
(* generates 2: OptionValue: Null is not a valid *)
(* but does render a working button *)
fn2[Evaluator -> Automatic]

(* generates 2: OptionValue: Null is not a valid *)
(* but does render a working button *)
fn2[]

What am I doing wrong?

POSTED BY: Jules Manson
2 Replies

Jules,

Your errors mostly have to do with a blank item in the option list (you have two commas in a row). Additionally, unless you specify that the options of Button are also options of fn1, it will not work.

This works:

ops = Options[Button];
Options[fn1] = 
  Join[ops, {"background" -> Automatic, "color" -> Black, 
    "appearance" -> Automatic, "label" -> "Echo Chamber", 
    "size" -> {90, 30}}];
fn1[opts : OptionsPattern[]] := 
  With[{bg = OptionValue["background"], col = OptionValue["color"], 
    app = OptionValue["appearance"], lab = OptionValue["label"], 
    siz = OptionValue["size"]}, 
   action[] := 
    Echo["an \"echo chamber\" is one who often repeats unoriginal \
ideas."];
   Button[lab, action[], ImageSize -> siz, BaseStyle -> {col}, 
    Appearance -> app, Background -> bg, 
    FilterRules[{opts}, Options[Button]]]];

fn1[Evaluator -> Automatic]


fn1[]

Regards,

Neil

POSTED BY: Neil Singer
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