Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
与用户交互:编程R以便向用户提问_R - Fatal编程技术网

与用户交互:编程R以便向用户提问

与用户交互:编程R以便向用户提问,r,R,我试图创建一个模型,其中R向用户提出一些问题,用户回答这些问题 我已经创建了两个向量。其中一个是数字,另一个是用字符写的相同数字: a <- 1:10 words <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten") > a [1] 1 2 3 4 5 6 7 8 9 10 > words [1] "one" "two" "thr

我试图创建一个模型,其中R向用户提出一些问题,用户回答这些问题

我已经创建了两个向量。其中一个是数字,另一个是用字符写的相同数字:

a <- 1:10
words <- c("one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
> a
 [1]  1  2  3  4  5  6  7  8  9 10
> words
 [1] "one"   "two"   "three" "four"  "five"  "six"   "seven" "eight" "nine"  "ten" 
a单词
[1] “一”“二”“三”“四”“五”“六”“七”“八”“九”“十”
我想创建一个脚本,对于
a
中的每个项目,它会自动问一个问题,如:
1?
然后
2?
,等等。用户回答要转到下一个号码,需要回答
1
,然后回答
2
,依此类推

如果用户犯了错误,程序需要能够记住错误并在n个问题之后再次提问。我想我应该使用while命令和
readline
,但我不确定这一点。

这应该可以:

test <- winDialogString("Please insert a value", default="")
所需循环的简单示例如下:

a <- ""
test <- 1L
while(a!=test){
  a <- winDialogString(paste0(test,"?"), default="")
}

不断询问解决方案(对于所有数字1到10),直到您正确响应,单击“取消”不会有任何效果。

一个简单的替代方法是使用“菜单”命令。@joran是的,可能是这样。但是OP没有提到任何关于选择的内容,只是开放性的问题。我只是从名字上盲目地猜测
winDialog*
只是WindowsOS?在这种情况下,我们剩下的人又开始使用
readline
或编写Tcl GUI了。@CarlWitthoft我很确定,对于基于窗口的功能,如进度条和win dialog,有一个Tcl/Tk等价物。我看看现在是否可以编辑,谢谢你的建议。
菜单
可以是图形化的(tcl/tk),也可以是跨平台的。不过,在这种情况下,您可能只需要
readline
。John我试图让问题更清楚,但您必须做出最后的调整。我可能会改变意思,因为我可能误解了真正的问题。。。
a <- ""
test <- 1L
while(a!=test){
  a <- winDialogString(paste0(test,"?"), default="")
}
a <- 1L:10L
test <- c("one","two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
res <- ""

for(i in seq_along(a)){
  if(is.null(res)) break
  while(res!=test[i]){
    res <- winDialogString(paste0(a[i],"?"), default="")
    if(is.null(res)) break 
  }
}
a <- 1L:10L
test <- c("one","two", "three", "four", "five", "six", "seven", "eight", "nine", "ten")
res <- NULL

for(i in seq_along(a))
  while(is.null(res) || res!=test[i])
    res <- winDialogString(paste0(a[i],"?"), default="")