0

I want to replace print()/cat() by message()/warning() as I want it to be suppressed.

However, my code looks quite nested print()/cat() and used print()/cat() multiple times.

I wonder if there is an easier way to fix this problem one-off.

Instead of do packageStartupMessage() & suppressPackageStartupMessages() for each time I used cat() and print()

Also, I am not quite sure, should I just replace print()/cat() directly without affect setMethod() and other functions.

Also, I also get advice about, but how can I used that in my situation?

if(verbose)cat(..) (or maybe stop()) if you really have to write text to the console.
(except for print, summary, interactive functions)

this is my code.

setMethod("print", "ga", function(x, ...) str(x))

setMethod(
  "show", "ga",
  function(object) {
    cat("An object of class \"ga\"\n")
    cat("\nCall:\n", deparse(object@call), "\n\n", sep = "")
    cat("Available slots:\n")
    print(slotNames(object))
  }
)
... ...

if (x$type == "real-valued") {
    cat(paste("Search domain = \n"))
    print(x$domain, digits = digits)
  }
  • You still have 2 print statements, replace both with `cat`? – Rui Barradas Aug 02 '20 at 10:27
  • But cat() is also not a suppressed function. I have to replace both cat() and print() –  Aug 02 '20 at 10:49
  • It looks like you're using a `show` method to build the console representation of your class. Perhaps you should instead declare a `format` method which builds the representation as a character string. That way, you never have to explicitly call `cat` or `print` – Allan Cameron Aug 02 '20 at 11:06
  • Thank you for reply Allan. What is a ```format``` method? –  Aug 02 '20 at 12:41
  • You keep posting this incomplete code. Where is that final `if` statement? Is it really standing alone, outside of any function? That doesn't make sense. Regarding your question, you're allowed to use `print` and `cat` in `show` methods, but should not be using them in other functions/methods unless a user asks for verbose output. – user2554330 Aug 02 '20 at 13:01

1 Answers1

0

Thank you everyone's helping today. The solution I figure out is just replacing all the print() and cat() function to message().