R中的readline命令不执行其他编程行(readline command in R is not executing further programming lines)

编程入门 行业动态 更新时间:2024-10-26 10:28:00
R中的readline命令不执行其他编程行(readline command in R is not executing further programming lines)

我在R编程方面相当新颖,想要获取键盘输入来执行更多编程代码。 当执行这里给出的代码时,一切都很好,但是当输入退出时,程序终止并且它不打印y和z 。

你能告诉我如何在循环中使用readline命令并在该循环后执行其他程序行吗?

n=1 a=1 y=c() z=c() x="" while(x!="exit"){ x<-readline("Enter your name ") library(stringr) if(x!="exit" & str_detect(x,"N")){ y[n]=x n=n+1 }else{ z[a]=x a=a+1 } } print(y) print(z)

I am fairly new in R programming and want to grab keyboard entry to execute further programming codes. As the code as given here is executed, everything works all good but when the exit is entered the program is terminated and it didn't print y and z.

Could you please advise me how to use readline command in loop and execute other program lines after that loop?

n=1 a=1 y=c() z=c() x="" while(x!="exit"){ x<-readline("Enter your name ") library(stringr) if(x!="exit" & str_detect(x,"N")){ y[n]=x n=n+1 }else{ z[a]=x a=a+1 } } print(y) print(z)

最满意答案

此代码有效。 我已经将它复制到foo.R文件中,如下所示:

# in "foo.R" n = 1 a = 1 y = character() z = character() x = "" library(stringr) while (x!="exit") { x <- readline("Enter your name\n") if (x!="exit" & str_detect(x,"N")) { y[n] = x n = n+1 } else { z[a] = x a = a+1 } } print(y) print(z)

然后,从我的R控制台(具有适当的工作目录),我可以运行:

source("foo.R") # Enter your name # Bob # Enter your name # Nate # Enter your name # exit # [1] "Nate" # [1] "Bob" "exit"

它似乎工作得很好。

This code works. I have copied it in a foo.R file as so :

# in "foo.R" n = 1 a = 1 y = character() z = character() x = "" library(stringr) while (x!="exit") { x <- readline("Enter your name\n") if (x!="exit" & str_detect(x,"N")) { y[n] = x n = n+1 } else { z[a] = x a = a+1 } } print(y) print(z)

And then, from my R console (with the proper working directory), I can run :

source("foo.R") # Enter your name # Bob # Enter your name # Nate # Enter your name # exit # [1] "Nate" # [1] "Bob" "exit"

and it seems to work just fine.

更多推荐

本文发布于:2023-04-28 01:21:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1329637.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:命令   command   readline   lines   programming

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!