1

我正在努力编写一个使用fun1的函数fun2 ...并不断出错。我在下面写了一个简化的例子。这是我第一次处理“整洁的评估”,不确定它的来龙去脉。

示例数据框:

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)
)

第一个功能

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)
}

如果我对此进行测试,我会得到一个数据框作为回报,正如预期的那样

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

第二个功能 现在有了fun2,我试图在 for 循环中使用 fun1 来从 ti 迭代到 seq 向量的 tf :

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)
}

但是如果我运行这个

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

我收到一条错误消息

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

我尝试了各种事情......我有点卡在这里。我想我没有正确使用enquo()。我可以通过不使用varX并将每个元素的实际适当名称直接放入fun1参数中来使其工作,但对我而言,这样做的全部意义在于使其“通用”,因此仅指定参数fun2然后将传递给fun1

任何帮助将不胜感激。

4

1 回答 1

1

非常感谢您的回答 aosmith。我现在使用以下代码进行排序:

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)
于 2018-11-02T20:47:20.017 回答