tryCatch in R [Software]

posted by mittyri – Russia, 2022-09-01 16:07 (870 d 07:02 ago) – Posting: # 23250
Views: 3,020

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,363 posts in 4,906 threads, 1,676 registered users;
135 visitors (0 registered, 135 guests [including 14 identified bots]).
Forum time: 22:09 CET (Europe/Vienna)

It is not every question that deserves an answer.    Publilius Syrus

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