Shadow issue while retrieving package from palette action

My simple first palette assumes:

  • Add My Packages Path to $Path

ActionMenu["test",{"The Simple Packages Path":> AppendTo[$Path, ToFileName[{NotebookDirectory[], "02 Simple Packages"}]]}]

  • Get my packages

ActionMenu["Load Packages", {"Get my package":> Get["myPackage`"]}]

  • A place on the selected input cell (or on a new input cell), a specified input expression containing different place holders.

OpenerView[{"my Package", Button["construct", Paste[ StandardForm@Defer @construct[Placeholder["description"],Placeholder["another description"]]]]}]

The problem is that I continue to receive “shadow” messages when I click on the “Get my package” menu item. And I'm sure that I don't download the package intentionally twice. When I click on "construct", he writes Global`construct["description","another description"] . But I'm sure I did not define it before receiving the package (I killed the kernel in my tests).

Do you know what is wrong?

(I use Get for my packages instead of Needs to provide a clean start to the package context)

Also: do you know an easier way to do Paste[ StandardForm@Defer... , which ensures that the expression being the insert is not evaluated and that it enters the input cell even if that cell is not selected?

+6
source share
2 answers

Well, it looks like your problem is with the interaction between parsing and creating an interface. What would you like in this case, to delay the parsing of package symbols in your interface - building code (package symbols that you use in the button action functions), from the time the interface was created until the click button is pressed (when provided that the package has been downloaded by this time). Here is one way to do this:

 Column[{ActionMenu["Load Packages", {"Get my package" :> Get["ANOVA`"]}], OpenerView[{"ANOVA", Button["construct", With[{sym = Symbol["ANOVA"]}, Paste[ StandardForm@Defer @sym[Placeholder["DATA"]]]]]}]}] 

What we have done here is to use With to enter a character in the code for the button function. But during the analysis of the interface code, we prevent the creation of the Global symbol with this name - this is what happens differently, and this is exactly what causes your problem.

EDIT

If you know for sure that you use only symbols (functions) from packages, and not from the Global' context, here is the version that will be "protected" from this problem: it will be Remove generated symbol if its context turns out to be Global' - and thus , clicking the button before downloading the package will only lead to a warning (I use the package symbol to attach a message to it), you should replace it with any name of your interface creation function):

 package::noload = "Please load the package containing symbol `1`"; Column[{ActionMenu["Load Packages", {"Get my package" :> Get["ANOVA`"]}], OpenerView[{"ANOVA", Button["construct", With[{sym = Symbol["ANOVA"]}, If[Context[sym] === "Global`", Message[package::noload, Style[ToString[sym], Red]]; Remove[sym];, (* else *) Paste[ StandardForm@Defer @sym[Placeholder["DATA"]]]]]]}]}] 
+6
source

Well, I don't have your package, so for testing, I changed the action menu to get the ANOVA package:

 ActionMenu["Load Packages", {"Get my package" :> Get["ANOVA`"]}] 

ANOVA[{{1, 1}, {1, 2}, {2, 4}, {2, 3}}] now works without a hitch. No complaints about shading. This suggests that the cause of your shadowing problem lies elsewhere. I noticed, however, that the word ANOVA remains blue. This will be due to the issue you reported here .

+2
source

Source: https://habr.com/ru/post/894815/


All Articles