简体   繁体   English

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

[英]passing ellipsis arguments to map function purrr package, R

I want to use ellipsis parameters inside map function of purrr package.我想在 purrr 包的 map 函数中使用省略号参数。 this is a toy example:这是一个玩具示例:

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

now I want to use f1 inside another function f2:现在我想在另一个函数 f2 中使用 f1:

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

where ... is intended mainly to manipulate SW and a or b parameters in f1 function.其中 ... 主要用于操作 f1 函数中的 SW 和 a 或 b 参数。 however f2 is not doing what I want as can be seen here但是 f2 没有做我想要的,可以在这里看到

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

I appreciate any guide in how to use addecuatelly ... inside map我感谢任何有关如何使用 addecuatelly ... 内地图的指南

You just need to pass the ellipses through the map_df() call as well.您只需要通过map_df()调用传递省略号。 Otherwise they can't get into the inner f1() call.否则他们无法进入内部f1()调用。

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

You can also capture the ellipses early on in your second function, and use do.call to add them to your first function later on.您还可以在第二个函数的早期捕获省略号,然后使用do.call将它们添加到您的第一个函数中。 This makes it more explicit where and how they are used.这使得它们的使用地点和使用方式更加明确。

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

MrFlick solution did not work for me: I think indeed you also need to pass the ... to the anonymous function, which then requires using function(x,...) instead of ~ (as suggested by @dmi3kno). MrFlick 解决方案对我不起作用:我认为您确实还需要将...传递给匿名函数,然后需要使用function(x,...)而不是~ (如@dmi3kno 所建议的那样)。

That means you need the quite surprising triple ... call:这意味着您需要非常令人惊讶的三重...调用:

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

Example:示例:

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

Created on 2020-11-16 by the reprex package (v0.3.0)reprex 包(v0.3.0) 于 2020 年 11 月 16 日创建

For this issue, I've found that rlang::exec() allows you to pass ... to purrr::map() when combined with an anonymous function, like this:对于这个问题,我发现rlang::exec()允许您在与匿名函数结合时将 ... 传递给purrr::map() ,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM