Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress system error with trycatch in R

I am unable to supress the error,though my pdf file is getting converted into text but i am unable to suppress this error.

 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

Any reason why am i unable to suppress it ? how can i get rid of this error ?Thanks.

EDIT

With silent=TRUE and function(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

It stopped the error but it has also stopped the functionality as well,The pdf file is not getting converted into text by this.

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

source code

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)
like image 680
Aashu Avatar asked Oct 28 '25 16:10

Aashu


1 Answers

Normally one would write

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

So

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

but it seems that some errors thrown by system() cannot be caught in this way.

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

The hint that something might be done is from the value of xx:

> xx
[1] 127

The help page first points us to system2() instead, where we see

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.

and sure enough

> tryCatch(system2("foobar", stdout=TRUE, stderr=TRUE), error=function(err) NA)
[1] NA
like image 70
Martin Morgan Avatar answered Oct 31 '25 06:10

Martin Morgan