Join GitHub today
GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together.
Sign upganalytics DSL suggestions #2
Comments
|
I like this idea very much Hadley and would like to aim for this. |
|
Hi @hadley, I've implemented your first suggestion above, but needed to add the methods to Looking at the second part of your suggestion I can see how much easier this would make writing expressions. Mapping between the Google Analytics expression operators (comparators) and the equivalent R functions is my first step. Going along the lines of your examples above, I've come up with the following mapping table but would very much appreciate any further suggestions you can offer:
Any suggestions for the equivalent R functions for those where I have put a question mark? If it helps, details about the above Google Analytics filter operators and segment operators can be found in the Google Analytics Core Reporting API developer reference. |
|
For all of the infix operators that have no equivalent in base R, doing (e.g.) |
|
Just to keep everything in one place, capturing the suggestion by @hadley in June 2013: "...use the approach of https://github.com/hadley/dplyr/blob/master/R/translate-sql.r … to automatically convert R expressions into GA expressions." Further to that, I note the use of lazyeval by the rex package - this approach also looks helpful in implementing this suggested enhancement. |
|
First attempt looks promising: AutoExpr <- function(...) {
expr <- lazy(...)$expr
comparator <- as.character(expr[[1]])
var <- as.character(expr[[2]])
operand <- expr[[3]]
Expr(var, comparator, eval(operand))
}
AutoExpr(pageviews > 1)
#> An object of class "gaMetExpr"
#> Slot "var":
#> An object of class "gaMetVar"
#> [1] "ga:pageviews"
#>
#> Slot "comparator":
#> An object of class "gaMetComparator"
#> [1] ">"
#>
#> Slot "operand":
#> An object of class "gaMetOperand"
#> [1] 1 |
|
I have committed a solution to the dev branch: https://github.com/jdeboer/ganalytics/blob/dev/R/condition.R#L9 ... Any feedback would be welcome. I think there are opportunities to expand on this to go further towards the example expression Hadley provided at the start of this discussion. Here is a an example to demonstrate use of this new function: condition(pageviews > 1) &
(
condition(keyword %matches% "buy") |
condition(city %in% c("Sydney", "Melbourne")
)
) |
Taking an example from http://www.slideshare.net/johanndeboer/web-analytics-with-r-melb-urn:
it would be relatively straightforward to allow you to write
just by adding the appropriate methods for
&&and||.But I think it would be relatively straightforward to go even further and be able to write
using the principles described in http://adv-r.had.co.nz/dsl.html.