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

标签 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, 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>

关于r - 在 R 中写入文件后关闭文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11682355/

相关文章:

r - knitr/jekyll/server::jekyll 图像很大或分辨率不高

r - 如何在ggplot2中添加纹理以填充颜色

r - 将不同长度的字符向量列表放入数据框中

r - 阻止 ggplot2 中的文本标签重叠

c - 在用 c 语言写入文件时,二进制模式和文本模式之间是否存在性能差异?

bash - 将文件追加到不带cat的bash文件中

r - 如何使用 lm() 修复此无效类型错误?

java - 最后一行仅在 Java 中部分打印

c# - 无法将数据写入传输连接 : An existing connection was forcibly closed by the remote host

Java - 以编程方式将文件附加到 Jar 文件