📌  相关文章
📜  在 R 编程中从用户那里获取输入

📅  最后修改于: 2022-05-13 01:55:15.541000             🧑  作者: Mango

在 R 编程中从用户那里获取输入

开发人员经常需要与用户交互,以获取数据或提供某种结果。今天的大多数程序都使用对话框来要求用户提供某种类型的输入。与 R 中的其他编程语言一样,它也可以从用户那里获取输入。为此,R中有两种方法。

  • 使用readline()方法
  • 使用scan() 方法

使用 readline() 方法

在 R 语言中, readline()方法以字符串格式接受输入。如果输入一个整数,那么它作为一个字符串输入,比方说,一个人想输入255 ,那么它将输入为“255” ,就像一个字符串。所以需要将输入的值转换为他需要的格式。在这种情况下,字符串“255”被转换为整数255。要将输入的值转换为所需的数据类型,R中有一些函数,

  • as.integer(n) ; —> 转换为整数
  • as.numeric(n) ; —> 转换为数字类型(浮点数、双精度等)
  • as.complex(n) ; —> 转换为复数(即 3+2i)
  • as.Date(n) —> 转换为日期……等

例子:

R
# R program to illustrate
# taking input from the user
 
# taking input using readline()
# this command will prompt you
# to input a desired value
var = readline();
 
# convert the inputted value to integer
var = as.integer(var);
 
# print the value
print(var)


R
# R program to illustrate
# taking input from the user
 
# taking input with showing the message
var = readline(prompt = "Enter any number : ");
 
# convert the inputted value to an integer
var = as.integer(var);
 
# print the value
print(var)


R
# R program to illustrate
# taking input from the user
 
# taking multiple inputs
# using braces
{
    var1 = readline("Enter 1st number : ");
    var2 = readline("Enter 2nd number : ");
    var3 = readline("Enter 3rd number : ");
    var4 = readline("Enter 4th number : ");
}
 
# converting each value
var1 = as.integer(var1);
var2 = as.integer(var2);
var3 = as.integer(var3);
var4 = as.integer(var4);
 
# print the sum of the 4 number
print(var1 + var2 + var3 + var4)


R
# R program to illustrate
# taking input from the user
 
# string input
var1 = readline(prompt = "Enter your name : ");
 
# character input
var2 = readline(prompt = "Enter any character : ");
# convert to character
var2 = as.character(var2)
 
# printing values
print(var1)
print(var2)


R
# R program to illustrate
# taking input from the user
 
# taking input using scan()
x = scan()
# print the inputted values
print(x)


R
# R program to illustrate
# taking input from the user
 
# double input using scan()
d = scan(what = double())
 
# string input using 'scan()'
s = scan(what = " ")
 
# character input using 'scan()'
c = scan(what = character())
 
# print the inputted values
print(d) # double
print(s) # string
print(c) # character


R
# R program to illustrate
# taking input from the user
 
# string file input using scan()
s = scan("fileString.txt", what = " ")
 
# double file input using scan()
d = scan("fileDouble.txt", what = double())
 
# character file input using scan()
c = scan("fileChar.txt", what = character())
 
# print the inputted values
print(s) # string
print(d) # double
print(c) # character


输出:

255
[1] 255

还可以在控制台窗口中显示消息,告诉用户在程序中输入什么。为此,必须在readline()函数中使用一个名为prompt的参数。实际上,提示参数有助于其他功能来构建文件记录。但是提示并不是一直都必须使用的。

例子:

R

# R program to illustrate
# taking input from the user
 
# taking input with showing the message
var = readline(prompt = "Enter any number : ");
 
# convert the inputted value to an integer
var = as.integer(var);
 
# print the value
print(var)

输出:

Enter any number : 255
[1] 255

在 R 中接受多个输入

R语言中的多输入与单输入相同,只需要为输入定义多个readline() 。可以使用大括号在其中定义多个readline()

例子:

R

# R program to illustrate
# taking input from the user
 
# taking multiple inputs
# using braces
{
    var1 = readline("Enter 1st number : ");
    var2 = readline("Enter 2nd number : ");
    var3 = readline("Enter 3rd number : ");
    var4 = readline("Enter 4th number : ");
}
 
# converting each value
var1 = as.integer(var1);
var2 = as.integer(var2);
var3 = as.integer(var3);
var4 = as.integer(var4);
 
# print the sum of the 4 number
print(var1 + var2 + var3 + var4)

输出:

Enter 1st number : 12
Enter 2nd number : 13
Enter 3rd number : 14
Enter 4th number : 15
[1] 54

在 R 中获取字符串和字符输入

取字符串输入与整数相同。对于“字符串”,不需要将输入的数据转换为字符串,因为 R 始终将输入作为字符串。而对于“字符”,则需要转换为“字符”。有时它可能不会导致任何错误。也可以像字符串一样输入字符,但输入的数据是整个程序的字符串类型。因此,将输入的数据用作“字符”的最佳方法是将数据转换为字符。

例子:

R

# R program to illustrate
# taking input from the user
 
# string input
var1 = readline(prompt = "Enter your name : ");
 
# character input
var2 = readline(prompt = "Enter any character : ");
# convert to character
var2 = as.character(var2)
 
# printing values
print(var1)
print(var2)

输出:

Enter your name : GeeksforGeeks
Enter any character : G
[1] "GeeksforGeeks"
[1] "G"

使用 scan() 方法

在 R 语言中获取用户输入的另一种方法是使用一种称为scan()方法的方法。此方法从控制台获取输入。这种方法是一种非常方便的方法,因为任何数学计算或任何数据集都需要快速获取输入。该方法以向量或列表的形式读取数据。此方法也用于从文件中读取输入。

例子:
这是使用scan()方法获取输入的简单方法,其中一些整数作为输入并在控制台的下一行打印这些值。

R

# R program to illustrate
# taking input from the user
 
# taking input using scan()
x = scan()
# print the inputted values
print(x)

输出:

1: 1 2 3 4 5 6
7: 7 8 9 4 5 6
13: 
Read 12 items
 [1] 1 2 3 4 5 6 7 8 9 4 5 6

说明
当控件转到第 3 行时,总共 12 个整数在 2 行中作为输入,然后按Enter键 2 次将终止输入过程。

使用 scan() 方法获取双精度、字符串、字符类型值

要采用双精度、字符串、字符类型输入,请在scan()方法中指定输入值的类型。为此,有一个名为what的参数,通过它可以指定输入值的数据类型。

例子:

R

# R program to illustrate
# taking input from the user
 
# double input using scan()
d = scan(what = double())
 
# string input using 'scan()'
s = scan(what = " ")
 
# character input using 'scan()'
c = scan(what = character())
 
# print the inputted values
print(d) # double
print(s) # string
print(c) # character

输出:

1: 123.321 523.458 632.147
4: 741.25 855.36
6: 
Read 5 items
1: geeksfor geeks gfg
4: c++ R java python
8: 
Read 7 items
1: g e e k s f o
8: r g e e k s
14: 
Read 13 items
[1] 123.321 523.458 632.147 741.250 855.360
[1] "geeksfor" "geeks"    "gfg"      "c++"      "R"        "java"     "python"  
[1] "g" "e" "e" "k" "s" "f" "o" "r" "g" "e" "e" "k" "s"

说明
这里,双项数为 5,排序项数为 7,字符项数为 13。

使用 scan() 方法读取文件数据

使用scan()方法读取文件与普通控制台输入相同,唯一不同的是,需要将文件名和数据类型传递给scan()方法。

例子:

R

# R program to illustrate
# taking input from the user
 
# string file input using scan()
s = scan("fileString.txt", what = " ")
 
# double file input using scan()
d = scan("fileDouble.txt", what = double())
 
# character file input using scan()
c = scan("fileChar.txt", what = character())
 
# print the inputted values
print(s) # string
print(d) # double
print(c) # character

输出:

Read 7 items
Read 5 items
Read 13 items
[1] "geek"   "for"    "geeks"  "gfg"    "c++"    "java"   "python"
[1] 123.321 523.458 632.147 741.250 855.360
[1] "g" "e" "e" "k" "s" "f" "o" "r" "g" "e" "e" "k" "s"

将数据文件保存在保存程序的相同位置以便更好地访问。否则文件的总路径需要在scan()方法中定义。