我如何 return 到 R 函数中的特定行或参数?
How do I return to a certain line or argument in a function in R?
我有一个函数可以根据用户的输入验证 url。函数设置的方式,如果 url 没有验证(因为它不存在或者因为它是重复的)函数简单地结束。
如果 url 未通过验证,我该如何做到这一点,用户返回输入对话框重新开始验证过程,而不是仅仅结束该功能?
exfun <- function(){
x <- toupper(readline("Do you want to do the function? Y/N......."))
if (x == "Y"){
writeLines("This is where the function body would be, but it's huge so for the sake of this StackExchange question, we'll just make it a simple thing")
} else
writeLines("Well then why did you start the function? We'll try this again")
#This is where I would like the function to return to the "x<-...." line.
有几个地方我希望能够 "return to line X" 因为至少有两个验证点。我将它设置为一系列 if else
个参数,这些参数 else
到一条消息中。我怎样才能将它 else
变成消息并将用户带回 beginning/a 之前的验证测试?
我摆弄了 repeat
函数,但无法将其从 if else
正确地转换为 return。
我并没有真正回答你的问题,但我会给你一个可能对你有帮助的例子(可能有人会给出改进的提示)。
您可以设置一个 while
循环并设置继续循环或结束循环的条件,就像这样:
i <- 1
while (T) {
print(i)
i <- i + 1
if (i==5) {
print("NEXT")
next
}
if (i==10) break
}
如您所见,带有 next
条件的 if
在您的代码中执行某些操作,并在命令 print("NEXT")
之后保留 运行。此外,break
处于停止循环的条件内。
希望对你有所帮助,因为你的例子很难给出完整的答案。
我有一个函数可以根据用户的输入验证 url。函数设置的方式,如果 url 没有验证(因为它不存在或者因为它是重复的)函数简单地结束。
如果 url 未通过验证,我该如何做到这一点,用户返回输入对话框重新开始验证过程,而不是仅仅结束该功能?
exfun <- function(){
x <- toupper(readline("Do you want to do the function? Y/N......."))
if (x == "Y"){
writeLines("This is where the function body would be, but it's huge so for the sake of this StackExchange question, we'll just make it a simple thing")
} else
writeLines("Well then why did you start the function? We'll try this again")
#This is where I would like the function to return to the "x<-...." line.
有几个地方我希望能够 "return to line X" 因为至少有两个验证点。我将它设置为一系列 if else
个参数,这些参数 else
到一条消息中。我怎样才能将它 else
变成消息并将用户带回 beginning/a 之前的验证测试?
我摆弄了 repeat
函数,但无法将其从 if else
正确地转换为 return。
我并没有真正回答你的问题,但我会给你一个可能对你有帮助的例子(可能有人会给出改进的提示)。
您可以设置一个 while
循环并设置继续循环或结束循环的条件,就像这样:
i <- 1
while (T) {
print(i)
i <- i + 1
if (i==5) {
print("NEXT")
next
}
if (i==10) break
}
如您所见,带有 next
条件的 if
在您的代码中执行某些操作,并在命令 print("NEXT")
之后保留 运行。此外,break
处于停止循环的条件内。
希望对你有所帮助,因为你的例子很难给出完整的答案。