r_mvl
r_mvl

Reputation: 109

Quosure with in a nested function

I am struggling to write a function fun2 that uses fun1... and keep getting errors. I have written a simplified example below. It is the first time I deal with "tidy evaluation" and not sure to understand the in and outs of it.

Example dataframes:

d1 = data.frame(
  ID = c("A", "A", "A", "B", "B", "C", "C", "C", "C"),
  EXPR = c(2, 8, 3, 5, 7, 20, 1, 5, 4)
)

d2 = data.frame(
  ID = c("A", "B", "C"),
  NUM = c(22, 50, 31)
)

First function

fun1 <- function(
  df1 = "df 1", 
  df2 = "df 2",
  t1 = "threshold 1",
  expr_col = "expr column", 
  id_col = "sample column - must be present in df1 and df2") {

  # dataframes
  df <- df1
  db <- df2

  # quosure
  enquo_id <- enquo(id_col)
  enquo_expr <- enquo(expr_col)

  # classify
  df <- df %>% 
    mutate(threshold = t1) %>% 
    mutate(class = ifelse(!!enquo_expr > t1, "positive", "negative")) %>% 
    mutate(class = factor(class, levels = c("positive", "negative")))

  # calculate sample data
  df.sum <- df %>% 
    group_by(!!enquo_id, class) %>% 
    summarise(count = n()) %>% 
    complete(class, fill = list(count = 0)) %>% 
    mutate(total = sum(count), freq = count/total)

  # merge dataframes
  df.sum <- left_join(df.sum, db, by = quo_name(enquo_id))

  # return
  return(df.sum)
}

If I run a test of this, I get a dataframe in return, as expected

test <- fun1(df1 = d1, df2 = d2, t1 = 3, expr_col = EXPR, id_col = ID)

Second funtion Now with fun2, I am trying to use fun1 in a for loop to iterate from ti to tf of the seq vector:

fun2 <- function(
  df1 = "df 1", 
  df2 = "df 2",
  expr_col = "expr column", 
  id_col = "sample column - must be present in df1 and df2",
  ti = "initial value",
  tf = "final value",
  res = "resolution") {

  # define variables for fun1
  var1 <- enquo(d1)
  var2 <- enquo(d2)
  var3 <- enquo(t1)
  var4 <- enquo(EXPR)
  var5 <- enquo(ID)

  # get sequence of values
  seq <- seq(from = ti, to = tf, by = res)

  # open list
  t.list <- list()

  # Loop ----
  for (i in seq_along(seq)){
    t1 <- seq[i]
    t.list[[i]] <- fun1(df1 = var1,
                        df2 = var2,
                        t1 = var3,
                        expr_col = var4,
                        id_col = var5)
  }
  df.out <- plyr::ldply(t.list, rbind)

  ### Return ---
  return(df.out)
}

But if I run this

test <- fun2(df1 = d1, df2 = d2, expr_col = EXPR, id_col = ID, ti = 1, tf = 10, res = 1)

I get an error message

Error in (function (x)  : object 'EXPR' not found

I tried various things... and I am kind of stuck here. I guess I am not using enquo() properly. I can get it to work by not using varX and putting directly the actual appropriate name of each element in the fun1 arguments, but the whole point of doing this, to me, is to make it "generalisable" and therefore specify the arguments only in fun2 which will then be passed to fun1.

Any help would be greatly appreciated.

Upvotes: 1

Views: 438

Answers (1)

r_mvl
r_mvl

Reputation: 109

Many thanks for your answer aosmith. I am now sorted using the following code:

fun2 <- function(
  df1 = "df 1", 
  df2 = "df 2",
  expr_col = "expr column", 
  id_col = "sample column - must be present in df1 and df2",
  ti = "initial value",
  tf = "final value",
  res = "resolution") {

  # define variables for fun1
  var4 <- enquo(expr_col)
  var5 <- enquo(id_col)

  # get sequence of values
  seq <- seq(from = ti, to = tf, by = res)

  # open list
  t.list <- list()

  ### Loop --------------------------------------------------------------
  for (i in seq_along(seq)){
    t1 <- seq[i]
    t.list[[i]] <- fun1(df1 = df1,
                        df2 = df2,
                        t1 = t1,
                        expr_col = !!var4,
                        id_col = !!var5)
  }
  df.out <- plyr::ldply(t.list, rbind)

  ### Return ---
  return(df.out)
}

# TEST FUN2
test <- fun2(df1 = d1, df2 = d2, expr_col = EXPR, id_col = ID, ti = 1, tf = 10, res = 1)

Upvotes: 1

Related Questions