如何将所有控制台输出保存到R中的文件? [英] How to save all console output to file in R?

查看:2900
本文介绍了如何将所有控制台输出保存到R中的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将所有控制台文字重新导向至档案。这里是我尝试:

I want to redirect all console text to a file. Here is what I tried:

> sink("test.log", type=c("output", "message"))
> a <- "a"
> a
> How come I do not see this in log
Error: unexpected symbol in "How come"

这是我在test.log中得到的:

Here is what I got in test.log:

[1] "a"

这里是我想要的test.log:

Here is what I want in test.log:

> a <- "a"
> a
[1] "a"
> How come I do not see this in log
Error: unexpected symbol in "How come"

我做错了什么?谢谢!

推荐答案

你必须分别接收输出和消息( code>函数只查看类型的第一元素

You have to sink "output" and "message" separately (the sink function only looks at the first element of type)

现在,如果您还希望将输入也记录在脚本中:

Now if you want the input to be logged too, then put it in a script:

script.R

1:5 + 1:3   # prints and gives a warning
stop("foo") # an error

并根据提示:

con <- file("test.log")
sink(con, append=TRUE)
sink(con, append=TRUE, type="message")

# This will echo all input and not truncate 150+ character lines...
source("script.R", echo=TRUE, max.deparse.length=10000)

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

# And look at the log...
cat(readLines("test.log"), sep="\n")

这篇关于如何将所有控制台输出保存到R中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆