3.3 Structuring your app

A shiny app has two main components: - Application logic is what makes your {shiny} app interactive - Business logic includes the components with the core algorithms and functions that make your application specific to your area of work

3.3.1 Small is beautiful (bis repetita)

  • Long scripts are almost always synonymous with complexity when it comes to building software

3.3.2 Conventions matter

app_*.R (typically app_ui.R and app_server.R) contain the top-level functions defining your user interface and your server function.

fct_* files contain the business logic, which are potentially large functions. They are the backbone of the application and may not be specific to a given module. They can be added using {golem} with the add_fct(“name”) function.

mod_* files contain a unique module. Many {shiny} apps contain a series of tabs, or at least a tab-like pattern, so we suggest that you number them according to their step in the application. Tabs are almost always named in the user interface, so that you can use this tab name as the file name. For example, if you build a dashboard where the first tab is called “Import”, you should name your file mod_01_import.R. You can create this file with a module skeleton using golem::add_module(“01_import”).

utils_* are files that contain utilities, which are small helper functions. For example, you might want to have a not_na, which is not_na <- Negate(is.na), a not_null, or small tools that you will be using application-wide. Note that you can also create utils for a specific module.

*_ui_*, for example utils_ui.R, relates to the user interface.

*_server_* are files that contain anything related to the application’s back-end. For example, fct_connection_server.R will contain functions that are related to the connection to a database, and are specifically used from the server side.