rpharma-demo icon indicating copy to clipboard operation
rpharma-demo copied to clipboard

Why do we need () at end of callModule(bookmark_mod, ...)?

Open rpodcast opened this issue 5 years ago • 3 comments

@jcheng5 I tried using this approach in a new app at the day job and I was going nuts trying to figure out why my app would never enter the customized onBookmarked() callback within the bookmark module (i.e. the part that writes to the SQLite database):

https://github.com/jcheng5/rpharma-demo/blob/1e8bd67851e0a4c3e62de277c49512f7e1d7e421/modules/bookmark_module.R#L131-L161

It turns out that when I invoked the bookmark module's server function, I forgot to put the () at the end like you did in this repo's example app:

https://github.com/jcheng5/rpharma-demo/blob/1e8bd67851e0a4c3e62de277c49512f7e1d7e421/app.R#L69-L71

I should know this, but I still can't figure out why you need to add the () at the end of this call? Does it have to do with the module returning a function that wraps the onBookmarked callback?

rpodcast avatar Apr 18 '19 19:04 rpodcast

I don't have perfect recall of this but I think there's a problem with calling onBookmark inside of a module (i.e. when callModule is still on the callstack), if you're intending to bookmark stuff from outside that module? Something like that?

If that's not enough of a hint for you to connect the rest of the dots, let me know and I'll take a closer look.

jcheng5 avatar Apr 18 '19 20:04 jcheng5

You are certainly right about calling onBookmark inside a module, as I got that error numerous times in my debugging!

To give more context what I'm trying to do with my latest attempt: Yes I certainly want to have a section of the UI devoted to saving and loading state, much like your example app. But I also want to have an "auto-bookmark" functionality after a key system process has completed (in this case a HPC job). That HPC submission piece is in a separate module, and it has a text input for the user to name the session before launching the job. In the ideal case I want to do a customized version of onBookmarked for each of these two modules, so that they can have their own "naming" of session widgets respected. In one of the last examples of shiny::onBookmark() it does something similar albeit for different callbacks. I have contingency plans in place if I can't get the separate modules to do their own version of the onBookmarked callback, but it would be nice to get this part working out-of-box.

rpodcast avatar Apr 19 '19 14:04 rpodcast

I ended up using one of my "contingency plans" to get a solution for my immediate use case:

  • There is only one onBookmarked callback defined, and it's in the bookmark module
  • To get around the fact that the callback function only takes the url parameter, I define a custom option for a session name. For the module that performs the calculations, I'm able to define the value of that option to some session name (that module has its own text input for the user to name the session). The onBookmarked callback checks for a non-null value of that option. If it is present, then it will simply use that for the session name. If NULL, it will look for the text input value of the session name from the bookmark module UI directly.
  • I do have to be careful to reset that option value to NULL after I perform a restore so that any subsequent state saving will have a "clean slate" to work with.

Probably not elegant, but it was enough to get the job done for now.

rpodcast avatar Apr 25 '19 02:04 rpodcast