Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple action buttons with one event handler in Shiny?

Tags:

r

shiny

I'd like to have a variable number of identical actionButton()s on a page all handled by one observeEvent() function.

For example, in a variable-length table of tables, I'd like each interior table to have a button that links to more information on that table.

In standard HTML, you do this with a simple form, where you use a hidden input to designate the interior table number, like this:

<form ...>
  <input id="table_number" type="hidden" value="1"/>
  <input type="submit" value="Examine"/>
</form>

When a button is pressed, you can examine the hidden input to see which one it was.

Is there a way to do this in Shiny? The only solution I've come up with is to give each actionButton() it's own inputId. This requires a separate observeEvent() for each button. Those have to be created ahead of time, imposing a maximum number of buttons.

like image 433
3D0G Avatar asked Dec 15 '22 03:12

3D0G


1 Answers

It only took me a couple of years, but I now have a much better answer to this question. You can use a JavaScript/jQuery function to put an on-click event handler on every button in a document, then use the Shiny.onInputChange() function to pass the ID of a button (<button id="xxx"...) that has been clicked to a single observer in your Shiny code.

There's a full description with code examples at One observer for all buttons in Shiny using JavaScript/jQuery

like image 184
3D0G Avatar answered Jan 12 '23 16:01

3D0G