将省略号参数传递给映射函数 purrr 包,R

passing ellipsis arguments to map function purrr package, R

我想在 purrr 包的 map 函数中使用省略号参数。这是一个玩具示例:

f1<-function(x,a=NA,b=NA,prs=seq(0, 1, 0.25),SW=T){
  if(SW){
    res<-data.frame(name1=a,name2=b,t(quantile(x, prs, na.rm = T)),  mean=mean(x, na.rm = T), sd=sd(x, na.rm = T),
                    NAs=length(x[is.na(x)]),n=length(x[!is.na(x)]),SWp=shapiro.test(x)$p.value,stringsAsFactors =F)
  }else
  {
    res<-data.frame(name1=a,name2=b,t(quantile(x, prs, na.rm = T)),  mean=mean(x, na.rm = T), sd=sd(x, na.rm = T),
                    NAs=length(x[is.na(x)]),n=length(x[!is.na(x)]),stringsAsFactors =F)
  }
return(res)
}

f1(c(NA,rnorm(25),NA),SW=F)
f1(c(NA,rnorm(25),NA))

现在我想在另一个函数 f2 中使用 f1:

f2<-function(df,...){
  res<-map_df(colnames(df),~f1(df[,.],a=.,...))
  return(res)
}

其中...主要用于操作 f1 函数中的 SW 和 a 或 b 参数。然而,f2 并没有按照我的意愿行事,如下所示

f2(iris[,-5])
f2(iris[,-5],SW=F)

我很感激任何关于如何在地图中使用 addecuatelly ... 的指南

您只需要通过 map_df() 调用也传递省略号。否则他们无法进入内部 f1() 调用。

f2 <- function(df, ...){
  res <- map_df(colnames(df), ~f1(df[,.], a=., ...), ...)
  return(res)
}

您还可以在第二个函数的早期捕获省略号,然后使用 do.call 将它们添加到您的第一个函数中。这使得它们的使用位置和方式更加明确。

f2 <- function(df, ...){
  params <- list(...)
  res <- map_df(colnames(df), ~ do.call(
    f1, c(list(x = df[,.], a=.), params)))
  return(res)
}

MrFlick 解决方案对我不起作用:我认为您确实还需要将 ... 传递给匿名函数,然后需要使用 function(x,...) 而不是 ~(如@dmi3kno 建议。

这意味着您需要非常令人惊讶的三重 ... 调用:

map(x, function(x, ...) mean(x, trim=0, ...), ...)

示例:

library(purrr)
x <- list(c(1,2), c(1,2,NA))
fo1 <- function(...) map(x, ~mean(., trim=0, ...), ...)
fo2 <- function(...) map(x, function(x, ...) mean(x, trim=0, ...), ...)

fo1()
#> Warning in if (na.rm) x <- x[!is.na(x)]: the condition has length > 1 and only
#> the first element will be used

#> Warning in if (na.rm) x <- x[!is.na(x)]: the condition has length > 1 and only
#> the first element will be used
#> [[1]]
#> [1] 1.5
#> 
#> [[2]]
#> [1] 1.5

fo2()
#> [[1]]
#> [1] 1.5
#> 
#> [[2]]
#> [1] NA
fo2(na.rm=TRUE)
#> [[1]]
#> [1] 1.5
#> 
#> [[2]]
#> [1] 1.5

reprex package (v0.3.0)

于 2020-11-16 创建

对于这个问题,我发现 rlang::exec() 允许您在与匿名函数结合使用时将 ... 传递给 purrr::map(),如下所示:

f2 <- function(df, ...){
  res <- map(colnames(df), function(x) rlang::exec("f1", df[,x], ...))
  return(res)
}