MathGroup Archive 2010

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Using package functions in Manipulate/DynamicModule

  • To: mathgroup at smc.vnet.net
  • Subject: [mg109140] Re: Using package functions in Manipulate/DynamicModule
  • From: Hannes Kessler <HannesKessler at hushmail.com>
  • Date: Mon, 19 Apr 2010 04:10:21 -0400 (EDT)
  • References: <hq0mqd$k0u$1@smc.vnet.net>

On 13 Apr., 05:01, Rui <rui.r... at gmail.com> wrote:
> Hey. My objective is to deploy in a Mathematica Player file a document
> which has some interactive stuff, without any code visible. Ideally,
> it would be all in a single file but I can live with sending a .m
> package file too. So far so good.
>
> Inside a DynamicModule I used some functions from a .m file I had
> created some time ago. As expected, when I close the file and reopen
> it, the dynamic gives errors everywhere.
>
> First I thought there might be something magical similar to
> SaveDefinitions that would automatically store in the cell everything
> it needs to work fine. But I coulnd't find it. I tried wrapping it up
> in a Manipulate with SaveDefinitons->True but doesn't work either.
>
> So, I decided to try for the cell to just load the package whenever it
> needs to be displayed for the first time.
> "Initialization:>Needs["AddOns'"]" seemed to be perfect. However, it
> doesn't work as expected.
> For example: (SS and IListPlot are from AddOns.m)
> Manipulate[
> IListPlot[SS[p, 100] @@ Range[100]], {p, 1, 100, 1},
>  Initialization -> Needs["AddOns`"], SaveDefinitions -> True]
> doesn't work and tells me there are double definitions in Global` and
> AddOns` of IListPlot and SS (right after starting the kernel)
>
> The few times I've tried to use "Initialization" it gave me a
> headache. I would have thought it just executes that whenever the
> dynamicmodule/manipulate is displayed for the first time, but evidence
> proves it isn't that simple, and I don't get it.
>
> Perhaps I should put all of my package's variables in the
> DynamicModule and copy the whole package inside?
> When something that strikes me as useful and common is getting
> convoluted and messy, it's a sign I'm overseing something and needing
> your help.
>
> Thanks a lot! ;D

Try the following:

Needs["AddOns`"];
DynamicModule[{p},
 Column[{
   Slider[Dynamic[p], {1, 100, 1}] ,
   Dynamic  @  IListPlot[SS[p, 100] @@ Range[100]]
   }],
 Initialization :> AbortProtect[Needs["AddOns`"]]]

The first Needs makes sure the package is loaded when the
DynamicModule is created.
The second Needs initializes the package after quitting Mathematica
and re-opening the notebook.
It is important to use a RuleDelayed and not a Rule: Initialization ->
Needs["AddOns`"] loads the output of Needs["AddOns`"] generated when
the DynamicModule is created. This output is Null. Thus the package
would not be loaded when the notebook is re-opened after quitting
Mathematica.
AbortProtect prevents time out errors in case the package needs a
longer time to load.

Best regards,
Hannes Kessler


  • Prev by Date: Re: if using Mathematica to solve an algebraic problem
  • Next by Date: Re: Root again
  • Previous by thread: Re: Using package functions in Manipulate/DynamicModule
  • Next by thread: Selecting subscripted variables from an expression