Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Save user-defined functions along with MizerParams object #171

Closed
gustavdelius opened this issue Aug 7, 2020 · 3 comments
Closed

Save user-defined functions along with MizerParams object #171

gustavdelius opened this issue Aug 7, 2020 · 3 comments
Labels
enhancement New feature requests or suggestions for enhancement of existing features minor effort Resolving this issue will involve a minor effort

Comments

@gustavdelius
Copy link
Member

I propose new functions saveParams() and readParams() for saving MizerParams objects. Currently one would use the generic saveRDS() and readRDS(), which are fine as long as the MizerParams object does not contain any user-defined functions (either rate functions registered with setRateFunction() or component functions registered with setComponent() or a resource dynamics function registered with setResource() and density-dependence function registered with setReproduction()). For these functions, only their name is saved by saveRDS(), not their definition. That is fine if the saved params object is always shared together with an R script defining those functions. But it would be nice to be able to save all that is necessary to simulate the model in one place. This is what the function saveParams() should do. readParams() would then also read in those function definitions.

Perhaps we should also have functions saveSim() and readSim() to save and read MizerSim objects.

For a discussion of why the MizerParams object holds the names of the user-defined functions rather than the functions themselves, which one might have thought would circumvent the issue, see #91 . For a discussion of other meta data that we might want to include in a saved MizerParams object, see #45.

@gustavdelius gustavdelius added enhancement New feature requests or suggestions for enhancement of existing features minor effort Resolving this issue will involve a minor effort labels Aug 7, 2020
gustavdelius added a commit that referenced this issue Aug 31, 2021
…s class and setting them to defaults in `emptyParams()`.
@gustavdelius
Copy link
Member Author

One thing to think about is whether loadParams() should use load() to put the restored functions into the current environment, or whether it should use attach() to put everything into its own environment on the search path.

gustavdelius added a commit that referenced this issue Sep 1, 2021
…he issue about what environment to load into still needs thought. Currently `loadParams()` is just doing `load()`.
@gustavdelius
Copy link
Member Author

It turns out that there does not seem to be an easy and reliable way to save a function with all its dependencies. There is a function codetools::findGlobals() that can find the names of all objects that a function depends on, but one would then need to find out which of those need to be saved because they are not part of a package and then one would need to find and save their dependencies as well.... I don't think it would be wise to try. And saving the functions without making sure their dependencies are also saved would not be good.

So saveParams() now only checks if the model uses external functions and alerts the user to the fact that they need to share these function definitions separately in an R file. It then saves only the MizerParams object via saveRDS(). readParams() simply reads a file with readRDS() and if necessary prompts the user to install and load the required extension packages and it alerts them in case there are external functions used in the model for which they need to get the definitions. The result is also passed through validParams() which will take care of any upgrading of the params object if needed.

@gustavdelius
Copy link
Member Author

So there are now saveParams() and readParams() functions (read rather than load because it does not load the object into the namespace but instead returns it as a value). However they do not save or load the user-defined functions, for the reasons explained above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature requests or suggestions for enhancement of existing features minor effort Resolving this issue will involve a minor effort
Projects
None yet
Development

No branches or pull requests

1 participant