Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R sink():错误,无法拆分消息连接_R_Logging_Sink - Fatal编程技术网

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

R sink():错误,无法拆分消息连接,r,logging,sink,R,Logging,Sink,我正在尝试将R脚本的错误和警告记录到外部文件中。同时,我希望能够在RStudio中看到控制台中的错误和警告(对开发和调试有用)。我正在尝试使用以下代码: logfile <- file("my_file_path", open="wt") sink(logfile, type="message", split = TRUE) 是否有解决办法或替代方案 谢谢所以,我试着在sink中使用split=T 但是,它没有做我们希望它做的事情。它要么将输出重定向到日志文件,要么抛出您指出的错误,而不

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

logfile <- file("my_file_path", open="wt")
sink(logfile, type="message", split = TRUE)
是否有解决办法或替代方案


谢谢

所以,我试着在
sink
中使用
split=T

但是,它没有做我们希望它做的事情。它要么将输出重定向到日志文件,要么抛出您指出的错误,而不是将错误或警告消息打印到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)
因此,上面的代码将错误和警告消息转移到日志文件中,并在控制台中打印出来


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

sink的第一个参数应该是与文件的连接。所以,应该是这样的
文件\u con你是对的。我没有很好地复制示例代码。但这个问题仍然有效。这是一个解决方法,但在运行所有脚本之前需要获取警告和错误时,它对调试没有多大用处。我不敢相信这个问题没有任何真正的替代方案……这是一个解决办法。我在答复中提到了这一点。如果您的脚本不需要花费很多时间来完成,这将非常有用。如果需要很长时间才能完成,最终你会得到想要的输出。是的,但奇怪的是,没有任何真正的替代方案来解决这个问题……这是真的。当你因为
split
这件事而出错时,这真的很烦人。
# 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 "