我需要在运行带有嵌套函数的代码时填充.txt文件。为此,我使用sink()
。输出由a)文本消息,b)数据帧行组成。我无法从嵌套函数内部打印数据框:
sink("log.txt")
cat("Some message") # Successfully prints to log.txt
head(some_df) # Successfully prints to log.txt
some_fun = function(x){
# ...
cat("Another message") # Successfully prints to log.txt
head(another_df) # Nothing gets printed to log.txt
# check that another_df is not empty:
cat(nrow(another_df)) # Successfully prints to log.txt (>0)
# ...
}
some_fun(x=0)
sink()
那么正确的方法是什么?