在本教程中,您将熟悉基本的输入和输出函数。
在 R 语言中,通过以下两种方法从控制台获取用户输入。它们是:
readline() 函数:以字符串格式从用户获取输入。scan() 函数:从用户控制台或文件读取数据。readline() 函数以字符串格式从用户控制台获取或读取输入。
<variable_name> <- readline()
<variable_name> <- as.integer(<variable_name>)
readline() 以字符或字符串形式接收用户输入,并将其分配给任何已声明数据类型的变量。下一行 as. integer() 方法**将 readline() 方法读取的字符串数据类型转换为整数**类型或基于预定义变量数据类型的任何其他类型。然后将转换后的值分配给变量。
a <- readline()
a <- as.integer(a)
在给定的示例中,声明的变量为 a。readline() 方法读取输入,转换为整数数据类型,并将值存储到变量 a 中。
# Program to read input from user using readline()
# readline() reads input from user console
a = readline();
# convert the inputted value to integer
a = as.integer(a);
# print the value
print(a)
输出
5 [1] 5
将字符串数据类型变量转换为相应数据类型(如整数、浮点数、复数等)的一些函数在下表中给出,a 是一个变量。
| 转换类型 | 用途 |
|---|---|
| as.integer(a) | a 从字符串转换为整数 |
| s.numeric(a) | a 从字符串转换为数字类型。 |
| as.complex(a) | a 转换为复数 |
| as.Date(a) | a 转换为日期 |
注意:R 中没有字符串类型转换,因为 readline 以字符串格式从控制台获取输入或读取数据。
# Program to read input from user using readline()
# readline() reads input from user console as string
a = readline("Enter a value of floating type :") #4.3
b = readline("Enter a value of complex type :") #4+2i
# convert the inputted value to float ,complex
a = as.numeric(a)
b = as.complex(b)
# print the value
print(a)
print(b)
输出
Enter a value of floating type :4.5 Enter a value of complex type :4+2i [1] 4.5 [1] 4+2i
prompt() 函数是一个内置函数,可在控制台中显示文本消息,用户可以读取消息并执行相应操作。
<variable_name> <- readline(prompt = “ “)
<variable_name> <- as.integer(<variable_name>)
a <- readline(prompt = "Enter value of a : ")
a <- as.integer(a)
声明一个变量 **a**,然后使用 readline() 方法为其赋值。readline() 方法从用户控制台或屏幕读取数据。您可以看到突出显示的部分(**prompt = "Enter value of a : "**),prompt() 函数将引号内的给定字符串打印到用户控制台。用户可以看到消息 Enter value of a,然后用户将输入相应的数字。读取到 a 的数字是字符串类型,在下一行使用 **as.integer(a)** 转换为整数类型,并存储在 **a** 本身中。
类似地,在下面的程序中读取变量 b。执行加法运算,并使用 cat() 函数将结果输出到控制台。
a <- readline(prompt = "Enter value of a : ")
a <- as.integer(a)
b <- readline(prompt = "Enter value of b : ")
b<- as.integer(b)
add = a + b
cat("The sum after addition is ",add)
输出
Enter value of a : 6 Enter value of b : 6 The sum after addition is 12
在 R 程序中,scan() 函数是一个内置函数,用于从用户控制台读取数据或输入。
# R program to illustrate
# taking input from the user
# taking input using scan()
A = scan()
B = scan(what = " ")
c = scan(what = character())
# print the inputted values
print(A) #integer
print(B) # string
print(c) # character
输出
1: 4 5 6 7 5: 8 9 10 11 9: Read 8 items 1: hello welcome to Rlanguage 1: hello welcome to Rlanguage 5: enjoy learning 7: Read 6 items 1: Read 0 items [1] 4 5 6 7 8 9 10 11 [1] "hello" "welcome" "to" "Rlanguage" "enjoy" "learning" character(0)
R 程序支持内置函数,用于向用户控制台或屏幕显示输出。它们是:
在 R 中,print() 和 cat() 函数在向用户控制台显示内容方面功能几乎相同。也就是说,print() 和 cat() 作为提供相应输出的函数。
print(arguments) #print arguments to console
cat(any_text) #print text to console
language <- "R programming language" #created a string language
print(language) #Apply print to character string language
cat(language) #Apply cat to character string
输出
[1] "R programming language" R programming language
注意 :
• print() 函数将传递给函数的对象或字符向量返回到控制台。
• cat() 函数返回字符字符串或打印参数而不带引号。cat() 函数仅将内容(文本)显示在屏幕或控制台。