我使用 cat() 创建警告消息,但结果警告消息出现在“警告消息:”部分之前。是否有不同的方法来创建此警告消息,以便在“警告消息:”之后打印?
这就是输出的样子:
> acceptable_classes <- c(
+ "character",
+ "gg",
+ "ggplot",
+ "data.frame",
+ "flextable"
+ )
>
> # This prints the correct way:
> if(TRUE){
+ warning("Some result is not an acceptable class.")
+ }
Warning message:
Some result is not an acceptable class.
>
> # This prints backwards:
> if(TRUE){
+ warning(
+ cat(
+ "Some result is not an acceptable class.",
+ "Acceptable classes are:",
+ acceptable_classes,
+ sep = "\n"
+ )
+ )
+ }
Some result is not an acceptable class.
Acceptable classes are:
character
gg
ggplot
data.frame
flextable
Warning message:
正如评论中提到的,您应该避免使用
cat
,而是使用 paste
,例如:
acceptable_classes <- letters[1:5]
if(TRUE){
warning(
paste(
"Some result is not an acceptable class.",
"Acceptable classes are:",
paste(acceptable_classes, collapse = ", "),
sep = "\n"
)
)
}
结果是:
Warning message:
Some result is not an acceptable class.
Acceptable classes are:
a, b, c, d, e