我的文件上有一个适用于矩阵的代码,我使用
读取它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”)
并且还收到诸如“你想继续吗?”之类的问题。 预先感谢您的所有建议! 嘎布
最佳答案
将其放在文件的开头:
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.")
关于r - R打印警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39390115/