如何格式化带有换行符的多行 R 消息?

问题描述 投票:0回答:1

基于这个优秀的问题和回复,我了解到您可以使用

message(strwrap(<some message>))
使您编写的R包中的消息在IDE中很好地流动。但是,我如何才能超越重排,变成某种格式化的消息呢?

那里接受的答案表明

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"))

这解决了OP对在换行符处保留空格的担忧。但是,如果我希望“继续计算”实际上开始一个新行,也许中间有一个硬返回,该怎么办?

r warnings r-package
1个回答
0
投票

如评论中所示,添加

initial = "\n"
表示新句子另起一行:

message(strwrap(prefix = "\n", 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"))

输出:

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

要添加硬返回,一种选择是添加两个换行符“ ") 在消息字符串中:

message(strwrap(prefix = "\n", initial = "", 
                "If you got to this point in the code, it means 
the matrix was empty.\n\nCalculation 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 consider re-evaluating an earlier step in
the process
© www.soinside.com 2019 - 2024. All rights reserved.