2

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

操作系统:OS X 10.9.5(小牛);R 3.1.1 GUI 1.65 Mavericks 构建 (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

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

结构是'data.frame':28947 obs。17 个变量: $ store : int 2 2 2 2 2 2 2 2 2 2 ... $ brand : Factor w/ 3 个级别 "dominicks","minute.maid",..: 3 3 3 3 3 3 3 3 3 3 ... $ 周: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
第一排: 53.00 分钟女仆:9649 第一排: 70.0 第一排: 8.490 第一排:0.0000 第一排:1.790 第一排:0.12210
中位数: 86.00 热带:9649 中位数:101.0 中位数: 9.034 :0.0000 中位数 :2.170 中位数 :0.17065
平均值 :80.88 平均值 :100.5 平均值 :9.168 平均值 :0.2373 平均值 :2.282 平均值 :0.17313
3rd Qu.:111.00 3rd Qu.:130.0 3rd Qu.:000.0 3rd Qu.0 2.730 第三曲:0.21395
最大限度。:137.00 最大。:160.0 最大。:13.482 最大。:1.0000 最大。:3.870 最大。:0.30740
[...]


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

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

尺寸是

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

结构是'data.frame':28947 obs。17 个变量: $ store : int 2 2 2 2 2 2 2 2 2 2 ... $ brand : Factor w/ 3 个级别 "dominicks","minute.maid",..: 3 3 3 3 3 3 3 3 3 3 ... $ 周:int 40 46 47 48 50 51 52 53 54 57 ... $ logmove:num 9.02 8.72 8.25 8.99 9.09 ... [...]

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

总结是

4

1 回答 1

0

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

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

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

此外,而不是为自己设置sink()capture.out()命令而烦恼。您可能会考虑查看knitr以使用 R 代码制作漂亮的报告。

于 2015-01-11T22:50:34.180 回答