Build custom output objects
Right out of the box, Shiny makes it easy to include plots, simple tables, and text as outputs in your application; but we imagine that you’ll also want to display outputs that don’t fit into those categories. Perhaps you need an interactive choropleth map or a googleVis motion chart.
Server-Side Output Functions
Start by deciding the kind of values your output component is going to receive from the user’s server side R code.
Whatever value the user’s R code returns is going to need to somehow be turned into a JSON-compatible value (Shiny uses jsonlite to do the conversion). If the user’s code is naturally going to return something jsonlite-compatible – like a character vector, a data frame, or even a list that contains atomic vectors – then you can just direct the user to use a function on the server. However, if the output needs to undergo some other kind of transformation, then you’ll need to write a wrapper function that your users will use instead (analogous to
For example, if the user wants to output time series objects then you might create a
renderTimeSeries function that knows how to translate
ts objects to a simple list or data frame:
which would then be used by the user like so:
Design Output Component Markup
For many components, you’ll be able to have extremely simple HTML markup, something like this:
We’ll use the
timeseries-output CSS class as an indicator that the element is one that we should bind to. When new output values for
Write an Output Binding
Each custom output component needs an output binding, an object you create that tells Shiny how to identify instances of your component and how to interact with them. (Note that each instance of the output component doesn’t need its own output binding object; rather, all instances of a particular type of output component share a single output binding object.)
An output binding object needs to have the following methods:
Given an HTML document or element (
scope), find any descendant elements that are an instance of your component and return them as an array (or array-like object). The other input binding methods all take an
elargument; that value will always be an element that was returned from
A very common implementation is to use jQuery’s
findmethod to identify elements with a specific class, for example:
Return the Shiny input ID for the element
nullif the element doesn’t have an ID and should therefore be ignored. The default implementation in
data-input-idattribute and falls back to the element’s
idif not present.
Called when a new value that matches this element’s ID is received from the server. The function should render the data on the element. The type/shape of the
dataargument depends on the server logic that generated it; whatever value is returned from the R code is converted to JSON using the
Called when the server attempts to update the output value for this element, and an error occurs. The function should render the error on the element.
erris an object with a
If the element
elis currently displaying an error, clear it.
Register Output Binding
Once you’ve created an output binding object, you need to tell Shiny to use it:
The second argument is a string that uniquely identifies your output binding. At the moment it is unused but future features may depend on it.
We love it when R users help each other, but RStudio does not monitor or answer the comments in this thread. If you'd like to get specific help, we recommend the RStudio Community as well as the Shiny Discussion Forum for in depth discussion of Shiny related questions and How to get help article for a list of the best ways to get help with R code.comments powered by Disqus