gpt4 book ai didi

R sink() : Error, 无法拆分消息连接

转载 作者:行者123 更新时间:2023-12-03 17:36:04 30 4
gpt4 key购买 nike

我正在尝试将 R 脚本的错误和警告记录到外部文件中。同时我希望能够在 RStudio 的控制台中看到错误和警告(对开发和调试有用)。我正在尝试使用以下代码:

logfile <- file("my_file_path", open="wt")
sink(logfile, type="message", split = TRUE)

但是当我尝试使用函数 sink() 拆分消息连接时,出现以下错误:
Error in sink(logfile, type = "message", split = TRUE) : 
cannot split the message connection

是否有任何解决方法或替代解决方案?

谢谢

最佳答案

所以,我尝试使用 split = Tsink .

但是,它没有做我们想要它做的事情。它要么将输出重定向到日志文件,要么抛出您指出的错误,而不是将错误或警告消息打印到 RStudio 控制台。

有一个解决您的问题的方法可能会解决您的问题。

我试过用这个:-

# path to your log file
file_path <- "path/documents/log/log.txt"

# open a connection to your log file
file_con <- file(file_path, open = "a")

## capture warning messages and errors to log file
sink(file_con, type = "message")

## to get error and warning message
sum(a)
warning("this is a warning message. please ignore")

## revert output back to the console and close the file connection
sink(type = "message")
close(file_con)

# get all the errors and warnings from log file
readLines(file_path)

它向控制台输出了一个输出:-
[1] "Error: object 'a' not found"              
[2] "Warning message:"
[3] "this is a warning message. please ignore "

因此,上面的代码段将错误和警告消息转移到日志文件并在控制台中打印出来。

您可以使用 sink正常然后使​​用 readLines将您的错误和警告消息打印到控制台。

关于R sink() : Error, 无法拆分消息连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48010243/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com