如何将所有控制台输出保存到R中的文件?

如何将所有控制台输出保存到R中的文件?

我想将所有控制台文本重定向到文件。这是我尝试过的:


> 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中得到的:


[1] "a"

这是我想要的test.log:


> a <- "a"

> a

[1] "a"

> How come I do not see this in log

Error: unexpected symbol in "How come"

我究竟做错了什么?谢谢!


一只名叫tom的猫
浏览 1649回答 4
4回答

斯蒂芬大帝

您必须分别接收“输出”和“消息”(该sink功能仅查看的第一个元素type)现在,如果您也想记录输入内容,则将其放在脚本中:脚本1:5 + 1:3&nbsp; &nbsp;# prints and gives a warningstop("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 consolesink()&nbsp;sink(type="message")# And look at the log...cat(readLines("test.log"), sep="\n")

烙印99

如果可以访问命令行,则可能更喜欢使用R CMD BATCH从命令行运行脚本。==脚本内容的开始R ==a <- "a"aHow come I do not see this in log==脚本的最终内容R ==在命令提示符下(许多un * x变量中为“ $”,Windows中为“ C:>”),运行$ R CMD BATCH script.R &尾随的“&”是可选的,并在后台运行命令。日志文件的默认名称在扩展名后附加了“ out”,即script.Rout==开始脚本内容.Rout ==R version 3.1.0 (2014-04-10) -- "Spring Dance"Copyright (C) 2014 The R Foundation for Statistical ComputingPlatform: i686-pc-linux-gnu (32-bit)R is free software and comes with ABSOLUTELY NO WARRANTY.You are welcome to redistribute it under certain conditions.Type 'license()' or 'licence()' for distribution details.&nbsp; Natural language support but running in an English localeR is a collaborative project with many contributors.Type 'contributors()' for more information and'citation()' on how to cite R or R packages in publications.Type 'demo()' for some demos, 'help()' for on-line help, or'help.start()' for an HTML browser interface to help.Type 'q()' to quit R.[Previously saved workspace restored]> a <- "a"> a[1] "a"> How come I do not see this in logError: unexpected symbol in "How come"Execution halted==脚本的最终内容。Rout==

炎炎设计

你不能&nbsp;最多可以单独保存输出sink和输入savehistory。或使用script,screen或这类外部工具tmux。
打开App,查看更多内容
随时随地看视频慕课网APP