Is there a way to print custom message when there is no error

Hi all,

I know about tryCatch to customize error message. But is there a way to incorporate messages when there is no error. For example the below code does not show any error. But can we print like " No error and the code is good"

a <- 2+6 # we get no error here. 

Can you share a bit more about use-case you have in mind? I'm not aware of anything that does exactly what you want, but perhaps your problem can be solved in some other way.

One thing that does come to mind is to do something like this:

> (a <- 2+6 )
[1] 8

If you get output, then there is no error.

Thanks for the time. I found it

a <- 2+6 ; cat('The code is good')

actually you can use

message("the code is good")

which means other people can use supressMessages() on your function if they want to ...

1 Like

I'd also like to know what you're going for. If there's a better tool for your situation, we'd be happy to let you know.

For example, if you want to check what's going on inside a function, examining the results of each statement, use debug and the commands listed under the help for ?browser.

my_fun <- function(x) {
  y <- x + 2
  z <- y^2
  z
}

debug(my_fun)
my_fun(5)
# debugging in: my_fun(5)
# debug at #1: {
#     y <- x + 2
#     z <- y^2
#     z
# }
Browse[2]> x
# [1] 5
Browse[2]> n
# debug at #2: y <- x + 2
Browse[2]> n
# debug at #3: z <- y^2
Browse[2]> y
# [1] 7
Browse[2]> n
# debug at #4: z
Browse[2]> z
# [1] 49
Browse[2]> Q

undebug(my_fun)