0

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?

Luiz Rodrigo
  • 936
  • 1
  • 7
  • 19

2 Answers2

3

You can use something like deparse to go over the language elements in your list to turn those into strings. For example

func = function(tb, Y, Z) {
  Y_names <- map_chr(as.list(enexpr(Y))[-1], deparse)
  Y_matrix = enquo(Y) %>% 
    eval_tidy(tb) %>%
    as.data.frame %>%
    set_names(Y_names) %>% 
    as.matrix

  Z_names <- map_chr(as.list(enexpr(Z))[-1], deparse)
  Z_matrix = enquo(Z) %>%
    eval_tidy(tb) %>%
    as.data.frame %>%
    set_names(Z_names) %>% 
    as.matrix

  list(Y_matrix, Z_matrix)
}

which works with this example

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
MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • Wow! Thanks! Wasn't really aware that `enexpr()` objects had an easily accessible tree structure. Now I feel like doing more advanced stuff. Thanks! – Luiz Rodrigo May 11 '18 at 02:37
2

We could pass the arguments as a quosure and then do the evaluation (!!!)

library(tidyverse)
f1 <- function(tb, Y, Z) {
       Y_matrix <-  tb %>% 
             transmute(!!! Y)

       Z_matrix <- tb %>%
             transmute(!!! Z)

       list(Y_matrix, Z_matrix)


   }

f1(tb, quos(a + b, a - b), quos(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
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Nice workaround! I'm not choosing it as my favourite answer only because it has the inconvenience of forcing the user of the function to have rlang loaded. – Luiz Rodrigo May 11 '18 at 02:23