tryCatch in R [Software]

posted by mittyri – Russia, 2022-09-01 16:07 (595 d 23:03 ago) – Posting: # 23250
Views: 1,607

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
22,987 posts in 4,824 threads, 1,664 registered users;
89 visitors (0 registered, 89 guests [including 9 identified bots]).
Forum time: 15:10 CEST (Europe/Vienna)

The only way to comprehend what mathematicians mean by Infinity
is to contemplate the extent of human stupidity.    Voltaire

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