【问题标题】:Suppress system error with trycatch in R在 R 中使用 trycatch 抑制系统错误
【发布时间】:2014-09-23 09:49:49
【问题描述】:

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

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

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文件没有被转换成文本。

tryCatch(system2(paste("\"", exe, "\" \"", dest, "\"", sep = ""), wait = TRUE, 
                 stdout=TRUE, stderr=TRUE), 
         error=function(err) NA)
[1] NA

源代码

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)

【问题讨论】:

    标签: r error-handling try-catch


    【解决方案1】:

    一般都会写

    tryCatch({
        system("xyz")
    }, error=function(err) {
        ## do something with 'err', then maybe throw it again stop(err)
        ## or providing an alternative return value
        NA
    })
    

    所以

    > xx = tryCatch(stop("oops"), error=function(err) NA)
    > xx
    [1] NA
    

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

    > xx = tryCatch(system("foobar"), error=function(err) NA)
    sh: 1: foobar: not found
    

    可能做某事的提示来自xx的值:

    > xx
    [1] 127
    

    帮助页面首先将我们指向system2(),我们看到的地方

    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.
    

    果然

    > tryCatch(system2("foobar", stdout=TRUE, stderr=TRUE), error=function(err) NA)
    [1] NA
    

    【讨论】:

    • 感谢 system2() 但我无法使用它提供功能。
    • 很高兴知道这一点,但是当错误为“状态 1”时这不起作用。
    【解决方案2】:

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

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

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

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

    【讨论】:

    • 问题是关于system()的错误,你的代码不能工作,试试try(system("impossible"), silent=TRUE)
    猜你喜欢
    • 2016-06-17
    • 2021-12-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-06
    • 2017-06-10
    相关资源
    最近更新 更多