gpt4 book ai didi

r - knitr 与 latex forloop

转载 作者:行者123 更新时间:2023-12-04 05:40:27 25 4
gpt4 key购买 nike

对不起,如果这是一个愚蠢的问题,但是有人知道如何在 knitr 中循环 R 函数吗?到目前为止,我的问题是将变量从 Latex 传递给 R 函数。我正在尝试做这样的事情:

\documentclass{article}
\usepackage{forloop}
\newcounter{ind}
\begin{document}

%Simple R function:
<<simpleRFun, results='asis' ,echo=FALSE>>=
simpleRFun = function(ind){
set.seed(ind) ;
plot(runif(100)) ;
}
@

%Run the function for value of 1
<<>>=
simpleRFun(1)
@

%Run the function for value of 2
<<>>=
simpleRFun(2)
@

%Loop over values of 1 and 2:
\forloop{ind}{1}{\value{ind} < 3}{
\arabic{ind}
}

%Loop over values of 1 and 2 and pass to R function:
%Everything runs fine until this line:
\forloop{ind}{1}{\value{ind} < 3}{
<<>>=
simpleRFun(ind)
@
}

\end{document}

我收到以下错误:
Runaway argument?
#### Error: object 'ind' not found \end {verbatim} \end {kframe} \end \ETC.
./knitr-minimal.tex:97: Paragraph ended before \@xverbatim was complete.
<to be read again>
\par
l.97 }

预先感谢您的帮助。

最佳答案

我不认为你可以做你想做的事(至少不是你想做的事)。 knitr函数运行所有 R 代码而不对 LaTeX 代码做任何事情,然后你在结果上使用 LaTeX,它不会为 R 部分运行 R。

您可以重做循环以在 R 中进行循环,以便 knitr将循环的结果放入 LaTeX 中?

或者你需要一个 LaTeX 的扩展来为你运行 R 部分。

关于r - knitr 与 latex forloop,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11318021/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com