Alexey Shevchuk
Alexey Shevchuk

Reputation: 1

Export csv in R

Try to export my data.frame to csv

google_analytics(214199202,
                     date_range = c("2020-05-27", "2020-06-11"),
                     metrics = c("ga:transactions","ga:transactionRevenue"),
                     dimensions = c("ga:date","ga:campaign","ga:sourceMedium"),
                     filtersExpression = "ga:sourceMedium=~(yandex / cpc|google / cpc)",
                     write.csv(google_analytics, file = "filename.csv")
                     )

But after running it, I receive this error:

Ошибка в as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘"function"’ to a data.frame

I don't understand where the problem is

Upvotes: 0

Views: 461

Answers (1)

Basil Eric Rabi
Basil Eric Rabi

Reputation: 343

I don't use googleAnalyticsR but from what I can see in the manual, google_analytics() returns a data.frame and the 6th argument is met_filters. You put the function write.csv() in the 6th argument as met_filters thus resulting in error. You should save the output of google_analstics first then export to csv.


library(googleAnalyticsR)

# Fetch data and save data.frame to "yourData"
yourData <- google_analytics(
  214199202,
  date_range = c("2020-05-27", "2020-06-11"),
  metrics = c("ga:transactions","ga:transactionRevenue"),
  dimensions = c("ga:date","ga:campaign","ga:sourceMedium"),
  filtersExpression = "ga:sourceMedium=~(yandex / cpc|google / cpc)"
)

# Export "yourData" as csv
write.csv(yourData, file = "filename.csv")

Upvotes: 1

Related Questions