如何在 Sweave 中循环全局变量

发布于 2024-08-29 17:45:50 字数 262 浏览 7 评论 0原文

我有一个很大的 Sweave 文件,顶部附近有一个名为“specialty”的变量。该文件的其余部分是 Latex 和 R,并使用此变量。

如何循环遍历“专业”的各种值?

两种可能性是:

  1. 使文件成为一个大循环(并将 Latex 部分转换为 R)。
  2. 编写一个脚本来复制 Sweave 文件,替换“specialty”的值,并在每个副本上运行 Sweave。

您能否对这些想法发表评论,或者提出更好的建议?

I have a big Sweave file with a variable called "specialty" near the top. The rest of this file is Latex and R, and uses this variable.

How can I loop over various values for "specialty"?

Two possibilities are:

  1. Make the file be one big loop (and convert the Latex parts to R).
  2. Write a script that copies the Sweave file, replace the value of "specialty", and run Sweave on each copy.

Can you comment on these ideas, or suggest better ones?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终止放荡 2024-09-05 17:45:50

以下是一些可能对刚开始酿造的人有所帮助的信息。

(我今天了解了brew,并用它创建了一个书籍文档,每个“专业”都有一个章节。)

Shane 的链接很有帮助。另一个链接是 Brew。有下载和简短的参考手册(七页)。

至少在一个方面,brew 比 Sweave 更好:

  • 在brew 中,标签更简单,是<%...%> 的变体。
  • 在 Sweave 中,标签为 <<...>>=...@ 和 \Sexpr{...}。

如果您想尝试brew,请在R 中执行以下操作:

install.packages("brew")
library(brew)

将以下brew 代码保存在名为book.brew 的文件中。该代码打印 pi 的一些数字,每章一位数字。请注意,有一个循环,其中一部分位于 Latex 中,部分位于 brew 标签中。

   \documentclass{book}
    \title{A book}
    \begin{document}
    \maketitle
    <%# This comment will not appear in the Latex file. %>
    <%
    digits = c(3, 1, 4, 1, 5, 9)
    for (i in 1:length(digits))
    {
    %>
    \chapter{Digit 
lt;%= i %>$}
    Digit 
lt;%= i %>$ of $\pi$ is 
lt;%= digits[i] %>$.
    <%
    }
    %>
    \end{document}

注意:保存文件时,请将最后一行设为空行,否则brew 会警告您有未完成的行。

在 R 中,输入

brew("/your/path/to/book.brew", "/where/you/want/brew/to/create/book.tex")

编译 Latex 文件 book.tex。

Here is some information that may be helpful to people who are new to brew.

(I learned about brew today and used it to create a book document, with a chapter for each "specialty".)

Shane's link is helpful. Another link is Brew. This has downloads and a short reference manual (seven pages).

In at least one way, brew is nicer than Sweave:

  • In brew, the tags are simpler, being variations of <%...%>.
  • In Sweave, the tags are <<...>>=...@ and \Sexpr{...}.

If you want to try brew, do the following in R:

install.packages("brew")
library(brew)

Save the following brew code in a file called book.brew. The code prints some digits of pi, with one digit per chapter. Note that there is one loop, and that parts of it are in Latex, and parts of it are in brew tags.

   \documentclass{book}
    \title{A book}
    \begin{document}
    \maketitle
    <%# This comment will not appear in the Latex file. %>
    <%
    digits = c(3, 1, 4, 1, 5, 9)
    for (i in 1:length(digits))
    {
    %>
    \chapter{Digit 
lt;%= i %>$}
    Digit 
lt;%= i %>$ of $\pi$ is 
lt;%= digits[i] %>$.
    <%
    }
    %>
    \end{document}

Note: when you save the file, make the last line be a blank line, or brew will give you a warning about an unfinished line.

In R, type

brew("/your/path/to/book.brew", "/where/you/want/brew/to/create/book.tex")

Compile the Latex file book.tex.

傾旎 2024-09-05 17:45:50

您能说明您希望文档最终是什么样子吗?显然它有重复的结构。在这种情况下,Sweave 可能不是最好的工具。您可能想考虑使用诸如 brew 之类的东西。请参阅学习 R 博客上的这篇博文有关其工作原理的示例。

Could you state what you want your document to look like in the end? Clearly it has repetitive structures in it. In which case, Sweave may not be the best tool. You might instead want to consider using something like brew. See this blog post on the Learning R blog for an example of how this works.

装迷糊 2024-09-05 17:45:50

我之前已经使用你的第二个选项完成了这一点。我有一个单独的 R 文件,它将迭代组名称,将每个组名称分配给组变量(您的专业),创建 Sweave 主文件的重命名副本(将组名称粘贴到文件名中) ),然后 Sweave 该新文件。您使用“替换”一词让我犹豫 - 我不会尝试任何类型的正则表达式解决方案(也许这不是您想要的)。只需在主脚本中分配它(specialty <-specialties[i])就可以了。

该代码被困在我目前已经死机的家用电脑上,但我可能将其保存在闪存驱动器上的某个地方。如果您在正常工作时遇到困难,请告诉我,我会尽力解决。

brew 可能也值得研究,尽管我还没有任何个人经验,因此无法将它与 Sweave 进行比较。

I've done exactly this before, using your second option. I had a separate R file that would iterate through the group names, assign each to the group variable (your specialty), create a renamed copy of the Sweave master file (with the group's name pasted into the filename), and then Sweave that new file. Your use of the word "replace" makes me hesitant - I wouldn't try any kind of regex solution (and maybe that's not what you intend). Just assigning it in the master script (specialty <- specialties[i]) is fine.

That code is trapped on my currently-dead home PC, but I might have it on a flashdrive somewhere. If you have trouble getting this working right, let me know and I'll dig around for it.

brew is probably also worth looking into, though I don't have any personal experience with it yet and therefore can't compare it to Sweave.

画尸师 2024-09-05 17:45:50

有一个解决方案可以让您留在 Sweave 中而无需使用 Brew 。关键是使用 \newcommand 将循环中应用的代码转换为 Latex 宏,然后有一个 R 块循环遍历您的变量并为每个值

A 发出对 Latex 宏的调用完整的示例可在 https://stat.ethz.ch/ Pipermail/r-help/2008-June/164783.html,但要点如下:

\documentclass{article}
\SweaveOpts{echo=FALSE}
\newcommand\digit[2]{%
  \section{Digit #1}
  Digit #1 of $\pi$ is $#2$.
}
\title{Digits of $\pi$}
\begin{document}
\maketitle
<<results=tex>>=
digits = c(3, 1, 4, 1, 5, 9)
for (i in seq(digits)) {
  cat(paste("\\digit{", i, "}{", digits[i], "}\n", sep=""))
}
@ 
\end{document}

There a solution that lets you stay in Sweave without having to use Brew . The key is to turn the code that's applied in the loop into a Latex macro with \newcommand, and then have an R chunk that loops over your variable and emits a call to your Latex macro for each value

A complete example is available at https://stat.ethz.ch/pipermail/r-help/2008-June/164783.html, but here is the gist of it:

\documentclass{article}
\SweaveOpts{echo=FALSE}
\newcommand\digit[2]{%
  \section{Digit #1}
  Digit #1 of $\pi$ is $#2$.
}
\title{Digits of $\pi$}
\begin{document}
\maketitle
<<results=tex>>=
digits = c(3, 1, 4, 1, 5, 9)
for (i in seq(digits)) {
  cat(paste("\\digit{", i, "}{", digits[i], "}\n", sep=""))
}
@ 
\end{document}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文