简体   繁体   English

R-自动创建每日文件以记录来自RStudio控制台的所有内容

[英]R - Automatically create daily file to log everything from RStudio console

I typically work in one script.R file for each project (which each last a few weeks). 我通常为每个项目使用一个script.R文件(每个文件持续数周)。 I'd like to set up a process such that every day when I open RStudio I can start a file that stores everything I see in the console (input, output, warnings) to a file. 我想设置一个过程,以便每天打开RStudio时都可以启动一个文件,该文件将控制台中看到的所有内容(输入,输出,警告)存储到文件中。 I'd also like it to create a new file every single day. 我也希望它每天创建一个新文件。 I've read around here and there is some talk about how to do parts of this but I'm not smart enough to bring it all together by myself. 我在这里读过书,并且有一些关于如何做部分内容的讨论,但是我不够聪明,无法独自将它们整合在一起。 Here's what I have so far: 这是我到目前为止的内容:

sink(paste("filename.txt", strtrim(Sys.time(),10)), append=T, split=T)
x <- 1:5
y <- 2:6
z <- c(5, 8, 3, 5, 9)
reg <- lm(y ~ x)  #intentional "mistake" used to produce a warning
reg <- lm(z ~ x)
summary(reg)
sink()

What I think this accomplishes: I get a log file which captures all output in a new file if it's the first time I've done it that, but will add to the existing file if I do some work in the AM, close it all out, and start anew later in the day. 我认为这完成了什么:我得到一个日志文件,如果这是我第一次这样做,它将捕获所有输出到一个新文件中,但是如果我在AM中做一些工作,它将添加到现有文件中,将其全部关闭出去,然后在当天晚些时候重新开始。

What this doesn't accomplish: it doesn't capture input or warning messages, and I think it's a bit clunky. 这不能完成什么:它不能捕获输入或警告消息,我认为这有点笨拙。

Am I right that this accomplishes what I think it does? 我是否可以完成我认为的功能? Is there a way to modify this (or do something similar) which will allow me to also capture input and warnings (basically the rest of what is seen in the console)? 是否有一种方法可以对此进行修改(或执行类似操作),使我也可以捕获输入和警告(基本上是在控制台中看到的其余内容)?

Thanks! 谢谢!

Up front: this answer does not provide both inputs and messages. 预先:此答案未同时提供输入和消息。 A more robust mechanism for you would be to use a R-markdown document : it will capture warnings and such, it will continue on errors, it does include inputs, etc. Just use RStudio's "knit document" (or even notebooks), and you'll get the same effect. 对于您来说,更可靠的机制是使用R-markdown文档 :它将捕获警告,并且这样,它将继续出现错误,包括输入内容,等等。只需使用RStudio的“编织文档”(甚至笔记本),然后您将获得相同的效果。 If that doesn't satisfy you and you want to stick with sink , read on. 如果那不满足于您,并且您想坚持使用接收sink ,请继续阅读。

There is an argument to sink that allows the saving of warnings and errors: sink有一个参数,它可以保存警告和错误:

sink(file = NULL, append = FALSE, type = c("output", "message"),
     split = FALSE)
...
type: character string.  Either the output stream or the messages
      stream.  The name will be partially matched so can be
      abbreviated.

You cannot sink both "output" and "messages" at the same time, so you need to call sink twice, one for each type. 你不能在同一时间都汇“输出”和“消息”,所以你需要调用sink两次,每一个类型。 (They are handled completely separately, so you'll need close them individually as well.) (它们是完全分开处理的,因此您也需要单独关闭它们。)

CAVEAT EMPTOR: the help page warns against sinking messages: CAVEAT EMPTOR: 帮助页面警告不要下沉消息:

 Sink-ing the messages stream should be done only with great care.
 For that stream 'file' must be an already open connection, and
 there is no stack of connections.

Furthermore, it does not support split , so you will not see any warnings or errors. 此外,它不支持split ,因此您将不会看到任何警告或错误。 (That's a significant-enough issue to discourage me from using it for this ... I'd just highlight the console in its entirety and save in a text editor. But I understand you are trying to automate things, so we'll continue.) (这是一个很重要的问题,不鼓励我将其用于此...我只是要完整地突出显示控制台并保存在文本编辑器中。但是我知道您正在尝试使事情自动化,因此我们将继续。)

There are two ways to do it: safely (losing synchronization between output and messages), and not-well-tested-likely-unsafely. 有两种方法可以做到:安全(失去输出和消息之间的同步),以及未经充分测试(可能不安全)的方法。

Safely 安然

msgcon <- file("out1-msg.txt", open = "a")
sink("out1.txt", type = "output", append = TRUE, split = TRUE)
sink(msgcon, type = "message") # does not support split
# do your work here
a <- 1
a
stop("huh?")
sink(NULL, type = "message")
sink(NULL, type = "output")

The good is that you'll get both types of messages, separately and safely. 这样做的好处是,您将分别和安全地收到两种消息。 The bad is that you won't be able to tie a specific error/warning to anywhere in the code or output. 不利的是,您将无法将特定的错误/警告绑定到代码或输出中的任何位置。 If that doesn't bother you, stick with this. 如果那不打扰您,请坚持使用。

Not-well-tested-likely-Unsafely 未经过严格测试,可能-不安全

The problem with this approach is that two "processes" are writing to the same file, potentially simultaneously. 这种方法的问题在于,两个“进程”可能同时写入同一文件。 This could cause data loss or (more likely) jumbled/interleaved outputs. 这可能会导致数据丢失或(更可能是)混乱/交错的输出。 I have not mucked around in the code enough to see if this will happen, nor have I tested it exhaustively. 我没有在代码中四处摸索,以查看是否会发生这种情况,也没有进行详尽的测试。 You've been warned. 您已被警告。

con <- file("out1.txt", open = "a") # use for both sinks
sink(con, type = "output", append = TRUE, split = TRUE)
sink(con, type = "message")
a <- 2
a
stop("uh-wha?")
sink(NULL, type = "message")
sink(NULL, type = "output")

You still need to open and close both type s independently. 您仍然需要分别打开和关闭这两个type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM