在R中写入文件后关闭文件
在其他语言中,将数据写入文件时,必须关闭该文件。 我发现在R中,您在将数据写入文件后不需要关闭该文件,对吗? 如果我写下:在R中写入文件后关闭文件,r,file,R,File,在其他语言中,将数据写入文件时,必须关闭该文件。 我发现在R中,您在将数据写入文件后不需要关闭该文件,对吗? 如果我写下: require(quantmod) getSymbols("GS") write(GS,'test') 您不需要关闭该文件,因为write()会为您关闭它: > write function (x, file = "data", ncolumns = if (is.character(x)) 1 else 5, append = FALSE, se
require(quantmod)
getSymbols("GS")
write(GS,'test')
您不需要关闭该文件,因为
write()
会为您关闭它:
> write
function (x, file = "data", ncolumns = if (is.character(x)) 1 else 5,
append = FALSE, sep = " ")
# Using cat() function
cat(x, file = file, sep = c(rep.int(sep, ncolumns - 1), "\n"),
append = append)
<bytecode: 0x053fdb10>
<environment: namespace:base>
> cat
function (..., file = "", sep = " ", fill = FALSE, labels = NULL,
append = FALSE)
{
if (is.character(file))
if (file == "")
file <- stdout()
else if (substring(file, 1L, 1L) == "|") {
file <- pipe(substring(file, 2L), "w")
# Closing here
on.exit(close(file))
}
else {
file <- file(file, ifelse(append, "a", "w"))
# Or here
on.exit(close(file))
}
.Internal(cat(list(...), file, sep, fill, labels, append))
}
<bytecode: 0x053fdd68>
<environment: namespace:base>
>编写
函数(x,file=“data”,ncolumns=if(is.character(x))1其他5,
append=FALSE,sep=“”)
#使用cat()函数
cat(x,file=file,sep=c)(rep.int(sep,ncolumns-1),“\n”),
追加=追加)
>猫
函数(…,file=“”,sep=“”,fill=FALSE,labels=NULL,
append=FALSE)
{
if(is.character(文件))
如果(文件==“”)
文件它取决于你如何写入文件。你能发布你正在使用的代码吗?让我们澄清一下,当写入或cat
的参数是一个字符时,这是一种行为,在这里它被解释为一个文件名(这里'test'
)。如果文件是通过文件连接打开的:filehandle@flodel我认为你的评论值得回答,因为正如你所指出的,情况要复杂得多。