fileConn<-file("output.txt")
writeLines(c("Hello","World"), fileConn)
close(fileConn)
实际上,您可以使用 sink()
做到这一点:
sink("outfile.txt")
cat("hello")
cat("\n")
cat("world")
sink()
因此这样做:
file.show("outfile.txt")
# hello
# world
我将使用 cat()
命令,如本例所示:
> cat("Hello",file="outfile.txt",sep="\n")
> cat("World",file="outfile.txt",append=TRUE)
然后,您可以使用 R 查看结果
> file.show("outfile.txt")
hello
world
一个简单的 writeLines()
是什么?
txt <- "Hallo\nWorld"
writeLines(txt, "outfile.txt")
或者
txt <- c("Hallo", "World")
writeLines(txt, "outfile.txt")
"Hallo\nWorld"
是字符类型的长度为一的向量。试试txt <- "Hallo\nWorld"; is.character(txt) && length(txt) == 1 && is.vector(txt)
writeLines(txt, con="outfile.txt")
。
> coefficients<-summary(model) > writeLines(coefficients, "coefficients") Error in writeLines(coefficients, "coefficients") : invalid 'text' argument
你可以在一个语句中做到这一点
cat("hello","world",file="output.txt",sep="\n",append=TRUE)
我建议:
writeLines(c("Hello","World"), "output.txt")
它比当前接受的答案更短更直接。没有必要这样做:
fileConn<-file("output.txt")
# writeLines command using fileConn connection
close(fileConn)
因为 writeLines()
的文档说:
如果 con 是字符串,则函数调用 file 以获得在函数调用期间打开的文件连接。
# default settings for writeLines(): sep = "\n", useBytes = FALSE
# so: sep = "" would join all together e.g.
将文本行写入 R 中的文件的短方法可以使用 cat 或 writeLines 来实现,如许多答案中所示。一些最短的可能性可能是:
cat("Hello\nWorld", file="output.txt")
writeLines("Hello\nWorld", "output.txt")
如果您不喜欢“\n”,您也可以使用以下样式:
cat("Hello
World", file="output.txt")
writeLines("Hello
World", "output.txt")
虽然 writeLines
在文件末尾添加了 换行符,但 cat
的情况并非如此。可以通过以下方式调整此行为:
writeLines("Hello\nWorld", "output.txt", sep="") #No newline at end of file
cat("Hello\nWorld\n", file="output.txt") #Newline at end of file
cat("Hello\nWorld", file="output.txt", sep="\n") #Newline at end of file
但主要区别在于 cat
使用 R 对象 和 writeLines
字符向量 作为参数。所以写出例如数字 1:10 需要为 writeLines 强制转换,而它可以像在 cat 中一样使用:
cat(1:10)
writeLines(as.character(1:10))
cat
可以采用 许多 个对象,但 writeLines
只能采用 一个 向量:
cat("Hello", "World", sep="\n")
writeLines(c("Hello", "World"))
tidyverse 版本,带有管道和来自 readr 的 write_lines()
library(tidyverse)
c('Hello', 'World') %>% write_lines( "output.txt")
一个简单的 write.table()
怎么样?
text = c("Hello", "World")
write.table(text, file = "output.txt", col.names = F, row.names = F, quote = F)
参数 col.names = FALSE
和 row.names = FALSE
确保排除 txt 中的行名和列名,参数 quote = FALSE
排除 txt 中每行开头和结尾的引号。要重新读入数据,您可以使用 text = readLines("output.txt")
。
为了完善可能性,您可以将 writeLines()
与 sink()
结合使用,如果您愿意:
> sink("tempsink", type="output")
> writeLines("Hello\nWorld")
> sink()
> file.show("tempsink", delete.file=TRUE)
Hello
World
对我来说,使用 print()
似乎总是最直观的,但如果你这样做,输出将不是你想要的:
...
> print("Hello\nWorld")
...
[1] "Hello\nWorld"
基于 the best answer:
file <- file("test.txt")
writeLines(yourObject, file)
close(file)
请注意,yourObject
需要采用字符串格式;如果需要,使用 as.character()
进行转换。
但这对于每次保存尝试都输入过多。让我们在 RStudio 中创建一个片段。
在全局选项>>代码>>片段中,输入:
snippet wfile
file <- file(${1:filename})
writeLines(${2:yourObject}, file)
close(file)
然后,在编码期间,键入 wfile
并按 Tab。
file <- file(...)
行在我看来很可疑。不是将 file
作为函数调用并赋予 file
新的含义吗?即使在这段代码运行之后,file()
是否仍然有效?现在无法访问 R 安装来测试自己...
丑陋的系统选项
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
在较新版本的 R 中,writeLines
将保留文本中的回车符和空格,因此您无需在行尾包含 \n
,您可以将一大段文本写入文件。这将适用于示例,
txt <- "Hello
World"
fileConn<-file("output.txt")
writeLines(txt, fileConn)
close(fileConn)
但是您也可以使用此设置来简单地包含具有结构的文本(换行符或缩进)
txt <- "Hello
world
I can
indent text!"
fileConn<-file("output.txt")
writeLines(txt, fileConn)
close(fileConn)
不定期副业成功案例分享
writeLines()
的选项比sink()
和cat()
的组合快大约十倍