是否可以在输出末尾打印诊断(简单)消息,但仍返回值?在下面的示例中,所有尝试返回数据帧并在末尾显示消息的尝试均无效。
使用warning()
可获得理想的效果-仅在我的R会话中有趣,而在使用reprex
时却没有-所以这可能是选项设置的问题吗?
sort_message1 <- function() {
message("that comes too early")
data.frame(a = 1)
}
sort_message2 <- function() {
data.frame(a = 1)
message("should return the data frame too")
}
sort_message3 <- function() {
print(data.frame(a = 1))
message("prints the data frame, but does not return it")
}
sort_message4 <- function() {
return(data.frame(a = 1))
message("does obviously not print message")
}
sort_message1()
#> that comes too early
#> a
#> 1 1
sort_message2()
#> should return the data frame too
sort_message3()
#> a
#> 1 1
#> prints the data frame, but does not return it
sort_message4()
#> a
#> 1 1
由reprex package(v0.3.0)于2020-05-21创建