What makes a Shiny App professional?

Do you want to make your Shiny App robust? Have you ever wished for a professional prototype?

Let us tell you a few good tips on how you could improve your Shiny coding skills.

1. Master reactivity

Understand the principles of the reactive programming model used by Shiny. Reactive sources (input), usually a user action in the UI, set the value, which is rendered (or observed) in a endpoint like a plot. The reactive components between source and endpoint are lazy, i.e. are re-run only if the results are invalidated, caching them otherwise. However, beware of the dependency structure, which can get quickly complicated if you use many reactive expressions.

Shiny

2. Structure your app

Create the Shiny App within an R package. As complexity increases, split the logic and big calculations from the UI and Server into new modules and functions, separating concerns and allowing re-usability. Use a css file for a personalized design and style formatting. Add the css file as well as other extra resources in the www directory.

3. Test

Automate unit testing: you can and should use testing packages like shinytest, testthat, which will help guarantee that individual functions keep working as expected. It’s a time investment upfront, but it will save you hours of debugging and struggling. You can add some integration tests to make sure the interactions between reactives remain smooth. Run some performance tests as well.

4. Manage the dependencies

Define the dependencies within the package. You can use renv to ensure using dependent packages with specific versions.

5. Use modules

If you are going to copy-paste a functionality, create a module instead. Organize your code based on input and output modules. Modules will help you avoid ID conflicts. Shiny modules are isolated and re-usable pieces of code, acting as a coordinator between the UI and the back-end. If necessary you can create a module that itself contains a module: modules are composable.

6. Debug

Unfortunately at some point something will go wrong. You might get an unexpected error, the results might be weird or the values might not get re-evaluated. Read the tracebacks to spot the issue, use the interactive debugger, with the help of browser() figure out what is causing the issue. You probably already know this if you are familiar with R scripting, but the specific Shiny issues are more challenging. If a reactive does not do what it is supposed to, then use the basic technique of message() to print the calculated value.

7. Ease the maintenance

Document the code, make sure to explain complex chunks of code. Select meaningful variables, functions and files names, adopt a naming convention. Get inspired by R best practice syntax.


Download infographic

Share on: