为 RStudio 包装错误消息,保留单词
wrap error messages for RStudio, preserving words
文本换行错误消息(message
、warning
、stop
)以避免拆分单词或文件路径的最佳方法是什么?
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."
我的想法大致如下:
- 检测控制台宽度(假设是 80 个字符)
- 如果消息长度至少为 80 个字符,则不超过就找到最接近 80 的空白字符的索引
- 用
\n
替换那个空格
- 从
\n
开始重新计数,然后重复
相关问题:
理想情况下我想避免依赖,因为这个函数是针对一个包的。
stringi
包中的 stri_wrap
已经做到了这一点,因此通过将其包装在 paste(..., collapse = "\n")
中,我们可以做到:
message(paste(stringi::stri_wrap(long_message), collapse = "\n"))
或使用{base}函数:
stop(paste(strwrap(long_message), collapse = "\n"))
文本换行错误消息(message
、warning
、stop
)以避免拆分单词或文件路径的最佳方法是什么?
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."
我的想法大致如下:
- 检测控制台宽度(假设是 80 个字符)
- 如果消息长度至少为 80 个字符,则不超过就找到最接近 80 的空白字符的索引
- 用
\n
替换那个空格
- 从
\n
开始重新计数,然后重复
相关问题:
理想情况下我想避免依赖,因为这个函数是针对一个包的。
stringi
包中的 stri_wrap
已经做到了这一点,因此通过将其包装在 paste(..., collapse = "\n")
中,我们可以做到:
message(paste(stringi::stri_wrap(long_message), collapse = "\n"))
或使用{base}函数:
stop(paste(strwrap(long_message), collapse = "\n"))