📜  打印 R 程序的输出

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

打印 R 程序的输出

在 R 中有多种方法可以打印输出。在 R 程序中打印输出最常用的方法是使用一个名为print()的函数。此外,如果 R 的程序是在控制台上逐行编写的,则输出将正常打印,无需使用任何函数来打印该输出。为此,只需选择输出变量并按下运行按钮。例子:

R
# select 'x' and then press 'run' button
# it will print 'GeeksforGeeks' on the console
x <- "GeeksforGeeks"
x


R
# R program to illustrate
# printing output of an R program
 
# print string
print("GFG")
 
# print variable
# it will print 'GeeksforGeeks' on the console
x <- "GeeksforGeeks"
print(x)


R
# R program to illustrate
# printing output of an R program
 
x <- "GeeksforGeeks"
 
# using paste inside print()
print(paste(x, "is best (paste inside print())"))
 
# using paste0 inside print()
print(paste0(x, "is best (paste0 inside print())"))


R
# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks" # string
x1 = 255            # integer
x2 = 23.14          # float
 
# string print
sprintf("%s is best", x)
 
# integer print
sprintf("%d is integer", x1)
 
# float print
sprintf("%f is float", x2)


R
# R program to illustrate
# printing output of an R program
 
# print string with variable
# "\n" for new line
x = "GeeksforGeeks"
cat(x, "is best\n")
 
# print normal string
cat("This is R language")


R
# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks"
# print string with variable
message(x, "is best")
 
# print normal string
message("This is R language")


R
# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks"
# write variable
write.table(x, file = "my_data1.txt")
 
# write normal string
write.table("GFG is best", file = "my_data2.txt")


输出:

[1] "GeeksforGeeks"

R程序的输出

使用print()函数打印输出

使用print()函数打印输出是 R 中最常用的方法。这种方法的实现非常简单。

例子:

R

# R program to illustrate
# printing output of an R program
 
# print string
print("GFG")
 
# print variable
# it will print 'GeeksforGeeks' on the console
x <- "GeeksforGeeks"
print(x)

输出:

[1] "GFG"
[1] "GeeksforGeeks"

print()函数中使用paste()函数打印输出

R 提供了一种paste()方法来将字符串和变量一起打印输出。此方法在print()函数内部定义。 paste()将其参数转换字符串。也可以使用paste0()方法。

例子:

R

# R program to illustrate
# printing output of an R program
 
x <- "GeeksforGeeks"
 
# using paste inside print()
print(paste(x, "is best (paste inside print())"))
 
# using paste0 inside print()
print(paste0(x, "is best (paste0 inside print())"))

输出:

[1] "GeeksforGeeks is best (paste inside print())"
[1] "GeeksforGeeksis best (paste0 inside print())"

使用sprintf()函数打印输出

sprintf()基本上是一个C 库函数。此函数用于将字符串打印为C 语言。这是一个包装函数,可以像C 语言一样将值和字符串一起打印。此函数返回一个字符向量,其中包含要打印的字符串和变量的格式化组合。

例子:

R

# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks" # string
x1 = 255            # integer
x2 = 23.14          # float
 
# string print
sprintf("%s is best", x)
 
# integer print
sprintf("%d is integer", x1)
 
# float print
sprintf("%f is float", x2)

输出:

> sprintf("%s is best", x)
[1] "GeeksforGeeks is best"
> sprintf("%d is integer", x1)
[1] "255 is integer"
> sprintf("%f is float", x2)
[1] "23.140000 is float"

使用cat()函数打印输出

在 R 中打印输出的另一种方法是使用 cat()函数。它与print()函数相同。 cat()将其参数转换字符串。这对于在用户定义的函数中打印输出很有用。

例子:

R

# R program to illustrate
# printing output of an R program
 
# print string with variable
# "\n" for new line
x = "GeeksforGeeks"
cat(x, "is best\n")
 
# print normal string
cat("This is R language")

输出:

GeeksforGeeks is best
This is R language

使用message()函数打印输出

使用message()函数在 R 中打印内容的另一种方法。这不是用于打印输出,而是用于显示简单的诊断消息,这些消息在程序中没有警告或错误。但它可以用于打印输出的正常用途。

例子:

R

# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks"
# print string with variable
message(x, "is best")
 
# print normal string
message("This is R language")

输出:

GeeksforGeeks is best
This is R language 

将输出写入文件

要打印或写入具有变量值的文件,有一个名为write()的函数。该函数使用名为table的选项来写入文件。

例子:

R

# R program to illustrate
# printing output of an R program
 
x = "GeeksforGeeks"
# write variable
write.table(x, file = "my_data1.txt")
 
# write normal string
write.table("GFG is best", file = "my_data2.txt")

输出: R程序的输出