Htmlwidgets with shiny input

htmlwidget

#1

Hello,

I have the following question.
I am trying to learn htmlwidgets by creating bindings to the select2 js library
(https://select2.org/) and bootstrap select library, together with a bunch of utilities to make the creation and use of selectors from R fully featured in both R markdown and shiny.
I do understand the basics of the htmlwidgets now, but I still wonder about one aspect.
I would like my widget to be usable both in R markdown and shiny. In R markdown it would operate as a normal widget. In shiny, however, it should be able to send data back to R. For instance, if the user selects an item from the selector widget, this should be available to R; in other words, the widget needs to send input back to R.

Now I was looking for hints about how to solve this and I found the following:

https://shiny.rstudio.com/articles/building-inputs.html

And from @daattali blog:

Now these two methods of sending input back to shiny from an htmlwidget seem at first sight very different to
me (although I'm just starting with shiny so that might be the reason). I was wondering if you have any suggestion on the best way to implement this. Is it best to create an htmlwidget which in a shiny context also behaves like a custom shiny input, or to use @daattali method?

Best,
Riccardo.


#2

It all depends. What do you intend to do with a custom input for R markdown? Who/what is going to process that input? Are you planning for R users to write their own JavaScript logic for dealing with the input?


#3

Hello,

the idea should be:
when run as an htmlwidget in R markdown, the widget does not send any input back to R (there's no R session anyway so that's not possible). It just takes as inputs, say, a named list of selection options and a list of ggplot2 plots/other widgets associated with each selection option, and allows the user to display plots depending on the selection made in the selector.

Currently I do this kind of things by injecting the js code directly into the R markdown, but that's quite hacky and I would like to have a widget that makes the process easier and streamlined. The user shouldn't need to use any js.

In a shiny context, however, I would like the widget to retain the above behaviour, but to also act as a shiny input, so that the developer could access the user selected value from R and use it in a reactive context in shiny to trigger other behaviour.

I hope this clarifies it a bit?