forked from dimagi/dimagi-data-platform-R
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths_dplyr.R
49 lines (41 loc) · 1.34 KB
/
s_dplyr.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# Helper functions that allow string arguments for dplyr's data modification functions like arrange, select etc.
# Author: Sebastian Kranz
# Examples are below
#' Modified version of dplyr's filter that uses string arguments
#' @export
s_filter = function(.data, ...) {
eval.string.dplyr(.data,"filter", ...)
}
#' Modified version of dplyr's select that uses string arguments
#' @export
s_select = function(.data, ...) {
eval.string.dplyr(.data,"select", ...)
}
#' Modified version of dplyr's arrange that uses string arguments
#' @export
s_arrange = function(.data, ...) {
eval.string.dplyr(.data,"arrange", ...)
}
#' Modified version of dplyr's arrange that uses string arguments
#' @export
s_mutate = function(.data, ...) {
eval.string.dplyr(.data,"mutate", ...)
}
#' Modified version of dplyr's summarise that uses string arguments
#' @export
s_summarise = function(.data, ...) {
eval.string.dplyr(.data,"summarise", ...)
}
#' Modified version of dplyr's group_by that uses string arguments
#' @export
s_group_by = function(.data, ...) {
eval.string.dplyr(.data,"group_by", ...)
}
#' Internal function used by s_filter, s_select etc.
eval.string.dplyr = function(.data, .fun.name, ...) {
args = list(...)
args = unlist(args)
code = paste0(.fun.name,"(.data,", paste0(args, collapse=","), ")")
df = eval(parse(text=code,srcfile=NULL))
df
}