How does a web app work?

A web app is a thing that updates based on user input/interaction.

Most web apps have two parts:

Shiny

Shiny is an R package that allows you to write web applications.

UI

Some inputs for the UI

  • pickerInput()

Server

Reactive programming

Reactives

  • Used to calculate a specific value
  • Execute only once, and otherwise cache the value returned
  • Can be sources, endpoints or conductors

Reactive Source: User input that comes through a browser interface, typically. reactive sources are accesible through any input$x

Reactive conductor: An intermediate that depends on reactive sources, and/or updates reactive endpoints. Conductors are good for slow or expensive calculations, and are placed between sources and endpoints.

Reactive endpoint: output that typically appears in the browser window, such as a plot or a table of values. Are accesible through any output$y, and are observers, primarily used for their side effects, and not directly to calculate things.

Reactive expressions: reactive expressions are lazy and cached. the expression behaves just like a function but with two key differences:

  • It’s lazy: Evaluated only when it is called, typically by a reactive endpoint.
  • It’s cached: Evaluated only when the value of one of its underlying dependencies changes.

Obsevers

  • Used for their side effects
  • Does not cache anything

Observers vs reactives

Role

  • reactive() is for calculating values, without side effects.
  • observe() is for performing actions, with side effects

Differences

  • return values: reactive expressions return values, but observers don’t
  • Evaluation: Observers eagerly respond to changes in their dependencies, while reactive expressions are lazy
  • Side effects: Observers are primarily useful for their side effects, wherease, reactive expressions must NOT have side effects.

Stop, delay, trigger

  • stop: The isolate({}) function allows to read a reactive value separately of the other inputs, then only the update will be place when the non-isolated input will be changed.

  • delay: In some situations we might want more explicit control to trigger the update. We can achieve this using eventReactive(). is used to create a calculated value that only updates in response to an event.

  • trigger: There are times when you want to perform an action in response to an event. We can achieve this with observeEvent(). Unlike eventReactive, observeEvent is used only for a side effects and it does not return any value.

Custom error messages

CEM are great way to nudge users and to using the app correctly.

  • validate()
  • need(logical expression (input), error message )

Other shiny tools

  • shinyWidgetsGallery(): opens a pre-built Shiny app that allows you to explore these pre-built inputs and gives you the code for implementing them

  • bootstrapPage(): full page with no margins