tryCatch in R [Software]

posted by mittyri – Russia, 2022-09-01 16:07 (743 d 05:31 ago) – Posting: # 23250
Views: 2,316

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,224 posts in 4,878 threads, 1,654 registered users;
23 visitors (0 registered, 23 guests [including 6 identified bots]).
Forum time: 21:38 CEST (Europe/Vienna)

One of the symptoms of an approaching nervous breakdown
is the belief that one’s work is terribly important.    Bertrand Russell

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