1

在下面的stop电话中,我收到一条消息,如下所示:

Error: cname not found in the 'data'.hhk not found in the 'data'.

我想知道是否有办法让消息看起来像:

'cname', 'hhk' not found in the 'data'.

这在 Base R 中可能吗?

vars = c("cname", "hhk")
a = 1
if(a) stop(paste(vars, "not found in the 'data'."))

Current Error message: Error: cname not found in the 'data'.hhk not found in the 'data'.
Desired Error message: 'cname', 'hhk' not found in the 'data'.
4

3 回答 3

3

折叠vars成一个字符串:

vars = c("cname", "hhk")
a = 1
if(a) stop(paste(toString(vars), "not found in the 'data'."))
#Error: cname, hhk not found in the 'data'.
于 2020-10-16T05:59:13.567 回答
2

您可以使用两个paste命令来执行此操作:

if(a) stop("'",paste(paste(vars,collapse = "', '",sep = ""), "' not found in the 'data'.",sep = ""))
Error: 'cname', 'hhk' not found in the 'data'.
于 2020-10-16T06:03:08.120 回答
2

我们可以用stopifnot

stopifnot(a)

或与glue

if(a) stop(glue::glue("{toString(vars)} not found in the data"))
#Error: cname, hhk not found in the data
于 2020-10-16T19:40:17.603 回答