8

With this question I'm only interested in obtaining some clarity on a best approach to using packages while working with a Shiny app. Despite the fact that, contrary to good practice on asking R-related questions, the question does not contain code or reproducible example, I hope that it touches on practical and relevant matters.

Problem

I'm working on a modular Shiny app that has the following structure:

  • server.R - contains some key functions and first few initial graphics
  • ui.R - provides basic user interface framework
  • data - folder with some data files that are not sourced dynamically
    • list.csv - sample file with data
    • ... - other data files
  • functionsAndModules - folder with *.R files pertaining to functions and modules
    • functionCleanGeo.R - simple function cleaning some data frames of format: cleanDataFrame <- function(data) { ... return(cleanDta) }
    • moduleTimeSeries.R - module providing time series analysis doing the following things:
      • generating user interface
      • sourcing data
      • generating charts
    • ...R - other modules and functions saved as *.R files.

Libraries

What I would like to know is how to approach loading packages that would be most optimal for the app structure outlined above. In particular, I would like to know:

  1. When it's sufficient to load libraries only in global.R and when (if at all) it may be required to load libraries across module files and/or server.R / ui.r?

    1.2. For example when using shinyTree package I load it in server.R and ui.R as, it is my understanding that this flows from examples. Modules and functions use dplyr / tidyr combination, would it be sufficient to load those packages in global.R?

  2. My preferred method for loading packages looks like that: Vectorize(require)(package = c("ggvis", "SPARQL", "jsonlite", "dplyr", "tidyr", "magrittr"), character.only = TRUE), will it work fine with the architecture described above?

SeGa
  • 9,454
  • 3
  • 31
  • 70
Konrad
  • 17,740
  • 16
  • 106
  • 167

1 Answers1

3

Here my two cents:

  • Packages to load need to be declared at the start of the app.R file, at the server side of the app.R or alternatively at the modules file.
  • At the modules, the libraries can be declared either outside of the fooUI and fooServer module functions or inside. Both works.
  • For re-usability of the modules, I prefer to declare them at the start of each module*.R file.
  • At least for me, libraries loaded on the global.R file, do not get to the modules, so they can run. My hypothesis is that it doesn't load on the app.R environment. I understood that the global.R is to load share objects from serverand ui and to set some configuration.
FoFaiFo
  • 54
  • 6
  • Thanks, very useful comments. I prefer to use to file architecture `ui.R` / `server.R`; would you then suggest pushing everything into the two files? I'm thinking whether this is efficient as the `require` / `load` calls would be executed twice. Your answer provides some useful insight and valid suggestions so in the absence of more comprehensive answers I will be happy to accept. – Konrad Sep 05 '16 at 14:29
  • 1
    If you have the structure `ui.R` / `server.R`, I will suggest to declare them at the `ui` side. Worst case scenario you can add them in both, although it will be nice to understand the internals of Shiny. I need to stick to some structure for the modules, specially since I used them in several apps, so the idea to declare the libraries in each of them, adds verbosity and readability to the code. – FoFaiFo Sep 05 '16 at 15:27
  • I reckon that some guidelines would be helpful. For instance, a common package, let's say `ggplot2` may be used across modules. Should it be then loaded in `ui.R` only once? Would it be possible to avoid loading a package that may be used in a specific module that user may not interact with? – Konrad Mar 20 '18 at 11:48
  • @Konrad I will add them at the `ui.R` side. As far as I know, either you add verbosity and risk not to have a function package loaded. Don't know a way to avoid redundancy. Since shiny with modules is already complex enough, I go for the verbosity.. – FoFaiFo Mar 21 '18 at 12:31
  • I load all libraries in global.R, and nothing in ui/server.R, but I'm not using modules. – SeGa Oct 21 '18 at 00:48