From: Rui on
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

From: Hannes Kessler on
On 13 Apr., 05:01, Rui <rui.r...(a)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

From: Albert Retey on
Hi,

> 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)

I think your problem are namespaces and the time when symbols are
created: When the Manipulate is seen, the variables IListPlot and SS
will be created as variables in the Global` context, and only afterwards
the package is loaded, creating the same variables in the
AddOns`-Context. To prevent this there are several ways, one of the most
straightforward would be to use explicitly the full long names of the
symbols, that is (presumably) AddOns`IListPlot and AddOns`SS. There are
several other possibilities, like only creating the symbols when the
body is evaluated with something like:

Symbol["IListPlot"][Symbol["SS"][p, 100] @@ Range[100]]

for your ultimate goal to include everything in one notebook there seem
to be solutions, but I don't know of anything "simple"...

hth,

albert

From: Rui on
On Apr 13, 11:44 pm, Albert Retey <a...(a)gmx-topmail.de> wrote:
> Hi,
>
> > 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)
>
> I think your problem are namespaces and the time when symbols are
> created: When the Manipulate is seen, the variables IListPlot and SS
> will be created as variables in the Global` context, and only afterwards
> the package is loaded, creating the same variables in the
> AddOns`-Context. To prevent this there are several ways, one of the most
> straightforward would be to use explicitly the full long names of the
> symbols, that is (presumably) AddOns`IListPlot and AddOns`SS. There are
> several other possibilities, like only creating the symbols when the
> body is evaluated with something like:
>
> Symbol["IListPlot"][Symbol["SS"][p, 100] @@ Range[100]]
>
> for your ultimate goal to include everything in one notebook there seem
> to be solutions, but I don't know of anything "simple"...
>
> hth,
>
> albert

Yeah, true. But that issue solved itself when I made sure I had called
Needs["AddOns`"] befoore calling the Manipulate.
I had just assumed that since I expected Mathematica to evaluate the
Initializatoin part befooore the Manipulate expression, I'd had no
trouble. But now I guess that even when Mathematica doesn't evaluate
something, it still translates the symbols to the full context as soon
as it can. Not sure, hehe

From: Rui on
On Apr 13, 11:43 pm, Hannes Kessler <HannesKess...(a)hushmail.com>
wrote:
> On 13 Apr., 05:01, Rui <rui.r...(a)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

Very helpful. I was definately needing the AbortProtect tobreak
through my frustration on the issue