nsheff nsheff - 1 year ago 58
R Question

How do you format multiline R package messages?

In developing an R package, I would like to use

R
's
message()
or
warning()
functions to produce output for my package user.

Sometimes these messages can be long. I can do this (text is just trivial example):

message("If you got to this point in the code, it means the matrix was empty. Calculation continues but you should consider re-evaluating an earlier step in the process")


Great... But for style, I also want my lines of code to be less than 80 characters, so they fit nicely in narrow screens, on GitHub, etc. And then I can use an IDE code reflow tool to reformat my message easily if it changes.

So I try this:

message("If you got to this point in the code, it means
the matrix was empty. Calculation continues but you should consider
re-evaluating an earlier step in the process")


This solves my code criteria -- it is less than 80 character lines and can reflow as expected. But that sticks the whitespace right in my message output, which I also don't want:

If you got to this point in the code, it means
the matrix was empty. Calculation continues but you should consider
re-evaluating an earlier step in the process


So I found this handy function called
strwrap()
that seems to solve the problem:

message(strwrap("If you got to this point in the code, it means
the matrix was empty. Calculation continues but you should consider
re-evaluating an earlier step in the process"))


Output:

If you got to this point in the code, it means the matrix was empty.
Calculation continues but you should considerre-evaluating an earlier
step in the process


Looks nice -- but it eliminated the space between "consider" and "re-evaluating" because that space was at a newline.

Another alternative is to break it up into chunks in the code:

message("If you got to this point in the code, it means ",
"the matrix was empty. Calculation continues but you should consider ",
"re-evaluating an earlier step in the process")


This makes the output look correct, but the text can no longer easily reflow with IDE, etc, because it's not one string, so this doesn't work for me on the dev side.

So: How can I make a nicely formatted message that lets me write the message easily across lines?

I have written this function:

.nicemsg = function(...) {
message(paste(strwrap(...), collapse="\n"))
}


Is there a better way using a built-in so I don't have to include this function in every R package I write?

Answer Source

Using a couple more arguments from strwrap makes this possible

message(strwrap(..., prefix = " ", initial = ""))

You might be able to improve readability by playing with the order of arguments. I'm not sure if this is better or not.

message(strwrap(prefix = " ", initial = "", 
  "If you got to this point in the code, it means 
the matrix was empty. Calculation continues but you should consider
 re-evaluating an earlier step in the process"))

Or, if you prefer wrapping it

tidymess <- function(..., prefix = " ", initial = ""){
  message(strwrap(..., prefix = prefix, initial = initial))
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download