创建提示/回答系统以将数据输入到 R [英] Creating a Prompt/Answer system to input data into R

查看:38
本文介绍了创建提示/回答系统以将数据输入到 R的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一些 R 代码供那些对 R 一无所知的人使用(尽管我自己很环保).我一直让人们将初始数据粘贴到 R 控制台(结果好坏参半),我希望为人们设置一种更用户友好的方式来输入数据.

I've created some R code for use by people who know nothing of R (though I'm pretty green myself). I've been having people paste in the initial data to the R console (with mixed results), and I was hoping to set up a more user friendly way for people to enter in data.

理想情况下,有人可以坐在控制台前,输入命令,然后系统会提示有关如何输入数据的具体问题.

Ideally someone could sit down at the console, type in a command, and be prompted with specific questions on how to enter the data.

例如,一个人加载 r 并看到一个提示:

For example, a person loads up r and sees a prompt:

What is x value?

此人输入:

2

下一个提示:

What is y value?

人员输入:

3

下一个提示:

 What are T values?

人员输入:

 4,3,2,1

下一个提示:

什么是 V 值?

人员输入:

4,5,6,9

有了这 4 个新定义的变量 (X,Y,T,V),R 的下一步是运行预先编写的代码

And with these 4 newly defined variables (X,Y,T,V) R's next step is to run the pre-written code

X+Y
V+T

在控制台中弹出答案

5
8 8 8 10

大家都很开心

我很抱歉,因为这不是一个可重现的代码类型的问题,但我不知道如何让 R 提问而不是我问关于 R 的问题!

My apologies as this is not a reproducible code kind of question, but I'm not sure how to approach making R ask questions as opposed to me asking question about R!

推荐答案

由于这应该仅用作交互式代码,readline() 可以为您工作.我没有添加任何错误检查,但您可能需要做大量的检查以确保正确的输入.不过,这是核心概念:

Since this is supposed to be used as interactive code only, readline() can work for you. I did not add any error checking, but you'd probably want to do a fair amount of that to ensure proper input. Here's the core concept though:

fun <- function(){
  x <- readline("What is the value of x?")  
  y <- readline("What is the value of y?")
  t <- readline("What are the T values?")
  v <- readline("What are the V values?")

  x <- as.numeric(unlist(strsplit(x, ",")))
  y <- as.numeric(unlist(strsplit(y, ",")))
  t <- as.numeric(unlist(strsplit(t, ",")))
  v <- as.numeric(unlist(strsplit(v, ",")))

  out1 <- x + y
  out2 <- t + v

  return(list(out1, out2))

}

这篇关于创建提示/回答系统以将数据输入到 R的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆