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

查看:53
本文介绍了如何将所有控制台输出保存到 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"

我做错了什么?谢谢!

推荐答案

你必须分别下沉输出"和消息"(sink 函数只看第一type)

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

现在,如果您希望 input 也被记录,请将其放入脚本中:

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="
")

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

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