【问题标题】:How to save all console output to file in R?如何将所有控制台输出保存到 R 中的文件?
【发布时间】:2011-10-29 03:30:52
【问题描述】:

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

> 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"

我做错了什么?谢谢!

【问题讨论】:

标签: file r console logging


【解决方案1】:

您必须分别接收“输出”和“消息”(sink 函数只查看typefirst 元素)

现在,如果您希望 input 也被记录,则将其放入脚本中:

script.R

1:5 + 1:3   # prints and gives a warning
stop("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 console
sink() 
sink(type="message")

# And look at the log...
cat(readLines("test.log"), sep="\n")

【讨论】:

  • 这仅打印输出,但不打印输入。我想查看输入行,例如1:5 + 1:3,然后是它的输出,然后是下一个,等等。我想要生成这种类型的日志的原因是因为我有一个程序需要 30+ GB 的 RAM 才能运行。我在亚马逊云中运行它并将回归的输出保存到单个文件中。我希望能够通过查看日志快速找到生成每个文件的代码。注意:如果我只是剪切-粘贴控制台输出,就可以了。
  • @user443854 如果是这样,最好放弃交互式工作并使用脚本。
  • @user443854:是的,你能把代码放在脚本里吗?在这种情况下, source("script.R", echo=TRUE) 可以解决问题 - 如果您按照上述说明重定向输出。
  • @Tommy 你这个人。谢谢!我确实有一个 .R 脚本,但我将它粘贴到远程框上的交互式会话中。采购就行了。
  • @user443854:是的,使用max.deparse.length 参数。我更新了答案。
【解决方案2】:

如果您可以访问命令行,您可能更喜欢使用 R CMD BATCH 从命令行运行脚本。

== 开始脚本的内容。R ==

a <- "a"
a
How 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 Computing
Platform: 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.

  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.

[Previously saved workspace restored]

> a <- "a"
> a
[1] "a"
> How come I do not see this in log
Error: unexpected symbol in "How come"
Execution halted

== script.Rout 的结束内容 ==

【讨论】:

  • 我正在使用 zsh,由于某种原因 R CMD BATCH script.R &amp; 不起作用。
【解决方案3】:

你不能。最多可以用sink 保存输出,用savehistory 分别输入。或使用scriptscreentmux 等外部工具。

【讨论】:

    【解决方案4】:

    使用 ESS(Emacs Speaks Statistics)r 模式在 emacs 中运行 R。我用我的脚本和 R 代码打开了一个窗口。另一个运行 R。代码从语法窗口发送并评估。命令、输出、错误和警告都出现在正在运行的 R 窗口会话中。在某个工作期结束时,我将所有输出保存到一个文件中。我自己的命名系统是 *.R 用于脚本和 *.Rout 用于保存输出文件。 这是带有示例的屏幕截图。

    【讨论】:

      【解决方案5】:

      如果您能够使用 bash shell,您可以考虑在 bash 脚本中简单地运行 R 代码并将 stdout 和 stderr 流通过管道传输到文件。这是一个使用heredoc的示例:

      文件:test.sh

      #!/bin/bash
      # this is a bash script
      echo "Hello World, this is bash"
      
      test1=$(echo "This is a test")
      
      echo "Here is some R code:"
      
      Rscript --slave --no-save --no-restore - "$test1" <<EOF
        ## R code
        cat("\nHello World, this is R\n")
        args <- commandArgs(TRUE)
        bash_message<-args[1]
        cat("\nThis is a message from bash:\n")
        cat("\n",paste0(bash_message),"\n")
      EOF
      
      # end of script 
      

      然后,当您运行脚本时,将 stderr 和 stdout 都通过管道传输到日志文件:

      $ chmod +x test.sh
      $ ./test.sh
      $ ./test.sh &>test.log
      $ cat test.log
      Hello World, this is bash
      Here is some R code:
      
      Hello World, this is R
      
      This is a message from bash:
      
       This is a test
      

      为此要考虑的其他事情是尝试简单地将标准输出和标准错误从 R heredoc 直接插入日志文件;我还没有尝试过,但它可能也会起作用。

      【讨论】:

        【解决方案6】:

        从控制台保存文本:运行分析,然后选择(Windows)“文件>保存到文件”。

        【讨论】:

          【解决方案7】:

          为大量行设置您的 Rgui 首选项,然后以适当的间隔设置时间戳并另存为文件。

          【讨论】:

          • 请详细说明
          【解决方案8】:

          您可以在运行 R 脚本时打印到文件,同时查看具有(或不具有)screen 的进度。

          不使用屏幕时,使用R CMD BATCH yourscript.R &amp;和第4步。

          1. 使用屏幕时,在终端中启动屏幕

             screen
            
          2. 运行你的 R 脚本

             R CMD BATCH yourscript.R
            
          3. CtrlA 转到另一个屏幕,然后按 c

          4. 使用(实时)查看您的输出:

             tail -f yourscript.Rout
            
          5. 使用 CtrlA 然后 n

            在屏幕之间切换

          【讨论】:

            【解决方案9】:
            1. 如果您想获取保存在文件中的错误消息

              zz <- file("Errors.txt", open="wt")
              sink(zz, type="message")
              

              输出将是:

              Error in print(errr) : object 'errr' not found
              Execution halted
              

              输出将保存在名为 Errors.txt 的文件中

            2. 如果您希望将控制台的打印值写入文件,您可以使用 'split' 参数:

              zz <- file("console.txt", open="wt")
              sink(zz,  split=TRUE)
              print("cool")
              print(errr)
              

              输出将是:

              [1] "cool"
              

              在 console.txt 文件中。因此,您的所有 控制台输出都将打印在名为 console.txt 的文件中

            【讨论】:

              【解决方案10】:

              这可能无法满足您的需求,但一种解决方案可能是从 Rmarkdown 文件中运行您的代码。您可以将代码和控制台输出写入 HTML/PDF/Word。

              【讨论】:

              • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
              猜你喜欢
              • 1970-01-01
              • 2020-06-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2022-07-15
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多