在 R 程序中询问用户输入的正确方法是什么?

发布于 2024-11-06 07:30:22 字数 1067 浏览 3 评论 0原文

如果我单独运行它们,下面的程序(分为两部分)可以工作 - 也就是说,如果我将第一部分粘贴到 R 控制台中,运行它,然后粘贴第二部分并运行它。然而,这不是我想要的。我想立即运行整个程序。如果我这样做,它会在我的控制台中显示以下错误:

1: 
Read 0 items
1: 
Read 0 items
Error in while ((n <= 0) | (acr <= 0) | (acr >= 1)) { : 
  argument is of length zero

我已尝试找出问题,但无法找到根本原因。如果有人能帮助我,我会非常高兴。

#**FIRST PART OF THE PROGRAM**

n <- -2
acr <- -2
while((n<=0) | (acr<=0) | (acr>=1)) {
   print("enter a  positive integer and the average cancellation rate between 0 and 1  
                you want")
   try(n <- scan(what=integer(), nmax=1), silent=TRUE)
   try(acr <- scan(what=double(), nmax=1), silent=TRUE)
}


#**SECOND PART OF THE PROGRAM**

bygrace <- read.table("C:\\MyRfolder\\bygrace.txt", header=FALSE)
r <- nrow(bygrace)
c <- ncol(bygrace)
copybygrace <- array(bygrace, dim=c(r, c))
copybygrace <- bygrace[-((n+1):r), ]
write.table(copybygrace,file="C:\\MyRfolder\\copybygrace.txt", sep="\t")
copybygrace <- read.table("C:\\MyRfolder\\copybygrace.txt", header=TRUE)

The my program below(which is in two parts) works if I run them separately – that is, if I paste the first part into the R Console, run it and then paste the second and run it. However, that is not how I want it. I want to run the whole program at once. If I do that it shows the following error in my console :

1: 
Read 0 items
1: 
Read 0 items
Error in while ((n <= 0) | (acr <= 0) | (acr >= 1)) { : 
  argument is of length zero

I have tried to identify the problem but I have not been able find the root cause. I would be more than glad, if someone could come to my aid.

#**FIRST PART OF THE PROGRAM**

n <- -2
acr <- -2
while((n<=0) | (acr<=0) | (acr>=1)) {
   print("enter a  positive integer and the average cancellation rate between 0 and 1  
                you want")
   try(n <- scan(what=integer(), nmax=1), silent=TRUE)
   try(acr <- scan(what=double(), nmax=1), silent=TRUE)
}


#**SECOND PART OF THE PROGRAM**

bygrace <- read.table("C:\\MyRfolder\\bygrace.txt", header=FALSE)
r <- nrow(bygrace)
c <- ncol(bygrace)
copybygrace <- array(bygrace, dim=c(r, c))
copybygrace <- bygrace[-((n+1):r), ]
write.table(copybygrace,file="C:\\MyRfolder\\copybygrace.txt", sep="\t")
copybygrace <- read.table("C:\\MyRfolder\\copybygrace.txt", header=TRUE)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一桥轻雨一伞开 2024-11-13 07:30:22

@Marek 说得很对。更多说明:

  • 一般来说,您不应该使用 scan() 而是使用 readline() 来执行此操作。
  • 我将拆分代码,以便清楚地了解 n 中读取的内容以及 acr 中读取的内容。
  • 考虑一下当人们按下回车键时您是否要返回到提示,或者您是否要重新提出问题直到他们填写一些正确的值。
  • 您可以使用 grepl() 的强大功能来检查输入的格式是否正确。

为了包含正确的控件并捕获所有可能的错误,以下构造更加简洁,并且在复制到控制台时不会破坏您的代码:

while(n < 1 ){
  n <- readline("enter a positive integer: ")
  n <- ifelse(grepl("\\D",n),-1,as.integer(n))
  if(is.na(n)){break}  # breaks when hit enter
}

这显示了当人们不填写任何内容时如何终止问题。 grepl 构造排除任何非数字字符,包括点。

while(is.na(acr) | acr <= 0 | acr >= 1 ){
  acr <- readline("and the average cancellation rate between 0 and 1 :")
  acr <- ifelse(grepl("[^0-9.]",acr),-1,as.numeric(acr))
}

这展示了当人们没有填写任何内容时如何重新提出问题。 grepl 排除任何非数字或点的字符。

@Marek is very right. A few more remarks :

  • In general, you shouldn't be using scan() but readline() for this.
  • I'd split the code so it becomes clear what serves to read in n, and what serves to read in acr.
  • think about whether you want to return to the prompt when people just press enter, or whether you want to reask the question until they fill in some correct value.
  • you can use the power of grepl() to check whether the input is the right format.

To include the correct controls and catch all possible mistakes, the following construct is a lot cleaner and won't break your code when copied to the console :

while(n < 1 ){
  n <- readline("enter a positive integer: ")
  n <- ifelse(grepl("\\D",n),-1,as.integer(n))
  if(is.na(n)){break}  # breaks when hit enter
}

This shows how to terminate the question when people don't fill in anything. The grepl construct exludes any character that is not a digit, including the dot.

while(is.na(acr) | acr <= 0 | acr >= 1 ){
  acr <- readline("and the average cancellation rate between 0 and 1 :")
  acr <- ifelse(grepl("[^0-9.]",acr),-1,as.numeric(acr))
}

This shows how to re-ask the question when people don't fill in anything. The grepl excludes any character that is not a digit or a dot.

旧情勿念 2024-11-13 07:30:22

这是因为当您复制并粘贴所有内容时,scan 会将粘贴的行读取为输入。

如果您将此树行复制到控制台

x <- scan(nmax=1)
1
2

x 变为 1scan 不要等待您的交互,因为它有行可以读取。

您必须将所有内容包装在 {} 中:

{
 x <- scan(nmax=1)
 1
 2
}

您必须包装程序的两个部分。更清楚地说:当您将代码粘贴到控制台时 } 应该是最后一个标志。

It's because when you copy and paste all then scan reads pasted lines as input.

If you copy this tree lines to console

x <- scan(nmax=1)
1
2

x become 1, scan don't wait for your interaction cause it got line to read.

You have to wrap everything in {}:

{
 x <- scan(nmax=1)
 1
 2
}

You have to wrap both parts of your program. To be more clear: when you paste your code to console } should be last sign.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文