为 RStudio 包装错误消息,保留单词

wrap error messages for RStudio, preserving words

文本换行错误消息(messagewarningstop)以避免拆分单词或文件路径的最佳方法是什么?

long_message <- paste("Gee I'm really sorry but I couldn't find the file at",
                      "/usr/yourname/doc/misc/foo/bar/baz.txt because the directory",
                      "/usr/yourname/doc/misc/foo/bar/ doesn't exist.  Take a deep",
                      "breath, step away from the keyboard, do some relaxation",
                      "exercises, and try again."

我的想法大致如下:

相关问题:

理想情况下我想避免依赖,因为这个函数是针对一个包的。

stringi 包中的

stri_wrap 已经做到了这一点,因此通过将其包装在 paste(..., collapse = "\n") 中,我们可以做到:

message(paste(stringi::stri_wrap(long_message), collapse = "\n"))

或使用{base}函数:

stop(paste(strwrap(long_message), collapse = "\n"))