Skip to content

    Shiny (in R)

    Shiny is an R package that enables building interactive web applications that can execute R code on the backend. With Shiny, you can host standalone applications on a webpage, embed interactive charts in R Markdown documents, or build dashboards. You can also extend your Shiny applications with CSS themes, HTML widgets, and JavaScript actions.

    Shiny enables you to write powerful interactive web applications entirely in R. Using R, you create a user interface and server, and Shiny compiles your code into the HTML, CSS and JavaScript needed to display your application on the web. Since the application is executing R code on the backend, it can perform any R calculation you can run on your desktop. Perhaps you want your app to slice and dice a dataset based on user inputs. Or maybe you want your web app to run linear models or machine learning methods on user-selected data. In such cases, Shiny can help.

    The Shiny web framework fundamentally enables collecting input values from a web page, making those inputs easily available to the application in R, and having the results of the R code written as output values to the web page. In its simplest form, a Shiny application requires a server function to do the calculations and a user interface. Shiny applications have two components, a user-interface definition and a server script.

    Shiny Application Example

    Source: R Studio

    Reactive Programming

    Shiny comes with a reactive programming library that you use to structure your application logic. By using this library, changing input values will naturally cause the right parts of your R code to be re-executed, which will in turn cause any changed outputs to be updated. The reactive programming model eliminates the need for additional event handling code.

    In Shiny, there are three kinds of objects in reactive programming: reactive sources, reactive endpoints, and reactive conductors.

    • Reactive sources and endpoints: The simplest structure of a reactive program involves just a source and an endpoint. In a Shiny application, the source typically is user input through a browser interface. For example, when the selects an item, types input, or clicks on a button, these actions will set values that are reactive sources. A reactive endpoint is usually something that appears in the user’s browser window, such as a plot or a table of values.
    • Reactive conductors: It’s possible to put reactive components in between the sources and endpoints. Reactive conductors can be useful for encapsulating slow or computationally expensive operations.

    Starting out with Shiny

    You can install Shiny the usual way from your R console:

    install.packages("shiny")

    Additional Resources