如何使R-Console的命令,结果和警告/消息/错误组合R-Session的对数文件



我想产生一个记录文件,以跟踪所有命令( stdin ),结果( stdout )和错误/警告/消息( stderr )。

我知道有很多记录包装,我尝试了几种像 TeachingDemos(似乎完全忽略了stderr)或 R2HTML(似乎忽略了消息),但是,它们似乎都不包含中的所有内容stderr

只有knitrmarkdown似乎能够将所有内容都包含到一个文件中。但是使用此解决方法,我必须编写R-Scripts,并且无法在控制台中自由地编写命令。
此外,我不能将knitrmarkdown命令包括在同一R-Script中(当然是一个次要的问题)。

这是一个示例

library(TeachingDemos)
library(R2HTML)    
library(TraMineR)
logdir <- "mylog.dir"
txtStart(file=paste(logdir,"test.txt", sep=""), commands=TRUE, 
         results=TRUE, append=FALSE)
HTMLStart(outdir = logdir, file = "test", echo=TRUE, HTMLframe=FALSE)
## Messages, warnings and errors
message("Print this message.")
warning("Beware.")
"a" + 1
geterrmessage()
## Some example application with the TraMiner package 
## which uses messages frequently
data(mvad)
mvad.seq <- seqdef(mvad[, 17:86])
mvad.ham <- seqdist(mvad.seq, method="HAM")
txtStop()
HTMLStop()

如果您是从unix/linux/mac/等运行的R。终端,您可以做:

R | tee mydir/mylog.txt

在Windows上,您可以在

中运行脚本
R CMD BATCH yourscript.R

您的结果将出现在与yousercript.out.out

的同一文件夹中

在Unices上,我经常使用以下代码习惯与bash:

Some Command 2>&1 | tee NameOfOutputFile.txt

" 2>&amp; 1"说要将STDERR重定向到STDOUT,然后将其管道输送到" Tee"。我将尝试使用这种记录R会话的方式和其他方法。

另一个Unix Trick是"脚本"命令,该命令启动了一个子壳,其I/O(基本上您输入的所有内容并看到回报)都记录到指定的文件。然后退出外壳以结束脚本。同样,进行实验。由于"接收器"是r的本地,我将首先尝试。

顺便说一句,我使用Solaris拿起了这些技巧,但是它们与Windows上的Unix模拟器一起使用相同的运行Cygwin。很久以前,我发现我的Cygwin图像比Solaris的机构安装更加最新,因为管理员所承担的责任要多得多,而不仅仅是让Solaris保持最新状态。请注意,机构机器更强大,因此,即使Cygwin更加方便,我的个人机器根本就无法满足需求。

下午:

我从Shumway的时间序列分析的第99页及其应用R示例进行了示例代码。这是测试文件调色板的内容:

r
plot(gnp)
acf2(gnp, 50)
gnpgr = diff(log(gnp)) # growth rate
plot(gnpgr)
acf2(gnpgr, 24)
sarima(gnpgr, 1, 0, 0) # AR(1)
sarima(gnpgr, 0, 0, 2) # MA(2)
ARMAtoMA(ar=.35, ma=0, 10) # prints psi-weights
quit("no")
exit

我使用:

调用它

脚本&lt;Palette.r

它从Palette.R和相应的输出中捕获命令。因此,似乎可用于批处理模式。对于交互式模式,我将使用我的原始计划并使用水槽。

我也有一个类似的问题,就我而言,我无法真正重定向 ast它取决于交互式模式的R运行。

具体来说,在我的日志文件中,我希望能够跟踪rjags::update()函数生成的进度条,但是,该函数需要交互式模式:

如果进度是"无",则抑制了进度条。或null,如果更新小于100迭代,或者如果不运行互动。

因此,我需要欺骗r以思考它正在交互性运行,而实际上它是从bash脚本(下面的Interactive_r.sh)运行的 there document ::

Interactive_r.sh

#!/bin/bash
R --interactive << EOT
# R code starts here
print(interactive())
quit("no")
# R code ends here
EOT

(旁注:确保避免使用R代码中的$字符,因为这将无法正确处理 - 例如,通过使用df[["X1"]]而不是df$X1data.frame()中检索一列。)

) 。

然后,您可以使用下面的bash命令来运行此脚本并将其内容发送到日志文件中:

$ ./interactive_R.sh > outputFile.log 2>&1

您的日志文件将如下显示:

outputfile.log

R version 4.0.2 (2020-06-22) -- "Taking Off Again"
Copyright (C) 2020 The R Foundation for Statistical Computing
Platform: x86_64-conda_cos6-linux-gnu (64-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.
  Natural language support but running in an English locale
R 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.
>
> # R code starts here
> print(interactive())
[1] TRUE
> quit("no")

相关内容

最新更新