r - R打印警告

标签 r warnings

我的文件上有一个适用于矩阵的代码,我使用

读取它

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() 中打印消息。

enter image description here


您还可以通过将提示语句放入 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/

相关文章:

r - 从嵌套列表中提取到数据框

r - 将 R 中具有不同范围间隔的 2 个数据集组合起来,创建一个新数据集,其范围可解释原始数据集中的重叠

c - 警告 : assignment makes point from integer without cast

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

r - 以编程方式将文件加载到 Rstudio 中

r - 如何加入位置数据(纬度,经度)

iphone - xcode: 属性 'title' 'copy' 属性与父类(super class) 'UIViewController' 属性不匹配

java - JUnit 和通过公共(public) api 导出非公共(public)类型的警告

r - R 中的 par(mfrow) 用于 ggplot

冲突类型错误和整数指针,没有强制转换警告 C