R - 控制台输出重定向不能(可靠地)从函数调用中工作

时间:2015-01-11 19:39:53

标签: r buffer output sink

我编写了以下代码以将控制台输出重定向到文本文件。所有三个命令'当我以交互方式运行代码时,(dim,str,summary)输出显示在文本文件中。但是,当我将代码放在函数中并以交互方式运行函数调用时,只会出现str命令outuput。这可能是缓冲问题。有什么建议吗?

操作系统:OS X 10.9.5(Mavericks); R 3.1.1 GUI 1.65 Mavericks build(6784)


此代码有效......

con <- file("FileInfoLog.txt")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")

writeLines("\n\n\n===============================================================\n")
writeLines("Dimensions are ")
dim(db)
writeLines("\n\n\n===============================================================\n")
writeLines("Structure is ")
str(db)
writeLines("\n\n\n===============================================================\n")
writeLines("Summary is ")
summary(db)

# Restore output to console
sink(type="message")
sink() 

此代码无法可靠地工作......只有str()输出出现在文本文件中。

getFileInfo <- function(db) {

    con <- file("FileInfoLog.txt")
    sink(con, append=TRUE)
    sink(con, append=TRUE, type="message")

    writeLines("\n\n\n===============================================================\n")
    writeLines("Dimensions are ")
    dim(db)
    writeLines("\n\n\n===============================================================\n")
    writeLines("Structure is ")
    str(db)
    writeLines("\n\n\n===============================================================\n")
    writeLines("Summary is ")
    summary(db)

    # Restore output to console
    sink(type="message")
    sink() 

}

FileInfoLog.txt来自有效的代码...

=============================================== ================

尺寸是 [1] 28947 17

=============================================== ================

结构是 &#39; data.frame&#39;:28947 obs。 17个变量:  $ store:int 2 2 2 2 2 2 2 2 2 2 ...  $品牌:因子w / 3级&#34; dominicks&#34;,&#34; minute.maid&#34;,..:3 3 3 3 3 3 3 3 3 3 ...  $ week:int 40 46 47 48 50 51 52 53 54 57 ...  $ logmove:num 9.02 8.72 8.25 8.99 9.09 ... [...]

=============================================== ================

摘要是      商店品牌周logmove专业价格AGE60
 闵。 :2.00多米尼克斯:9649分钟。 :40.0分钟:4.159分钟:0.0000分钟:0.520分钟:0.05805
 第1期:53.00分钟。女佣:9649第1期:70.0第1期:8.490第1期:0.0000第1期:1.790第1期:0.12210  中位数:86.00 tropicana:9649平均:101.0平均:9.034平均:0.0000平均:2.170平均:0.17065
 平均值:80.88平均值:100.5平均值:9.168平均值:0.2373平均值:2.282平均值:0.17313
 3 Qu.:111.00 3rd Qu.:130.0 3rd Qu。:9.765 3rd Qu.:0.0000 3rd Qu.:2.730 3rd Qu.:0.21395
 最大。 :137.00最大。 :最大160.0 :13.482 Max。 :1.0000最大。 :最大3.870 :0.30740
[...]


来自无法可靠工作的代码的FileInfoLog.txt ...

=============================================== ================

尺寸

=============================================== ================

结构是 &#39; data.frame&#39;:28947 obs。 17个变量:  $ store:int 2 2 2 2 2 2 2 2 2 2 ...  $品牌:因子w / 3级&#34; dominicks&#34;,&#34; minute.maid&#34;,..:3 3 3 3 3 3 3 3 3 3 ...  $ week:int 40 46 47 48 50 51 52 53 54 57 ...  $ logmove:num 9.02 8.72 8.25 8.99 9.09 ... [...]

=============================================== ================

摘要是

1 个答案:

答案 0 :(得分:0)

以交互方式运行时,会在每个结果上隐式调用print()命令。在交互模式下,情况并非如此。您必须明确调用print()。所以你需要print(dim(db))

str()函数不同,因为它通过cat()创建了它的输出,并且实际上并没有返回任何内容。

如果您使用source()功能运行脚本,则可以设置echo=TRUE以保持自动打印行为。

此外,不要费心自己设置sink()capture.out()命令。您可以考虑查看knitr以使用R代码制作漂亮的报告。

相关问题