How can I wrap error messages that are very long, for code styling purposes, without deleting spaces.
Here is an example:
#fun with long message
lng_mes <- function() {
message("This is a very long message and it goes on and on and on and it makes my code look terrible because it is so long and it stretches way out into the page")
}
#when I run it, it wraps fine in the console
lng_mes()
#trying to wrap with string wrap so it's shorter in the code
lng_mes2 <- function() {
message(strwrap("This is a very long message and it goes on and on
and on and it makes my code look terrible because
it is so long and it stretches way out into the
page"))
}
#run it again, and some spaces are deleted.
lng_mes2()
This is a very long message and it goes on and on and on andit makes my code look terrible because it is so long and itstretches way out into the page
So how do I wrap my long error messages so my code looks better without changing the actual error?
>Solution :
If you pass a vector of character values to message(), it just pastes them together without any space
message(c("a","b"))
# ab
If you want to split and use new-lines between each chuck, you need to add in the new-line your self. You can do that with passte
lng_mes3 <- function() {
message(paste(strwrap("This is a very long message and it goes on and on
and on and it makes my code look terrible because
it is so long and it stretches way out into the
page"), collapse="\n"))
}
lng_mes3()
# This is a very long message and it goes on and on and on
# and it makes my code look terrible because it is so long
# and it stretches way out into the page
If you don’t want new-lines, use collapse=" " just to insert a space.