【问题标题】:Shiny + Google Analytics: my output table doesn't appearShiny + Google Analytics:我的输出表没有出现
【发布时间】:2016-09-27 12:02:07
【问题描述】:

我尝试使用 API-R 从 Google Analytics 中提取数据。这里有两个文件用于运行我的闪亮应用程序:

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

如果我在 output$table 中放置一个反应式表达式,我会遇到同样的问题(输出表不出现,并且 R 不会打印任何错误消息)。 我加载的库:devtools、RGA、shiny。

【问题讨论】:

    标签: r windows google-analytics shiny


    【解决方案1】:

    您可以尝试使用 reactiveValues 和 observeEvent,而不是简单地使用响应式。 您的代码可能类似于:

        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(...) })
    

    您可以通过以下方式访问 google 分析对象:values$ga.data

    【讨论】:

    • 我试试这个,我发现这个消息错误:Access token will be stored in the 'token' file. Warning: Error in : is.atomic(x) is not TRUE Stack trace (innermost first): 70: stopifnot 69: FUN 68: vapply 67: compact 66: fix_query 65: build_query 64: get_ga 63: observeEventHandler [C:\Users......] 1: runApp ERROR: [on_request_read] connection reset by peer如何解决这个问题? is.atomic()?我不明白为什么。谢谢 Mohit Sainani
    • 尝试在 shinyServer 函数中包含你的 ga_token 对象
    • 您的问题出在 renderDataTable 中。您应该在响应式语句中移动“if”子句,根据您的逻辑使其返回 ga.data 或 NULL,并且仅按原样呈现“getDataFromGA()”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    相关资源
    最近更新 更多