The following notes were written for the Honolulu R Users Group and presented November 18, 2015.
An Introduction to reactive programming and shiny.
What is Shiny?
- An R package for building interactive web applications.
- Open source. Developed by R Studio.
- Rmarkdown and knitr are great but not interactive.
- Share data analysis without need for programming.
- Too much data to make simple overview plots.
- Models too complex for a few simple diagnostics.
- Working with non-R users.
- User facing applications built on top of R.
- Graphics generated using ggplot2
- Slider controls feedback input to ggplot2
- Plots regenerate when controls change
How does it work?
- Uses R as a backend for data.
- Use R data structures, R packages and R graphics.
- Supports integration with D3.js.
- Support for many libraries built on top of D3.
- Dygraphs, leaflet, Google charts and others.
- For running shiny code locally
- Shiny Package (on CRAN)
- For hosting web applications
- Linux server
- Shiny-server software
Simple example User Interface
Simple example Server Code
Shiny design flow
- User interface contained in
shinyUIfunction – often in its own file:
- Server logic contained inside
shinyServer– often in its own file:
- User interface, takes user input and displays output.
- Controls buttons, messages, menus etc.
- Server function take input from UI and has full access to R packages.
Shiny design flow 2
- New data entered from UI is automatically detected.
- New data prompts an internal update.
- All functions and plots using this data are regenerated.
- This is done automatically using reactively.
What is the value of
B at the end of this code segment?
- Normal R:
B == 101
- Reactive R:
B == 201
Shiny logic flow
- Data input from “Reactive Source”
- Typically user input from browser interface.
- Example: select an item, click a button, enter in a value.
- Data from “Reactive Source” processed using R
- Take user input and run further calculations.
- Example: calculate summary statistics on user input data.
- Data output as “Reactive endpoint”
- Reactive endpoint displays result to user.
- Example: Plot data after processing.
More complicated example
More complicated example
Server and UI Design
- UI does not contain any functions that process data.
- Server function has
- These are
reactiveValues– lists of reactive types.
inputis a reactiveValues list for data from the UI.
outputis a reactiveValues list with plots and output for the UI.
- These are
- Reactive Source:
reactiveValueslist from UI
- Reactive Endpoint:
reactiveValueslist to UI
Sessioncontains other metadata for shiny
- Reactive Source (from UI):
- These will send values to the server from UI.
- Reactive end points
- The reactive endpoint code in UI will have corresponding code in server.
reactiveis a type of reactive function.
- This function will run any time any of the reactive expressions inside the function change.
- In above example, this will detect when new time series data is available, validate the data and return it.
- Similar to
reactivefunctions but they are not a function and thus have no return output.
- Good way to update a reactiveValue based on a trigger (in this case button push).
- Reactive values must be handled in a “reactive context”.
- Reactive contexts include observers, reactive functions and reactive end points.
- Helper functions and objects can be defined outside of shiny.
- These functions will run once when the server first starts.
- Reactive code will rerun whenever the input changes.
- Support for text/numeric input, sliders, checkboxes, drop down menus, multi-selection
- See gallery: http://shiny.rstudio.com/gallery/widget-gallery.html
Output Display Types
- Support for the following output types: text, image, plots, tables.
- Example of data table: http://shiny.rstudio.com/articles/datatables.html