Mario M.
Mario M.

Reputation: 872

Shiny + Google Analytics: my output table doesn't appear

I try to pull data from Google Analytics with API-R. There are my two files for running my shiny app here:

ui.R

 shinyUI(pageWithSidebar(

      headerPanel("My Shiny APP"),

      sidebarPanel( 
        dateRangeInput("dateRange",
                       label = "Select date range:",
                       start = Sys.Date() - 7, end = Sys.Date()-6)),

      mainPanel(

        fluidPage(
          fluidRow(
            column(12,
                   dataTableOutput("table")
            )
          )
        ))))

server.R

ga_token <- authorize(client.id = "XXXXXXXXX.apps.googleusercontent.com", 
                      client.secret = "XXXXXXXXXXX",
                      cache = "token")

shinyServer(function(input, output){


  getDataFromGA <- reactive({

    ga.data <- get_ga(profileId = "ga:xxxxxxx", 
                              start.date =input$dateRange[1], end.date = input$dateRange[2], 
                              metrics = c("ga:sessions","ga:bounceRate"), dimensions = "ga:userType", 
                              samplingLevel = "HIGHER_PRECISION", start.index = NULL,
                              max.results = 10000, include.empty.rows = NULL, fetch.by = NULL, ga_token)

    return(ga.data)


  })

  output$table = renderDataTable({
    ga.data <- getDataFromGA()
    if (is.null(ga.data)) return(NULL)
    })
}) 

If I put a reactive expression at output$table, I have the same problem (the output table doesn't appear, and R doesn't print me any error message). Libraries I load: devtools, RGA, shiny.

Upvotes: 0

Views: 129

Answers (1)

rangeelo
rangeelo

Reputation: 118

Instead of simply using reactive, you can try reactiveValues and observeEvent. Your code may look something like:

    values <- reactiveValues(start.date = NULL, end.date = NULL, ga.data = NULL)

    observeEvent(input$dateRange, {
    values$start.date <- input$dateRange[1]
    values$end.date <- input$dateRange[2]
    values$ga.data <- get_ga(...) })

You can access the google analytics object as: values$ga.data

Upvotes: 1

Related Questions