R打印警告

printing warnings by R

我有一个在矩阵上运行的文件代码,我使用

读取它

source("filecode.r")

由于代码使用的矩阵必须具有某些特定特征,因此我想打印一条消息以记住用户输入矩阵的格式必须具有这些特征。

代码是这样的:

n<- nrow(aa)
d_ply(aa, 1, function(row){
cu<- dist(as.numeric(row[-1]))
cucu<- as.matrix(cu)
saveRDS(cucu, file = paste0(row$ID, ".rds"))
}, .progress='text', .print = TRUE)

理想情况下,我想在代码开始之前添加一条警告消息 运行...像这样:

Warning(“1) did you write ‘ID’ in position [1,1] of the input matrix?;  
2) is your matrix saved as a .txt? 
3) ensure that the matrix file does not have empty rows at the end”)

并且还收到类似 "do you want to go on then?" 的问题。 预先感谢您的所有建议! 唠叨

将其放在文件的开头:

check <- readline(prompt="Warning!\n(1) did you write 'ID' in position [1,1] of the input matrix? \n(2) is your matrix saved as a .txt?\n(3) ensure that the matrix file does not have empty rows at the end\n\n Do you wish to continue? (y/n)")
if(check == "n") stop("Aborted.")
print(check)  #Here would follow your code instead

如果您键入 "y",将计算以下代码。如果您键入 "n",脚本将停止并打印 stop().

中的消息


您还可以通过将提示语句放在 while 循环中来确保仅接受 'y' 和 'n':

check <- NA
while(!(check %in% c('y','n'))) {
  check <- readline(prompt="Warning!\n(1) did you write 'ID' in position [1,1] of the input matrix? \n(2) is your matrix saved as a .txt?\n(3) ensure that the matrix file does not have empty rows at the end\n\n Do you wish to continue? (y/n)")
}
if(check == "n") stop("Aborted.")