如何格式化多行 R 包消息?
How do you format multiline R package messages?
在开发 R 包时,我想使用 R
的 message()
或 warning()
函数为我的包用户生成输出。
有时这些消息可能会很长。我可以做到这一点(文本只是微不足道的例子):
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")
很好...但是为了风格,我还希望我的 行代码少于 80 个字符,这样它们就可以很好地适应狭窄的屏幕,在 GitHub, 等。然后我可以使用 IDE 代码回流工具轻松地重新格式化我的消息,如果它发生变化。
所以我试试这个:
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")
这解决了我的代码标准 -- 它少于 80 个字符行并且可以按预期重排。但这会在我的消息输出中留下白色space,我也不想要:
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
所以我发现这个名为 strwrap()
的函数似乎可以解决问题:
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"))
输出:
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
看起来不错——但它消除了 "consider" 和 "re-evaluating" 之间的 space,因为 space 是换行符。
另一种方法是在代码中将其分解成块:
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")
这使得输出看起来是正确的,但是文本不能再用 IDE 等轻松地回流,因为它不是一个字符串,所以这对我来说在开发方面不起作用。
那么:我怎样才能制作出格式良好的消息,让我可以轻松地跨行编写消息?
我写过这个函数:
.nicemsg = function(...) {
message(paste(strwrap(...), collapse="\n"))
}
是否有使用内置函数的更好方法,这样我就不必在我编写的每个 R 包中都包含此函数?
您可以通过在字符串中添加 \n
来强制换行。
message("If you got to this point in the code,\nit means the matrix was empty.\nCalculation continues but you should consider re-evaluating\nan earlier step in the process")
# 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
使用来自 strwrap
的更多参数使这成为可能
message(strwrap(..., prefix = " ", initial = ""))
您可以通过调整参数顺序来提高可读性。我不确定这是否更好。
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"))
或者,如果您喜欢包装它
tidymess <- function(..., prefix = " ", initial = ""){
message(strwrap(..., prefix = prefix, initial = initial))
}
在开发 R 包时,我想使用 R
的 message()
或 warning()
函数为我的包用户生成输出。
有时这些消息可能会很长。我可以做到这一点(文本只是微不足道的例子):
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")
很好...但是为了风格,我还希望我的 行代码少于 80 个字符,这样它们就可以很好地适应狭窄的屏幕,在 GitHub, 等。然后我可以使用 IDE 代码回流工具轻松地重新格式化我的消息,如果它发生变化。
所以我试试这个:
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")
这解决了我的代码标准 -- 它少于 80 个字符行并且可以按预期重排。但这会在我的消息输出中留下白色space,我也不想要:
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
所以我发现这个名为 strwrap()
的函数似乎可以解决问题:
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"))
输出:
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
看起来不错——但它消除了 "consider" 和 "re-evaluating" 之间的 space,因为 space 是换行符。
另一种方法是在代码中将其分解成块:
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")
这使得输出看起来是正确的,但是文本不能再用 IDE 等轻松地回流,因为它不是一个字符串,所以这对我来说在开发方面不起作用。
那么:我怎样才能制作出格式良好的消息,让我可以轻松地跨行编写消息?
我写过这个函数:
.nicemsg = function(...) {
message(paste(strwrap(...), collapse="\n"))
}
是否有使用内置函数的更好方法,这样我就不必在我编写的每个 R 包中都包含此函数?
您可以通过在字符串中添加 \n
来强制换行。
message("If you got to this point in the code,\nit means the matrix was empty.\nCalculation continues but you should consider re-evaluating\nan earlier step in the process")
# 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
使用来自 strwrap
的更多参数使这成为可能
message(strwrap(..., prefix = " ", initial = ""))
您可以通过调整参数顺序来提高可读性。我不确定这是否更好。
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"))
或者,如果您喜欢包装它
tidymess <- function(..., prefix = " ", initial = ""){
message(strwrap(..., prefix = prefix, initial = initial))
}