4

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)
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
Aashu
  • 1,247
  • 1
  • 26
  • 41

2 Answers2

8

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
Martin Morgan
  • 45,935
  • 7
  • 84
  • 112
0

Never actually used tryCatch() before but it seems your error function is error=function(e) e so you are just returning the error you get.

I have used try() in the past with option silent=TRUE to avoid showing the error message. Something like this (disclaimer, the help page for try mentions that using tryCatch may be more efficient):

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

There may be better ways to do this and hopefully others will correct my solution.

ddiez
  • 1,087
  • 11
  • 26
  • The question is about `system()` errors, for which your code won't work, try `try(system("impossible"), silent=TRUE)` – Matifou Feb 18 '22 at 10:40