在输出末尾打印简单消息(非警告)

时间:2020-05-21 15:16:10

标签: r

是否可以在输出末尾打印诊断(简单)消息,但仍返回值?在下面的示例中,所有尝试返回数据帧并在末尾显示消息的尝试均无效。

使用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创建

0 个答案:

没有答案