Learn R Programming

googleAnalyticsR (version 1.2.0)

ga_model_shiny: Create a Shiny app from a ga_model file

Description

Create a Shiny app from a ga_model file

Usage

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

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

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.

See Also

Other GA modelling functions: ga_model(), ga_model_edit(), ga_model_example(), ga_model_load(), ga_model_make(), ga_model_save(), ga_model_shiny_load(), ga_model_shiny_template(), ga_model_write()

Examples

Run this code

# see Shiny templates included with the package
ga_model_shiny_template("list")

# see an example of an ui.R template with macros
ga_model_shiny_template("basic/ui.R", read_lines = TRUE)

# see an example of an app.R template with macros
ga_model_shiny_template("basic_app/app.R", read_lines = TRUE)

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")
}



Run the code above in your browser using DataLab