如何使用 R 允许来自用户的多个输入? [英] How to allow multiple inputs from user using R?

查看:27
本文介绍了如何使用 R 允许来自用户的多个输入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我需要用户指定矩阵的行数和列数:

For example, if I need that the user specifies the number of rows and columns of a matrix:

提示:行数?:

用户输入:[一个数字]

USER INPUT: [a number]

我需要 R等待"输入.然后将 [a number] 保存到变量 v1 中.接下来,

I need that R 'waits' for the input. Then save [a number] into a variable v1. Next,

提示:列数?:

用户输入:[另一个号码]

USER INPUT: [another number]

同时将 [another number] 保存到变量 v2 中.最后,我将有两个变量 (v1, v2) 将在其余代码中使用.

Also save [another number] into a variable v2. At the end, I will have two variables (v1, v2) that will be used in the rest of the code.

readline"一次仅适用于一个输入.我不能同时运行两条线

"readline" only works for one input at a time. I can't run the two lines together

v1 <- readline("Number of rows?: ")
v2 <- readline("Number of columns?: ")

有什么想法或建议吗?

提前致谢

推荐答案

您可以将这些语句组合成一个子句:

You can combine those statements into a clause:

{ v1 <- readline("Number of rows?: "); v2 <- readline("Number of columns?: ") }

或者一般来说,把它们变成一个函数:

Or generally, make them into a function:

readlines <- function(...) {
   lapply(list(...), readline)
}
readlines("Number of rows?: ", "Number of columns?: ")

这篇关于如何使用 R 允许来自用户的多个输入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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