inst/extdata/SO_question.R

library(dplyr)
library(rlang)

as_myclass <- function(d, val.col) {
  attr(d, "value.col") <- enquo(val.col)
  class(d) <- c("myclass", "data.frame")
  d
}

my_mpg <- as_myclass(mtcars, cyl)
str(my_mpg)

my_summarise <- function(d) {
  value.col <- attr(d, "value.col")
  summ <- summarise(d, mean = mean(!!value.col))
  summ
}

my_summarise(my_mpg)

my_wrapper <- function(d) {
  my_summarise(d)
}

my_wrapper(my_mpg)






my_summarise_2 <- function(d, st) {
  summ <- select_(.data = d, .dots=st)
  #summ <- mutate_(.data=d, .dots=mean(st))
  summ
}

my_summarise_2(mtcars, "mpg")





my_obj <- as_myclass(d, val.col=my.value)
str(attr(my_obj, "value.col"))

my_mean <- function(d) {
  column <- attr(d, "value.col")
  browser()
  d_summ <- summarise(col, mean.value = mean(!!column))
  d_summ
}

my_mean(my_obj)

# Now I'd like to create a new function, which will read the grade attribute and do something with it

mean_grades <- function(d) {
  the.grade.col <- !!attr(d, "grade.col")
  browser()
  avg.grade <- summarise_(d, mean.grade = mean(!!the.grade.col))
}
mean_grades(g)


x <- seq(0, 2 * pi, length = 100)
sinx <- sin(x)
plot(x, sinx, type = "l")
adsteen/gradr documentation built on Dec. 18, 2017, 4:45 a.m.