English 中文(简体)
建立一个由职能归还的所有信息、警告和错误清单
原标题:Create a list of all messages, warnings, and errors returned by a function

我在R中担负着其他职能。 这些职能可以回来。 我只读一字。 这样做的最佳途径是什么?

在以下例子中,<代码>功能C将印刷“价值5”两倍,如果b和c的论点是5。 我只想一次印刷。

functionA <- function(a){
  
  if(a==5){
    message("The value is 5.")
  }
  return(a-2)
}

functionB <- function(b){
  
  if(b==5){
    message("The value is 5.")
  }
  return(b+7)
}

functionC <- function(a,b){
  a <- functionA(a)
  b <- functionB(b)
  c <- rbind(a,b)
  return(c)
}

functionC(5,5)
最佳回答

您可使用<代码>与CallingHandlers实时传送(和压制)电文,然后使用<代码>unique予以减少,然后将<>message予以删除。 为透明起见(否则,你可能不会清楚地知道如何进行内部调动),请加上<条码>(时段)。

functionC <- function(a,b){
  msgs <- character(0)
  a <- withCallingHandlers(
    functionA(a),
    message = function(m) {
      msgs <<- c(msgs, conditionMessage(m))
      invokeRestart("muffleMessage")
    })
  b <- withCallingHandlers(
    functionB(b),
    message = function(m) {
      msgs <<- c(msgs, conditionMessage(m))
      invokeRestart("muffleMessage")
    })
  msgs <- trimws(msgs)
  # since  table  does not preserve the original order, we ll do a few
  # extra steps to ensure the messages appear in the order of their
  # _first_ appearance
  counts <- table(msgs)
  counts <- counts[match(names(counts), msgs)]
  msgs <- paste0(names(counts), ifelse(counts > 1, sprintf(" (%d times)", counts), ""))
  for (m in msgs) message(m)
  c <- rbind(a, b)
  return(c)
}
functionC(5,5)
# The value is 5. (2 times)
#   [,1]
# a    3
# b   12

您也可重复<代码>message=/>>>>muffleMessage" with press=>>>>>>>----“muffle Warning”------------------------------------------------------------

functionC2 <- function(a,b){
  msgs <- character(0)
  warns <- character(0)
  aout <- purrr::quietly(functionA)(a)
  bout <- purrr::quietly(functionB)(b)
  fewer <- function(z) {
    z <- trimws(z)
    counts <- table(z)
    counts <- counts[match(names(counts), z)]
    paste0(names(counts), ifelse(counts > 1, sprintf(" (%d times)", counts), ""))
  }
  msgs <- fewer(c(aout$messages, bout$messages))
  warns <- fewer(c(aout$warnings, bout$warnings))
  cout <- rbind(aout$result, bout$result)
  for (m in msgs) message(m)
  for (w in warns) warning(w)
  return(cout)
}
问题回答

Would suppressing the functionA/functionB warnings and including a new a and/or b is 5 warning suit your use-case? E.g.

functionA <- function(a){
  if(a==5){
    message("The value is 5.")
  }
  return(a-2)
}

functionB <- function(b){
  if(b==5){
    message("The value is 5.")
  }
  return(b+7)
}

functionC <- function(a,b){
  a <- functionA(a)
  b <- functionB(b)
  c <- rbind(a,b)
  return(c)
}
functionC(5,5)
#> The value is 5.
#> The value is 5.
#>   [,1]
#> a    3
#> b   12

functionD <- function(a,b){
  if(a==5 | b==5){
    message("The value of a and/or b is 5.")
  }
  a <- suppressMessages(functionA(a))
  b <- suppressMessages(functionB(b))
  c <- rbind(a,b)
  return(c)
}
functionD(5,5)
#> The value of a and/or b is 5.
#>   [,1]
#> a    3
#> b   12

Created on 2023-08-18 with reprex v2.0.2





相关问题
Separating Business Layer Errors from API errors

The title is horrible, i know; I m terrible at titles on SO here. I m wondering what would be the best way to present unified error responses in a webapi when errors could be raised deep inside the ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

How to tell why a file deletion fails in Java?

File file = new File(path); if (!file.delete()) { throw new IOException( "Failed to delete the file because: " + getReasonForFileDeletionFailureInPlainEnglish(file)); } Is there a ...

Exceptions: redirect or render?

I m trying to standardize the way I handle exceptions in my web application (homemade framework) but I m not certain of the "correct" way to handle various situations. I m wondering if there is a ...

热门标签