Forked from DarwinAwardWinner/dplyr-DataFrame-methods.R
Last active
February 26, 2019 13:43
-
-
Save mjsteinbaugh/952e2263650262da1a93a560b0a98ccc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# I'm no longer using this approach. | |
# | |
# Instead, I encourage you to check out my transformer package. | |
# https://steinbaugh.com/transformer/ | |
# https://github.com/steinbaugh/transformer/ | |
# | |
# - M | |
# This will allow you to use dplyr functions with Bioconductor's | |
# S4Vectors::DataFrame class. They simply call "as.data.frame" on DataFrame | |
# objects and then pass them to the methods for "data.frame". Note that this | |
# entails conversion of S4 vector columns to primitive vectors. No attempt is | |
# made to convert the results back to DataFrame, since such a conversion would | |
# not restore S4 vector columns that were converted to primitive vectors. | |
library(S4Vectors) | |
library(dplyr) | |
single.table.verbs <- c( | |
"filter", "select", "mutate", "transmute", "group_by", | |
"summarise", "do", "arrange", "rename", "distinct" | |
) | |
for (verb in single.table.verbs) { | |
generic.name <- str_c(verb, "_") | |
method.name <- str_c(generic.name, ".DataFrame") | |
assign(method.name, local({ | |
generic <- get(generic.name) | |
function(.data, ...) { | |
.data %>% as.data.frame %>% generic(...) | |
} | |
})) | |
} | |
two.table.verbs <- c( | |
"inner_join", "left_join", "right_join", "full_join", "semi_join", | |
"anti_join", "intersect", "union", "setdiff" | |
) | |
for (verb in two.table.verbs) { | |
generic.name <- verb | |
method.name <- str_c(generic.name, ".DataFrame") | |
assign(method.name, local({ | |
generic <- get(generic.name) | |
function(x, y, ...) { | |
generic(as.data.frame(x), as.data.frame(y), ...) | |
} | |
})) | |
## Set up S4 method to dispatch to dplyr function | |
local({ | |
generic <- get(generic.name, envir=as.environment("package:dplyr")) | |
setMethod(verb, c(x="DataFrame", y="ANY"), | |
function(x, y, ...) { | |
generic(x=x, y=y) | |
}) | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment