簡體   English   中英

match.fun為函數內定義的函數提供錯誤

[英]match.fun provide error with functions defined inside functions

嘗試將match.fun應用於其他函數中的函數時,我收到錯誤。

x <- matrix(rnorm(10*100), nrow=100) # data sample
descStats <- function(x, stats = c("n", "min", "max", "srange", "mean", "median", "sd")) {
  n <- function(x, ...) sum(!is.na(x), ...)
  srange <- function(x, ...) max(x, ...) - min(x, ...)
  fun <- function(x) {
    result <- vapply(stats, function(z) match.fun(z)(x, na.rm=TRUE), FUN.VALUE=numeric(1))
  }
  if (is.vector(x)) {
    result <- fun(x)
  }
  if (is.matrix(x) || is.data.frame(x)) {
    result <- t(apply(x, 2, fun))
  }
  return(result)
}
descStats(x)
## Error in get(as.character(FUN), mode = "function", envir = envir) : 
##   object 'n' of mode 'function' was not found

如果我定義nsrange descStats之外的功能,它工作正常。

n <- function(x, ...) sum(!is.na(x), ...)
srange <- function(x, ...) max(x, ...) - min(x, ...)
descStats2 <- function(x, stats = c("n", "min", "max", "srange", "mean", "median", "sd")) {
  fun <- function(x) {
    result <- vapply(stats, function(z) match.fun(z)(x, na.rm=TRUE), FUN.VALUE=numeric(1))
  }
  if (is.vector(x)) {
    result <- fun(x)
  }
  if (is.matrix(x) || is.data.frame(x)) {
    result <- t(apply(x, 2, fun))
  }
  return(result)
}
descStats2(x)
##         n       min      max   srange        mean      median        sd
##  [1,] 100 -2.303839 2.629366 4.933205  0.03711611  0.14566523 1.0367947
##  [2,] 100 -1.968923 2.169382 4.138305 -0.03917503  0.02239458 0.9048509
##  [3,] 100 -2.365891 2.424077 4.789968 -0.08012138 -0.23515910 1.0438133
##  [4,] 100 -2.740045 2.127787 4.867832  0.03978241  0.15363449 0.9778891
##  [5,] 100 -1.598295 2.603525 4.201820  0.23796616  0.16376239 1.0428915
##  [6,] 100 -1.550385 1.684155 3.234540 -0.11114479 -0.09264598 0.8260126
##  [7,] 100 -2.438641 3.268796 5.707438  0.02948100 -0.05594740 1.0481331
##  [8,] 100 -1.716407 2.795340 4.511747  0.22463606  0.16296613 0.9555129
##  [9,] 100 -2.359165 1.975993 4.335158 -0.33321888 -0.17580933 0.9784788
## [10,] 100 -2.139267 2.838986 4.978253  0.15540182  0.07803265 1.0149671

另一種方式是使用eval(call(FUN, args)) 例如。

descStats3 <- function(x, stats = c("n", "min", "max", "srange", "mean", "median", "sd")) {
  n <- function(x, ...) sum(!is.na(x), ...)
  srange <- function(x, ...) max(x, ...) - min(x, ...)
  fun <- function(x) {
    result <- vapply(stats, function(z) eval(call(z, x, na.rm=TRUE)), FUN.VALUE=numeric(1))
  }
  if (is.vector(x)) {
    result <- fun(x)
  }
  if (is.matrix(x) || is.data.frame(x)) {
    result <- t(apply(x, 2, fun))
  }
  return(result)
}
descStats3(x)
##         n       min      max   srange        mean      median        sd
##  [1,] 100 -2.303839 2.629366 4.933205  0.03711611  0.14566523 1.0367947
##  [2,] 100 -1.968923 2.169382 4.138305 -0.03917503  0.02239458 0.9048509
##  [3,] 100 -2.365891 2.424077 4.789968 -0.08012138 -0.23515910 1.0438133
##  [4,] 100 -2.740045 2.127787 4.867832  0.03978241  0.15363449 0.9778891
##  [5,] 100 -1.598295 2.603525 4.201820  0.23796616  0.16376239 1.0428915
##  [6,] 100 -1.550385 1.684155 3.234540 -0.11114479 -0.09264598 0.8260126
##  [7,] 100 -2.438641 3.268796 5.707438  0.02948100 -0.05594740 1.0481331
##  [8,] 100 -1.716407 2.795340 4.511747  0.22463606  0.16296613 0.9555129
##  [9,] 100 -2.359165 1.975993 4.335158 -0.33321888 -0.17580933 0.9784788
## [10,] 100 -2.139267 2.838986 4.978253  0.15540182  0.07803265 1.0149671
identical(descStats2(x), descStats3(x))
## [1] TRUE

為什么descStats不起作用?

這是一個范圍問題。 查看match.fun的代碼,你會得到答案。

match.fun scope是envir <- parent.frame(2)

get范圍在envir = as.environment(-1) = parent.frame(1)

我想我們不能把環境作為一個論點。 一種解決方案是使用@nograpes(不安全)提供的get或者破解match.fun並進行更改

envir <- parent.frame(2) to envir <- parent.frame(1)

編寫自己的match.fun版本相對容易(並且是說明性的)。 我調用了我的函數fget來表明它是為函數專門設計的get版本,因此遵循函數的常規作用域規則。 (如果您不確定它們是什么,請考慮以下代碼: c <- 10; c(c, 5)

#' Find a function with specified name.
#'
#' @param name length one character vector giving name
#' @param env environment to start search in.
#' @examples
#' c <- 10
#' fget("c")
fget <- function(name, env = parent.frame()) {
  if (identical(env, emptyenv())) {
    stop("Could not find function called ", name, call. = FALSE)
  }

  if (exists(name, env, inherits = FALSE) && is.function(env[[name]])) {
    env[[name]]
  } else {
    fget(name, parent.env(env))
  }
}

實現是一個簡單的遞歸函數:基本情況是emptyenv() ,每個環境的最終祖先,並且對於父項堆棧中的每個環境,我們檢查是否存在名為name的對象,以及它是一個功能。

它適用於@nograpes提供的簡單測試用例,因為環境默認為調用環境:

fun <- function(x) {
  n <- sum
  fget('n')(x)
}
fun(10)
# [1] 10

由於我還不完全理解的原因,如果你使用get而不是match.fun ,一切正常。

x <- matrix(rnorm(10*100), nrow=100) # data sample
descStats <- function(x, stats = c("n", "min", "max", "srange", "mean", "median", "sd")) {
  n <- function(x, ...) sum(!is.na(x), ...)
  srange <- function(x, ...) max(x, ...) - min(x, ...)
  fun <- function(x) {
    # get added here.
    result <- vapply(stats, function(z) get(z)(x, na.rm=TRUE), FUN.VALUE=numeric(1))
  }
  if (is.vector(x)) {
    result <- fun(x)
  }
  if (is.matrix(x) || is.data.frame(x)) {
    result <- t(apply(x, 2, fun))
  }
  return(result)
}
descStats(x)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM