我正在尝试在 R 中编写一个将两个输入作为字符串的函数。如果没有设置输入,它会要求输入,然后继续执行该功能。
Input < - function(j,k){
if ((j==j)&&(k==k)){
j <- readline(prompt="Enter Input 1: ")
k <- readline(prompt="Enter Input 2: ")
Input(j,k)
}else if ((j=="<string here>")&&(k=="<string here>")){
....
}
}
最佳答案
我认为构建您的方法的更好方法是使用可选参数并在继续之前进行测试以查看它们是否为非空,尽管诚然您发布的问题非常模糊:
Input < - function(j=NA, k=NA) {
if (is.na(j) | is.na(k)){
j <- readline(prompt="Enter Input 1: ")
k <- readline(prompt="Enter Input 2: ")
Input(j, k)
} else if ((j == "<string here>") & (k == "<string here>")) {
....
}
}
关于R函数;如果未给出输入,则要求输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30158361/