Best Practices: Shiny Development

All of the shiny apps I'm currently maintaining and developing are embedded in packages. The package directory follows the general format of

pkg_name/
    - inst/
        - Application/
            - www/
            - global.R
            - server.R
            - ui.R
    - man
    - R

Each package includes a launch_application function that uses the system.file utility to find the Application folder and start the app. (I'm sure there are better ways of doing this, but my organization as weird technical policies)

This isn't much different than sourcing a file with all of the utility functions. I like doing it because it encourages me to write documentation for my functions (which I often appreciate when I have to come back to do maintenance or debugging), and it lets me use the devtools package to perform checks and tests. Checks and tests are useful for preventing problems before the app ever gets to production.

I build a lot of UI elements through functions. I should probably use modules more than I do, but I haven't yet gotten comfortable with designing modules that handle unique and diverse data structures yet.

When actually coding the app, I break the app into functional sections based on the UI. For example, if I have multiple tabs, I will place all of the code in server.R relevant to that tab in one place in the code. My server.R file usually follows the structure

shinyServer(function(input, output, session){
# Global Variables ---------------------------------------
# Reactive Lists ------------------------------------------
# Reactive Values ---------------------------------------
# General Observers -----------------------------------

# Tab 1 -----------------------------------------------------
# Reactive Values ---------------------------------------
# Observers -----------------------------------------------
# Event Observers --------------------------------------
# Output Elements --------------------------------------
# Download Handlers ----------------------------------

# Repeat the above structure for each tab/grouping

# Output Options
})
7 Likes