【问题标题】:How to write the text R outputs from a function to a file?如何将文本R输出从函数写入文件?
【发布时间】:2018-01-08 18:21:46
【问题描述】:

我有一个函数custom_1,当我使用print(custom_1) 时,R 会打印返回的 data.frame 以及底部的一些文本,例如

用于模型的最终值是 mtry = 10 和 ntree = 250

我想要做的是打印 data.frame 和文本,但每次我尝试写入文件时,只会打印 dataframe。

这是我一直在使用的代码。

write.csv(print(custom_1), file="results2.csv")

【问题讨论】:

  • 如果您向reproducible example 提供一些示例数据和该输入所需的输出,这将更容易为您提供帮助。

标签: r function text output


【解决方案1】:

使用sink。在你的情况下:

sink("results2.csv") # open the connection
print(custom_1)
sink()               # close the connection

sink 函数获取通常会发送到控制台的输出并将其写入指定文件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-07-03
    • 2014-04-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-11
    • 1970-01-01
    相关资源
    最近更新 更多