【问题标题】:R print object to file [duplicate]R将对象打印到文件[重复]
【发布时间】:2021-10-05 22:09:08
【问题描述】:

在 R 中,有没有办法将对象(例如线性模型的摘要)打印到 txt 文件? print() 可以很好地打印出对象,但我找不到重定向输出的方法。 另一方面 cat() 似乎无法处理列表,我得到了这个错误:

Error in cat(summary(lin_model), file = paste0(output_dir, "/linear_model_output.txt")) : 
  argument 1 (type 'list') cannot be handled by 'cat'

【问题讨论】:

  • 也许?capture.output 例如capture.output(summary(lm(1:10~1)), file="test.txt")

标签: r


【解决方案1】:

是的,您可以在 print 之前和之后使用 sink()。第一个调用包含输出文件的路径。

在你的情况下:

sink(file.path(output_dir, "linear_model_output.txt"))
print(summary(lin_model))
sink()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-07-01
    • 2016-12-19
    • 2023-03-27
    • 2021-03-19
    • 1970-01-01
    • 2013-10-28
    • 1970-01-01
    相关资源
    最近更新 更多