Skip to content

Instantly share code, notes, and snippets.

@chan-ume
Last active August 5, 2017 14:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chan-ume/6e259fe7f1de092ceb9982ee00065984 to your computer and use it in GitHub Desktop.
Save chan-ume/6e259fe7f1de092ceb9982ee00065984 to your computer and use it in GitHub Desktop.
library(shiny)
library(googleAuthR)
library(googleAnalyticsR)
library(listviewer)
library(ggplot2)
options(shiny.port = 1221)
options(googleAuthR.webapp.client_id = "先程作成したクライアントIDをコピペ")
options(googleAuthR.webapp.client_secret = "先程作成したクライアントシークレットをコピペ")
options(googleAuthR.scopes.selected = c("https://www.googleapis.com/auth/analytics.readonly"))
shinyServer(function(input, output, session){
#####--------- Setup
token <- callModule(googleAuth, "Google_login")
ga_accounts <- reactive({
validate(
need(token(), "Authenticate")
)
with_shiny(google_analytics_account_list, shiny_access_token = token())
})
selected_id <- callModule(authDropdown, "viewId_select", ga.table = ga_accounts)
 #####--------- Multi-date
md_metrics <- callModule(multi_select, "metric_md", type = "METRIC", subType = "all")
md_dims <- callModule(multi_select, "dim_md", type = "DIMENSION", subType = "all")
md_data <- eventReactive(input$get_md, {
viewId <- selected_id()
metrics <- md_metrics()
dims <- md_dims()
dates1 <- input$date1_md
dates2 <- input$date2_md
with_shiny(google_analytics_4,
shiny_access_token = token(),
viewId = viewId,
date_range = c(dates1[1], dates1[2], dates2[1], dates2[2]),
metrics = metrics,
dimensions = dims)
})
output$md_table <- renderDataTable({
md_data()
})
#####--------- Calculated Metrics
calc_dim <- callModule(multi_select, "dimensions_calc", type = "DIMENSION", subType = "all")
calc_met <- callModule(multi_select, "metrics_calc", type = "METRIC", subType = "all")
calc_data <- eventReactive(input$get_calc, {
viewId <- selected_id()
dims <- calc_dim()
dates <- input$date_clac
metric_name <- gsub(" ", "", input$calculated_name)
metric_exp <- input$calculated_exp
normal_metrics <- calc_met()
exp_metrics <- setNames(metric_exp, metric_name)
metrics <- c(exp_metrics, normal_metrics)
with_shiny(google_analytics_4,
shiny_access_token = token(),
viewId = viewId,
date_range = c(dates[1], dates[2]),
metrics = metrics,
dimensions = dims)
})
output$calc_table <- renderDataTable({
calc_data()
})
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment