tryCatch in R [Software]

posted by mittyri – Russia, 2022-09-01 16:07 (747 d 02:11 ago) – Posting: # 23250
Views: 2,330

Hi ElMaestro,

❝ It works with the hamburger example but not when I invoke gcc :


> tryCatch(

❝ +   {   

❝ +     a <- system("gcc -O2 -Wall Tester301.c -o T301", intern=T)

❝ +   },

❝ +   error = function(cond) {

❝ +     a <<- cond

❝ +   }

❝ + )

cc1: fatal error: Tester301.c: No such file or directory

❝ compilation terminated.

❝ Warning message:

❝ In system("gcc -O2 -Wall Tester301.c -o T301", intern = T) :

❝   running command 'gcc -O2 -Wall Tester301.c -o T301' had status 1

> a

character(0)

❝ attr(,"status")

❝ [1] 1


OK, a second attempt with already mentioned processx package
tryCatchHamburger <- function(string) {
  # initializing to prevent 'a' to go higher closures in case of error
  a <- NULL
  tryCatch({
    a <- processx::run(string, stderr = NULL)
  },
  error = function(cond) {
    a <<- cond
  })
  a
}

b <- tryCatchHamburger("Hamburger")
class(b)
# [1] "c_error"        "rlib_error_3_0" "rlib_error"     "error"          "condition" 
print(b$parent$message)
# [1] "cannot start processx process 'gcc -O2 -Wall Tester301.c -o T301' (system error 2, No such file or directory) @unix/processx.c:613 (processx_exec)"
c <- tryCatchHamburger("gcc -O2 -Wall Tester301.c -o T301")
class(c)
# [1] "c_error"        "rlib_error_3_0" "rlib_error"     "error"          "condition"   
print(c$parent$message)
# [1] "cannot start processx process 'gcc -O2 -Wall Tester301.c -o T301' (system error 2, No such file or directory) @unix/processx.c:613 (processx_exec)"

Kind regards,
Mittyri

Complete thread:

UA Flag
Activity
 Admin contact
23,226 posts in 4,879 threads, 1,657 registered users;
37 visitors (0 registered, 37 guests [including 11 identified bots]).
Forum time: 18:19 CEST (Europe/Vienna)

The real purpose of the scientific method is to make sure
nature hasn’t misled you into thinking you know something
you actually don’t know.    Robert M. Pirsig

The Bioequivalence and Bioavailability Forum is hosted by
BEBAC Ing. Helmut Schütz
HTML5