Create a Shiny app from a ga_model file

ga_model_shiny(
  models,
  template = ga_model_shiny_template("basic"),
  header_boilerplate = TRUE,
  title = "ga_model_shiny",
  auth_dropdown = c("ga4", "universal", "none"),
  web_json = Sys.getenv("GAR_CLIENT_WEB_JSON"),
  date_range = TRUE,
  scopes = "https://www.googleapis.com/auth/analytics.readonly",
  deployed_url = "",
  local_folder = "",
  ...
)

Arguments

models

The ga_model file location ("my_model.gamr") or a ga_model object - can pass in multiple as a list

template

The template Shiny files for the Shiny app - passed to shiny::runApp()

header_boilerplate

Whether to add header boilerplate to the template

title

The title of the Shiny app

auth_dropdown

What type of account picker to include

web_json

The client.id json file for Web

date_range

Most templates support a date_range global input for the data import functions, set this to FALSE to remove it

scopes

The scope the API requests will be under

deployed_url

If deploying Shiny app to a server, put the URL of the deployed app here so the authentication will redirect to the correct place

local_folder

If not empty, will not launch Shiny app but write code to the folder location you put here

...

Extra macro variables the template may support: a named list with the name being a template variable

Details

As ga_model objects have standardised code, they can be used to build standard templated Shiny apps. Templates are made using the whisker.render function

Some templates are included with the package, seen via ga_model_shiny_template("list")

Templates hold macro variables indicated via {{ macro_name }} in the Shiny app template code. See ga_model_shiny_template("basic_app", TRUE) for an example showing a minimal viable app. Templates can be files such as ui.R or app.R files; folders containing ui.R, app.R files; or ui.R with html files for advanced themes - see Shiny HTML templates. All additional files that may be in the folder are also copied over (such as global.R or www/ folders)

Templates contain code to allow multi-user login via Google OAuth2.

If your template is pointing at a file such as ui.R or app.R it will create an app.R Shiny object. If your template is pointing at a directory it will check for the presence of ui.R within the folder. In either case if the server.R is missing it will use the boilerplate version from ga_model_shiny_template("boilerplate")

By default the Shiny app is launched which in most cases will prompt authorisation for your Google Analytics. You can instead write the app out using local_folder to a valid location for deployment later.

Template macro variables

  • {{{ model_libraries }}}- Adds library() calls based on models$required_packages

  • {{{ web_json }}}- Adds Google OAuth2 client for web applications

  • {{{ scopes }}}- Adds Google OAuth2 scopes for the API calls

  • {{{ deployed_url }}}- Adds option(googleAuthR.redirect) option for deployed Shiny apps

  • {{{ model_load }}}- Adds ga_model_load calls loading all models in the list passed to this function's models argument. It creates R objects called 'model1', 'model2' etc. in the Shiny app code

  • {{{ model_list }}}- Adds a list of the model objects after model_load. Useful for creating custom functions in themes that can loop over model objects

  • {{{ shiny_title }}}- Adds the title to the Shiny app

  • {{{ auth_ui }}}- Adds the correct dropdown Shiny module for picking a GA4 or Universal Analytics properties

  • {{{ date_range }}}- Adds a shiny::dateInput() date selector with id "date_range" for use in model's data fetching functions

  • {{{ model_ui }}}- Adds the models UI elements as configured in the ga_model object. It uses the object loaded above via the model_load macro. It looks like model1$ui('model1') in the code.

  • {{{ auth_server }}}- Adds the authentication module's server side function

  • {{{ auth_accounts }}}- Adds a call to ga_account_list for the appropriate GA account type (GA4 or Universal)

  • {{{ model_server }}}- Adds the server side module for the models as configured in the ga_model configuration. It uses the object loaded above via the model_load macro. It looks like model1$server('model1') in the code.

  • {{{ model1 }}}- Alternative to model_load, this will load the model file location instead, which you can pass to ga_model_load() in the template. model1 is the first model passed, model2 the second, etc.

  • {{{ your_argument }}}- You can pass in your own custom variables to the template via the ... argument of this function if they are named the same as the template macro variable

Examples


# see Shiny templates included with the package
ga_model_shiny_template("list")
#>  [1] "argonDash/ui.R"                      
#>  [2] "basic/ui.R"                          
#>  [3] "basic_app/app.R"                     
#>  [4] "basic_bslib/ui.R"                    
#>  [5] "boilerplate/Dockerfile_app"          
#>  [6] "boilerplate/Dockerfile_ui_server"    
#>  [7] "boilerplate/deploy.R"                
#>  [8] "boilerplate/header_boilerplate.R"    
#>  [9] "boilerplate/server_app_boilerplate.R"
#> [10] "boilerplate/server_boilerplate.R"    
#> [11] "gentelella/ui.R"                     
#> [12] "html_based/template.html"            
#> [13] "html_based/ui.R"                     
#> [14] "html_based/www/dummy.js"             
#> [15] "shinydashboard/ui.R"                 
#> [16] "shinythemes/ui.R"                    

# see an example of an ui.R template with macros
ga_model_shiny_template("basic/ui.R", read_lines = TRUE)
#> [1] "## basic ui.R"                           
#> [2] "fluidPage(title = \"{{ shiny_title }}\","
#> [3] "                {{ auth_ui }},"          
#> [4] "                {{{ date_range }}},"     
#> [5] "                h2(\"Model Output\"),"   
#> [6] "{{{ model_ui }}}"                        
#> [7] ")"                                       

# see an example of an app.R template with macros
ga_model_shiny_template("basic_app/app.R", read_lines = TRUE)
#>  [1] "## basic_full ui.R"                                        
#>  [2] "library(shiny)"                                            
#>  [3] "library(googleAuthR)"                                      
#>  [4] "library(googleAnalyticsR)"                                 
#>  [5] "{{{ model_libraries }}}"                                   
#>  [6] ""                                                          
#>  [7] "gar_set_client(web_json = \"{{ web_json }}\","             
#>  [8] "               scopes = \"{{ scopes }}\", "                
#>  [9] "               activate = \"web\")"                        
#> [10] "options(googleAuthR.redirect = \"{{ deployed_url }}\")"    
#> [11] ""                                                          
#> [12] "# loads pre-existing models"                               
#> [13] "{{{ model_load }}}"                                        
#> [14] ""                                                          
#> [15] "## ui.R"                                                   
#> [16] "ui <- fluidPage(title = \"{{ shiny_title }}\","            
#> [17] "                {{ auth_ui }},"                            
#> [18] "                {{{ date_range }}},"                       
#> [19] "                h2(\"Model Output\"),"                     
#> [20] "{{{ model_ui }}}"                                          
#> [21] "                "                                          
#> [22] ")"                                                         
#> [23] ""                                                          
#> [24] "server <- function(input, output, session){"               
#> [25] "  "                                                        
#> [26] "  token <- gar_shiny_auth(session)"                        
#> [27] "  "                                                        
#> [28] "  {{{ auth_accounts }}}"                                   
#> [29] "  "                                                        
#> [30] "  # module for authentication"                             
#> [31] "  view_id <- {{ auth_server }}"                            
#> [32] "  "                                                        
#> [33] "  # module to display model results"                       
#> [34] "  {{{ model_server }}}"                                    
#> [35] "  "                                                        
#> [36] "}"                                                         
#> [37] ""                                                          
#> [38] "shinyApp(gar_shiny_ui(ui, login_ui = silent_auth), server)"

if (FALSE) {

# a universal analytics model using default template "basic"
ga_model_shiny(
  ga_model_example("decomp_ga.gamr"), 
  auth_dropdown = "universal")

# a template from a directory holding an app.R file
ga_model_shiny(
  ga_model_example("decomp_ga.gamr"), 
  auth_dropdown = "universal",
  template = ga_model_shiny_template("basic_app"))
  
  
# a template from only an ui.R file that will import boilerplate server.R
ga_model_shiny(
  ga_model_example("decomp_ga.gamr"), 
  auth_dropdown = "universal",
  template = ga_model_shiny_template("basic/ui.R"))

# a template from a custom html based theme
ga_model_shiny(
  ga_model_example("decomp_ga.gamr"), 
  auth_dropdown = "universal",
  template = ga_model_shiny_template("html_based"))

# a template using library(argonDash)
ga_model_shiny(
  ga_model_example("ga-effect.gamr"), 
  title = "Argon Demo",
  auth_dropdown = "universal",
  template = ga_model_shiny_template("argonDash") )

# multiple models
m3 <- ga_model_example("time-normalised.gamr")
m4 <- ga_model_example("ga-effect.gamr")

# launch in gentelella template
ga_model_shiny(list(m4, m3), auth_dropdown = "universal",
              template = ga_model_shiny_template("gentelella"))

     
# you can make custom ui embedded within the template file
# use {{{ model_list }}} to work with the models in the ui.R

# below adds custom macro 'theme' and a custom ui in box tabs
ga_model_shiny(list(m4, m3), auth_dropdown = "universal", 
               template = ga_model_shiny_template("shinythemes"), 
               theme = "yeti")

# shinydashboard's custom ui functions put a model in each side tab      
ga_model_shiny(list(m4, m3), auth_dropdown = "universal", 
               template = ga_model_shiny_template("shinydashboard"), 
               skin = "green")
               
# send in lots of theme variables to bslib in shiny > 1.6.0
ga_model_shiny(list(m4, m3), auth_dropdown = "universal",
               template = ga_model_shiny_template("basic_bslib"), 
               bg = "white", fg = "red", primary = "grey")
 
# write out an app to a local folder
ga_model_shiny(list(m4, m3), auth_dropdown = "universal",
               template = ga_model_shiny_template("basic_bslib"), 
               bg = "white", fg = "red", primary = "grey",
               local_folder = "deploy_shiny")
}