在R中写入文件后关闭文件

时间:2012-07-27 06:08:59

标签: r file

在其他语言中,将数据写入文件时,必须关闭该文件。 我在R中发现你在写入数据后不需要关闭文件,我是否正确? 如果我写的话会发生什么:

require(quantmod)  
getSymbols("GS")  
write(GS,'test')

1 个答案:

答案 0 :(得分:3)

您无需关闭该文件,因为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>