由R打印警告

由R打印警告,r,warnings,R,Warnings,我有一个在矩阵上工作的文件上的代码,我使用 源代码(“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")) },

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

源代码(“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)
还有一个问题,比如“你想继续吗?”。 提前感谢您的所有建议!
Gab

将其放在文件开头:

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

难道不可能在脚本中检查它,并在格式不正确时打印警告吗?每次运行脚本时都要显式地告诉脚本我要执行它,这会让我觉得很烦人。使在非交互模式下从另一个脚本运行它变得更加困难。是的,如果您是代码的唯一用户,那么您是对的。
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
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.")