2014-09-23 44 views
2

我無法抑制錯誤,雖然我的PDF文件正在轉換爲文本,但我無法抑制此錯誤。使用R的trycatch抑制系統錯誤

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 

EDITED

它停止錯誤,但它也停止功能以及,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) 

回答

0

實際上從未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) 
} 

可能有更好的方法來做到這一點,並希望別人會糾正我的解決方案。

4

通常一個會寫

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 
+0

感謝system2(),但我無法提供它的功能。 – Aashu 2014-09-23 12:45:17

+0

很高興知道這一點,但在錯誤狀態爲「狀態1」時不起作用。 – 2017-03-10 09:24:56