首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >R中尝试捕获抑制系统误差

R中尝试捕获抑制系统误差
EN

Stack Overflow用户
提问于 2014-09-23 09:49:49
回答 2查看 4.2K关注 0票数 4

我无法抑制错误,虽然我的pdf文件被转换为文本,但我无法抑制此错误。

代码语言:javascript
复制
 tryCatch(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T))
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
> suppressWarnings(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T))
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
>tryCatch(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T), error = function(e) e)
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
> suppressMessages(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T))
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
> tryCatch(suppressWarnings(capture.output(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T))), error = function(e) e)
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
character(0)
> tryCatch({system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T)}, error = function(e) e)
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function

为什么我不能压制它呢?我怎样才能摆脱这个错误呢?谢谢。

编辑

使用silent=TRUEfunction(e){invisible(e)}

代码语言:javascript
复制
tryCatch(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T), error = function(e){invisible(e)})
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
> try(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = T),silent=TRUE)
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function
Syntax Error: Missing or invalid 'Bounds' entry in stitching function

编辑的

它停止了错误,但是它也停止了功能,pdf文件没有被转换成文本。

代码语言:javascript
复制
tryCatch(system2(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = TRUE, 
                 stdout=TRUE, stderr=TRUE), 
         error=function(err) NA)
[1] NA

源代码

代码语言:javascript
复制
dest=paste("C:/wamp/www/LU/my/pdffiles",file_list[i],sep="/")
exe <- "C:\\Program Files\\xpdfbin-win-3.03\\bin32\\pdftotext.exe"
try(system(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = TRUE),
    silent=TRUE)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-09-23 12:20:52

通常一个人会写

代码语言:javascript
复制
tryCatch({
    system("xyz")
}, error=function(err) {
    ## do something with 'err', then maybe throw it again stop(err)
    ## or providing an alternative return value
    NA
})

所以

代码语言:javascript
复制
> xx = tryCatch(stop("oops"), error=function(err) NA)
> xx
[1] NA

但是,system()抛出的一些错误似乎无法以这种方式捕获。

代码语言:javascript
复制
> xx = tryCatch(system("foobar"), error=function(err) NA)
sh: 1: foobar: not found

可能会做一些事情的暗示来自xx的值:

代码语言:javascript
复制
> xx
[1] 127

帮助页面首先将我们指向system2(),在这里我们可以看到

代码语言:javascript
复制
If 'stdout = TRUE' or 'stderr = TRUE', a character vector giving
the output of the command, one line per character string.  (Output
lines of more than 8095 bytes will be split.)  If the command
could not be run an R error is generated.

而且很确定

代码语言:javascript
复制
> tryCatch(system2("foobar", stdout=TRUE, stderr=TRUE), error=function(err) NA)
[1] NA
票数 8
EN

Stack Overflow用户

发布于 2014-09-23 10:10:17

以前从未实际使用过tryCatch(),但似乎您的错误函数是error=function(e) e,所以您只是返回所得到的错误。

我在过去使用过try()和选项silent=TRUE,以避免显示错误消息。类似这样的内容(免责声明,try的帮助页面提到使用tryCatch可能更有效):

代码语言:javascript
复制
res=try(runif(Inf), silent=TRUE)
if(class(res)=="try-error") {
  print("some error happened")
} else {
  print(res)
}

res=try(runif(10), silent=TRUE)
if(class(res)=="try-error") {
  print("some error happened")
} else {
  print(res)
}

也许有更好的方法来做到这一点,希望其他人能纠正我的解决方案。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25991973

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档