Search code examples
rshinyinputbinding

Calling "bind"-ed input from server.R


I successfully implemented the binding capability of Shiny, but the function is defined in ui.R and any calls from server.R of this new input result in a function not found error.

I am passing session to server.R (not sure if that makes a difference) and I also created a global.R that simply has the function in it (not sure if I need anything beyond that, because that doesn't even let me call the function in ui.R).

How can I call a custom shiny input function from server.R (specifically in a renderUI)?

Example code here: (the call to sortList works fine from ui.R but not server.R) https://gist.github.com/jpd527/9687359


Solution

  • Here's a patched Gist: https://gist.github.com/trestletech/9691459

    There are two major changes here:

    1. I moved the sortListInput function to global.R. You mention you'd tried that, but it worked swimmingly to expose the function to both server and UI.
    2. The next issue was that you were only initializing the sortListInput at page creation. You had $( ".sortableList" ).sortable(); $( ".sortableList" ).disableSelection(); in a singleton <script> in your <head>, but that's only going to run once at page creation. So dynamically created sortableLists that get created later would never be initialized. So I added a <script> chunk to your sortListInput that will run that code every time you generate a new one. I'm unaware of a cleaner way to do this right now, but perhaps one does/will exist...

    A couple of things to be aware of:

    • Since you're now initializing all sortListInputs each time one gets dynamically created, you need to be sure that that init code is idempotent -- i.e. you're not going to break existing ones when you run that code.
    • I noticed that you're loading your own version of jQuery and some other jQuery libraries. Be careful about that since Shiny also has a version of jQuery it depends on. You'll need to be sure there aren't any conflicts.