In the R scripting language, how do I write lines of text, e.g. the following two lines
在R脚本语言中,如何编写文本行,例如以下两行。
Hello
World
to a file named "output.txt"?
对于一个名为“output.txt”的文件?
9 个解决方案
#1
304
fileConn<-file("output.txt")
writeLines(c("Hello","World"), fileConn)
close(fileConn)
#2
123
Actually you can do it with sink()
:
实际上你可以用sink()来做:
sink("outfile.txt")
cat("hello")
cat("\n")
cat("world")
sink()
hence do:
因此做:
file.show("outfile.txt")
# hello
# world
#3
90
I would use the cat()
command as in this example:
在本例中,我将使用cat()命令:
> cat("Hello",file="outfile.txt",sep="\n")
> cat("World",file="outfile.txt",append=TRUE)
You can then view the results from with R with
然后你可以用R来查看结果。
> file.show("outfile.txt")
hello
world
#4
35
What's about a simple writeLines()
?
一个简单的writeLines()是什么?
txt <- "Hallo\nWorld"
writeLines(txt, "outfile.txt")
or
或
txt <- c("Hallo", "World")
writeLines(txt, "outfile.txt")
#5
17
1.Using file argument in cat
.
1。使用cat中的文件参数。
cat("Hello World", file="filename")
2.Use sink
function to redirect all output from both print and cat to file.
2。使用sink函数将打印和cat的所有输出重定向到文件。
sink("filename") # Begin writing output to file
print("Hello World")
sink() # Resume writing output to console
NOTE: The print function cannot redirect its output, but the sink function can force all output to a file.
注意:print函数不能重定向它的输出,但是sink函数可以强制所有输出到一个文件。
3.Making connection to a file and writing.
3所示。连接到文件和写入。
con <- file("filename", "w")
cat("Hello World", file=con)
close(con)
#6
9
You could do that in a single statement
你可以在一个单独的语句中这样做。
cat("hello","world",file="output.txt",sep="\n",append=TRUE)
#7
2
To round out the possibilities, you can use writeLines()
with sink()
, if you want:
为了解决这一问题,您可以使用带有sink()的writeLines(),如果您想:
> sink("tempsink", type="output")
> writeLines("Hello\nWorld")
> sink()
> file.show("tempsink", delete.file=TRUE)
Hello
World
To me, it always seems most intuitive to use print()
, but if you do that the output won't be what you want:
对我来说,使用print()似乎总是最直观的,但是如果你这样做,输出就不是你想要的:
...
> print("Hello\nWorld")
...
[1] "Hello\nWorld"
#8
1
Based on the best answer:
基于最佳答案:
file <- file("test.txt")
writeLines(yourObject, file)
close(file)
Note that the yourObject
needs to be in a string format; use as.character()
to convert if you need.
注意,yourObject需要以字符串格式;如果需要,可以使用as.character()来转换。
But this is too much typing for every save attempt. Let's create a snippet in RStudio.
但是这对于每次保存尝试来说都是太多的输入。让我们在RStudio中创建一个片段。
In Global Options >> Code >> Snippet, type this:
在Global Options >>代码>>片段中,输入如下:
snippet wfile
file <- file(${1:filename})
writeLines(${2:yourObject}, file)
close(file)
Then, during coding, type wfile
and press Tab.
然后,在编码过程中,键入wfile并按下Tab键。
#9
0
The ugly system option
ptf <- function (txtToPrint,outFile){system(paste(paste(paste("echo '",cat(txtToPrint),sep = "",collapse = NULL),"'>",sep = "",collapse = NULL),outFile))}
#Prints txtToPrint to outFile in cwd. #!/bin/bash echo txtToPrint > outFile
#1
304
fileConn<-file("output.txt")
writeLines(c("Hello","World"), fileConn)
close(fileConn)
#2
123
Actually you can do it with sink()
:
实际上你可以用sink()来做:
sink("outfile.txt")
cat("hello")
cat("\n")
cat("world")
sink()
hence do:
因此做:
file.show("outfile.txt")
# hello
# world
#3
90
I would use the cat()
command as in this example:
在本例中,我将使用cat()命令:
> cat("Hello",file="outfile.txt",sep="\n")
> cat("World",file="outfile.txt",append=TRUE)
You can then view the results from with R with
然后你可以用R来查看结果。
> file.show("outfile.txt")
hello
world
#4
35
What's about a simple writeLines()
?
一个简单的writeLines()是什么?
txt <- "Hallo\nWorld"
writeLines(txt, "outfile.txt")
or
或
txt <- c("Hallo", "World")
writeLines(txt, "outfile.txt")
#5
17
1.Using file argument in cat
.
1。使用cat中的文件参数。
cat("Hello World", file="filename")
2.Use sink
function to redirect all output from both print and cat to file.
2。使用sink函数将打印和cat的所有输出重定向到文件。
sink("filename") # Begin writing output to file
print("Hello World")
sink() # Resume writing output to console
NOTE: The print function cannot redirect its output, but the sink function can force all output to a file.
注意:print函数不能重定向它的输出,但是sink函数可以强制所有输出到一个文件。
3.Making connection to a file and writing.
3所示。连接到文件和写入。
con <- file("filename", "w")
cat("Hello World", file=con)
close(con)
#6
9
You could do that in a single statement
你可以在一个单独的语句中这样做。
cat("hello","world",file="output.txt",sep="\n",append=TRUE)
#7
2
To round out the possibilities, you can use writeLines()
with sink()
, if you want:
为了解决这一问题,您可以使用带有sink()的writeLines(),如果您想:
> sink("tempsink", type="output")
> writeLines("Hello\nWorld")
> sink()
> file.show("tempsink", delete.file=TRUE)
Hello
World
To me, it always seems most intuitive to use print()
, but if you do that the output won't be what you want:
对我来说,使用print()似乎总是最直观的,但是如果你这样做,输出就不是你想要的:
...
> print("Hello\nWorld")
...
[1] "Hello\nWorld"
#8
1
Based on the best answer:
基于最佳答案:
file <- file("test.txt")
writeLines(yourObject, file)
close(file)
Note that the yourObject
needs to be in a string format; use as.character()
to convert if you need.
注意,yourObject需要以字符串格式;如果需要,可以使用as.character()来转换。
But this is too much typing for every save attempt. Let's create a snippet in RStudio.
但是这对于每次保存尝试来说都是太多的输入。让我们在RStudio中创建一个片段。
In Global Options >> Code >> Snippet, type this:
在Global Options >>代码>>片段中,输入如下:
snippet wfile
file <- file(${1:filename})
writeLines(${2:yourObject}, file)
close(file)
Then, during coding, type wfile
and press Tab.
然后,在编码过程中,键入wfile并按下Tab键。
#9
0
The ugly system option
ptf <- function (txtToPrint,outFile){system(paste(paste(paste("echo '",cat(txtToPrint),sep = "",collapse = NULL),"'>",sep = "",collapse = NULL),outFile))}
#Prints txtToPrint to outFile in cwd. #!/bin/bash echo txtToPrint > outFile