I currently have this function, which takes a table and two lists of expressions, and evaluates them, turning them into two matrices. I use two lists instead of ...
because I need to be able to determine whether the expressions are going to fall in Y or Z.
func = function(tb, Y, Z) {
Y_matrix = enquo(Y) %>%
eval_tidy(tb) %>%
as.data.frame %>%
as.matrix
Z_matrix = enquo(Z) %>%
eval_tidy(tb) %>%
as.data.frame %>%
as.matrix
list(Y_matrix, Z_matrix)
}
For instance:
> tb = data.frame(a = 1:3, b = 3:1, c = 2*(1:3))
> func(tb, list(a + b, a - b), list(a*c, a + b + c))
[[1]]
c.4L..4L..4L. c..2L..0L..2L.
[1,] 4 -2
[2,] 4 0
[3,] 4 2
[[2]]
c.2..8..18. c.6..8..10.
[1,] 2 6
[2,] 8 8
[3,] 18 10
However, I also want to capture the expressions inside the lists as text, so I could use them to name the columns. For instance, my desired output would be something like:
> func(tb, list(a + b, a - b), list(a*c, a + b + c))
[[1]]
a+b a-b
[1,] 4 -2
[2,] 4 0
[3,] 4 2
[[2]]
a*c a + b + c
[1,] 2 6
[2,] 8 8
[3,] 18 10
How can I do this?