【问题标题】:how can I suppress output from Sweave that is not suppressed by echo=FALSE?如何抑制 Sweave 中未被 echo=FALSE 抑制的输出?
【发布时间】:2010-10-21 19:49:04
【问题描述】:

我的 .tex 文件中出现了无法使用 或 sink() 抑制的无关输出。值得注意的是,不需要的行没有用 ..{Schunk} 或类似名称括起来。

当我使用 DEoptim 或 rjags 时会发生这种情况,尽管这可能不限于这些功能。

示例 .Rnw 文件:

\documentclass[a4paper, 12]{article}
begin{document}

<<echo=FALSE>>=
require(DEoptim) 
Rosenbrock <- function(x){ #example from DEoptim authors 
  x1 <- x[1]
  x2 <- x[2]
  100 * (x2 - x1 * x1)^2 + (1 - x1)^2
}
lower <- c(-10,-10)
upper <- -lower
set.seed(1234)
DEoptim(Rosenbrock, lower, upper)

@

\end{document}

我想要发生的事情 我想要的结果是如果输出被抑制,或者等效地,如果从 .Rnw 文件中删除代码块,则会生成 tex 文件:

\documentclass[a4paper, 12]{article}
\usepackage{Sweave}
\begin{document}

\end{document}

会发生什么 但是,生成的 .tex 文件具有函数的输出:

\documentclass[a4paper, 12]{article}
\usepackage{Sweave}
\begin{document}

Iteration: 1 bestvalit: 132.371451 bestmemit:   -1.851683    4.543355
Iteration: 2 bestvalit: 8.620563 bestmemit:   -1.854371    3.369908
....few hundred lines of DEoptim output ....
$member$storepop
list()


attr(,"class")
[1] "DEoptim"
\end{document}

注意输出没有被 \begin{Schunk} \end{Schunk} 括起来,所以 $ 符号会混淆 LaTeX 并且它不会编译。

【问题讨论】:

    标签: r latex sweave


    【解决方案1】:

    你试过了吗

    <<echo=FALSE, results=hide>>
    

    ?

    【讨论】:

      【解决方案2】:

      输出来自对 DEoptim 中编译函数(C 或 Fortran)的调用。

      这会产生干净的输出:

      \documentclass[a4paper, 12]{article}  
      \begin{document}
      
      \section{Computation in R}
      
      <<computation,results=hide>>=  
      require(DEoptim)  
      Rosenbrock <- function(x){  
          x1 <- x[1]  
          x2 <- x[2]  
          100 * (x2 - x1 * x1)^2 + (1 - x1)^2  
      }  
      lower &lt;- c(-10,-10)  
      upper &lt;- -lower  
      set.seed(1234)  
      res &lt;- DEoptim(Rosenbrock, lower, upper)  
      
      @  
      \section{Results}  
      
      <<results>>=  
      res$optim  
      
      
      @  
      \end{document}  
      

      【讨论】:

      • 嗨 Henrik,我从代码块中删除了 results=tex。我在实际文档中使用它与 xtable() 一起使用,但从示例中删除了该表。不管有没有,结果都是一样的。
      • 我添加了一个我希望看到的示例 - 运行代码的所有输出都被抑制。在 rjags 的情况下,输出是五页的进度报告。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-07
      • 2014-10-17
      • 2018-08-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多