【发布时间】:2020-12-03 09:19:11
【问题描述】:
我正在尝试从内部包函数 (stop_quietly()) 中调用 stop(),它应该会中断该函数并返回到顶部。这可行,除了 R CMD Check 认为这是一个错误,因为我正在强制停止。
如何绕过 R CMD 检查将此解释为错误?该函数需要停止,因为它需要用户输入作为确认,然后才能在给定位置创建文件目录树。该代码当前生成一条消息并停止该函数。
tryCatch({
path=normalizePath(path=where, winslash = "\\", mustWork = TRUE)
message(paste0("This will create research directories in the following directory: \n",path))
confirm=readline(prompt="Please confirm [y/n]:")
if(tolower(stringr::str_trim(confirm)) %in% c("y","yes","yes.","yes!","yes?")){
.....
dir.create(path, ... [directories])
.....
}
message("There, I did some work, now you do some work.")
}
else{
message("Okay, fine then. Don't do your research. See if I care.")
stop_quietly()
}
},error=function(e){message("This path does not work, please enter an appropriate path \n or set the working directory with setwd() and null the where parameter.")})
stop_quietly 是一个退出函数I took from this post,修改了error=NULL,它抑制了R 作为浏览器执行错误处理程序。我不希望函数终止到浏览器我只是希望它退出而不在 R CMD 检查中引发错误。
stop_quietly <- function() {
opt <- options(show.error.messages = FALSE, error=NULL)
on.exit(options(opt))
stop()
}
这是 R CMD 产生的错误的组成部分:
-- R CMD check results ------------------------------------------------ ResearchDirectoR 1.0.0 ----
Duration: 12.6s
> checking examples ... ERROR
Running examples in 'ResearchDirectoR-Ex.R' failed
The error most likely occurred in:
> base::assign(".ptime", proc.time(), pos = "CheckExEnv")
> ### Name: create_directories
> ### Title: Creates research directories
> ### Aliases: create_directories
>
> ### ** Examples
>
> create_directories()
This will create research directories in your current working directory:
C:/Users/Karnner/AppData/Local/Temp/RtmpUfqXvY/ResearchDirectoR.Rcheck
Please confirm [y/n]:
Okay, fine then. Don't do your research. See if I care.
Execution halted
【问题讨论】:
-
出于好奇,
R CMD check产生的确切错误消息是什么? (这可能与问题无关,但听起来很奇怪。) -
@KonradRudolph:我猜想
stop()的调用发生在一个例子中。示例不允许产生错误,除非您将它们包装在tryCatch()或等效项中。 -
@KonradRudolph 它是由一个例子抛出的,所以我想知道如何将它包装在 tryCatch() 中而不将其称为错误......因为它本身不是错误。错误添加到原始帖子。
-
@JonathanFluharty-Jaidee Ha,好的,知道了。显然
R CMD checkitself 依赖于已安装的错误处理程序,因此如果禁用它,它将失败。您需要确保R CMD check不会最终执行该特定代码(例如,通过示例,如您的情况,或在小插图中)。要解决此问题,只需将示例代码包装在\donotrun{…}中。
标签: r namespaces exit