如何将文本R输出从函数写入文件?

时间:2018-01-08 18:21:47

标签: r function text output

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

  

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

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

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

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

1 个答案:

答案 0 :(得分:1)

使用sink。在你的情况下:

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

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

相关问题