string - How do you format multiline R package messages? -


in developing r package, use r's message() or warning() functions produce output package user.

sometimes these messages can long. can (text trivial example):

message("if got point in code, means matrix empty. calculation continues should consider re-evaluating earlier step in process") 

great... style, want lines of code less 80 characters, fit nicely in narrow screens, on github, etc. , can use ide code reflow tool reformat message if changes.

so try this:

message("if got point in code, means  matrix empty. calculation continues should consider  re-evaluating earlier step in process") 

this solves code criteria -- less 80 character lines , can reflow expected. sticks whitespace right in message output, don't want:

if got point in code, means  matrix empty. calculation continues should consider  re-evaluating earlier step in process 

so found handy function called strwrap() seems solve problem:

message(strwrap("if got point in code, means  matrix empty. calculation continues should consider  re-evaluating earlier step in process")) 

output:

if got point in code, means matrix empty.  calculation continues should considerre-evaluating earlier  step in process 

looks nice -- eliminated space between "consider" , "re-evaluating" because space @ newline.

another alternative break chunks in code:

message("if got point in code, means ",  "the matrix empty. calculation continues should consider ", "re-evaluating earlier step in process") 

this makes output correct, text can no longer reflow ide, etc, because it's not 1 string, doesn't work me on dev side.

so: how can make nicely formatted message lets me write message across lines?

i have written function:

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

is there better way using built-in don't have include function in every r package write?

using couple more arguments strwrap makes possible

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

you might able improve readability playing order of arguments. i'm not sure if better or not.

message(strwrap(prefix = " ", initial = "",    "if got point in code, means  matrix empty. calculation continues should consider  re-evaluating earlier step in process")) 

or, if prefer wrapping it

tidymess <- function(..., prefix = " ", initial = ""){   message(strwrap(..., prefix = prefix, initial = initial)) } 

Comments

Popular posts from this blog

python Tkinter Capturing keyboard events save as one single string -

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

javascript - Z-index in d3.js -