2016-01-19 40 views
0

我期待通過Shiny應用中的多個Google Analytics Core Reporting API查詢來使用mapply查詢,並使用由數據幀的每一行指定的3個輸入變量。要通過R訪問Google Analytics API,我需要使用CRAN RGA庫。在Shiny應用中對多個Google AnalyticsAPI查詢進行映射

在我ui.R,我有textOutputdf$name映射到每個i。這樣,output$name應在df$name中呈現每個i的兩個查詢的結果。

目前,我的控制檯打印:

「未獲得結果。」

我已經單獨測試了這些查詢,並且它們都應該提取結果。

這是我server.R的簡化版本:

shinyServer(function(input, output) { 
    library(RGA) 
    thisWeekEnd<-"2016-01-17" 
    thisWeekStart<-as.character(as.Date(thisWeekEnd)-7) 
    lastWeekEnd<-as.character(as.Date(thisWeekEnd)-8) 
    lastWeekStart<-as.character(as.Date(thisWeekEnd)-13) 
    startOfCampaign<-"2015-12-23" 

    df <- data.frame(
    start= c(thisWeekStart,lastWeekStart,startOfCampaign), 
    end =c(thisWeekEnd,lastWeekEnd,thisWeekEnd), 
    name=c("thisWeek","lastWeek","total"), stringsAsFactors=FALSE) 

    gaData<-function(start,end,name){ 

    query1 <- get_ga(
     id, 
     start.date = start, 
     end.date = end, 
     metrics = "ga:sessions", 
    ) 

    query2 <- get_ga(
     id, 
     start.date = start, 
     end.date = end, 
     metrics = "ga:uniqueEvents", 
    ) 

    output$name<-renderText({ 
     paste(as.character(query1$sessions)," sessions", 
     as.character(query2$unique.events)," unique events" 
     )}) 
    } 

    mapply(gaData, df$start,df$end,df$name) 
} 
+0

此代碼的工作對我來說:https://gist.github.com/artemklevtsov/c510c0a7737bf7215099 –

+0

該代碼通過R工作,但在一個閃亮的應用程序中,沒有結果被拉。我認爲我的問題是使用輸出$ name <-renderText({})封裝粘貼函數,以便將輸出路由到UI中的每個df $名稱的動態元素。 –

回答

0

如果我正確的理解這個問題:

library(shiny) 
library(RGA) 
authorize() 
id <- "ga:100202885" 
getDates <- function() { 
    today <- Sys.Date() 
    thisWeekStart <- as.Date(cut(today, "weeks")) 
    thisWeekEnd <- as.Date(cut(today, "weeks")) + 6 
    lastWeekStart <- thisWeekStart - 7 
    lastWeekEnd <- thisWeekEnd - 7 
    startOfCampaign <- as.Date("2015-12-23") 
    data.frame(
     start = c(thisWeekStart, lastWeekStart, startOfCampaign), 
     end = c(thisWeekEnd, lastWeekEnd, thisWeekEnd), 
     row.names = c("thisWeek", "lastWeek","total"), 
     stringsAsFactors = FALSE) 
} 
getData <- function(start, end, ...) { 
    get_ga(..., start.date = start, end.date = end, "ga:sessions,ga:uniqueEvents") 

} 

ui <- shinyUI(fluidPage(
    title = "Test RGA input", 
    tableOutput("data") 
)) 

server <- shinyServer(function(input, output) { 
    GAData <- NULL 
    dates <- getDates() 
    GAData <- cbind(df, t(mapply(getData, dates$start, dates$end, profileId = id))) 
    output$data <- renderTable({ 
     validate(need(!is.null(GAData), "Loading data...")) 
     GAData 
    }) 
}) 

shinyApp(ui, server) 
相關問題