This is a terribly simple request, and I can't believe I haven't found the solution to this yet, but I've been searching for it far and wide without luck.
这是一个非常简单的请求,我无法相信我还没有找到解决方案,但我一直在寻找它,没有运气。
I have an .Rnw
file loaded up in Emacs, I use M-n s
to compile it. Everything works well, and it even opens an R
buffer. Great. But that buffer is entirely useless: it doesn't contain the objects that I just sourced!
我在Emacs中加载了一个.Rnw文件,我使用M-n来编译它。一切都运作良好,甚至打开一个R缓冲区。大。但是那个缓冲区完全没用:它不包含我刚刚提供的对象!
Example minimal .Rnw
file:
示例最小.Rnw文件:
\documentclass{article}
\begin{document}
<<>>=
foo <- "bar"
@
\end{document}
Using M-n s
, I now have a new R-buffer with a session loaded up, but:
使用M-n s,我现在有一个新的R缓冲区,会话已加载,但是:
> foo
Error: object 'foo' not found
That is disappointing. I would like to play around with the data interactively. How do I achieve that? I don't want to be sourcing the file line-by-line, or region-by-region with C-c C-c
or something similar every time I change my code. Ideally, it should be just like RStudio's source function, that leaves me with a fully prepared R session.
这令人失望。我想以交互方式使用数据。我如何实现这一目标?我不希望每次更改代码时逐行或逐个区域地使用C-c C-c或类似的东西来获取文件。理想情况下,它应该像RStudio的源函数一样,让我有一个完全准备好的R会话。
I haven't tried this with sweave yet, only with knitr.
我还没有用sweave试过这个,只有knitr。
EDIT: the eval=TRUE
chunk option does not seem to result in the correct behaviour.
编辑:eval = TRUE块选项似乎不会导致正确的行为。
2 个解决方案
#1
6
This behaviour was recently changed in ESS. Now sweave and knitr are executed directly in the global environment, as if when you write it yourself at command line. So wait for a couple of more weeks till ESSv13.09 is out or use the development version.
最近在ESS中更改了此行为。现在,sweave和knitr直接在全局环境中执行,就像在命令行自己编写它一样。所以等待几周,直到ESSv13.09出来或使用开发版本。
Alternatively, you can also set ess-swv-processing-command
to "%s(%s)" and you will get the same result, except automatic library loading.
或者,您也可以将ess-swv-processing-command设置为“%s(%s)”,除自动库加载外,您将获得相同的结果。
For the record, knitr (in contrast to sweave) evaluates everything in it's own environment unless you instruct it otherwise.
对于记录,knitr(与sweave相反)评估其自身环境中的所有内容,除非您另外指示。
[edit: Something went wrong. I don't see the correct .ess_weave any more. Probably some git commit messup again. So it is not fixed in 13.09. Fixing it now. Sorry.]
[编辑:出了点问题。我再也看不到正确的.ess_weave了。可能一些git再次提交了混乱。所以它在13.09没有修复。立即修复它。抱歉。]
#2
6
Open an interactive R session, and then call Sweave directly, I believe like this (untested though). knitr
works in the same way, though you need to load the knitr library first.
打开一个交互式R会话,然后直接调用Sweave,我相信这样(虽然未经测试)。 knitr的工作方式相同,但您需要先加载knitr库。
> Sweave("yourfile.Rnw")
There is some potential for peril here, though. If you call Sweave in a session after doing other things, your code can use things previously in the workspace, thus making your results unreproducible.
但是,这里有一些潜在的危险。如果在执行其他操作后在会话中调用Sweave,则代码可以使用先前在工作区中的内容,从而使结果无法再生。
#1
6
This behaviour was recently changed in ESS. Now sweave and knitr are executed directly in the global environment, as if when you write it yourself at command line. So wait for a couple of more weeks till ESSv13.09 is out or use the development version.
最近在ESS中更改了此行为。现在,sweave和knitr直接在全局环境中执行,就像在命令行自己编写它一样。所以等待几周,直到ESSv13.09出来或使用开发版本。
Alternatively, you can also set ess-swv-processing-command
to "%s(%s)" and you will get the same result, except automatic library loading.
或者,您也可以将ess-swv-processing-command设置为“%s(%s)”,除自动库加载外,您将获得相同的结果。
For the record, knitr (in contrast to sweave) evaluates everything in it's own environment unless you instruct it otherwise.
对于记录,knitr(与sweave相反)评估其自身环境中的所有内容,除非您另外指示。
[edit: Something went wrong. I don't see the correct .ess_weave any more. Probably some git commit messup again. So it is not fixed in 13.09. Fixing it now. Sorry.]
[编辑:出了点问题。我再也看不到正确的.ess_weave了。可能一些git再次提交了混乱。所以它在13.09没有修复。立即修复它。抱歉。]
#2
6
Open an interactive R session, and then call Sweave directly, I believe like this (untested though). knitr
works in the same way, though you need to load the knitr library first.
打开一个交互式R会话,然后直接调用Sweave,我相信这样(虽然未经测试)。 knitr的工作方式相同,但您需要先加载knitr库。
> Sweave("yourfile.Rnw")
There is some potential for peril here, though. If you call Sweave in a session after doing other things, your code can use things previously in the workspace, thus making your results unreproducible.
但是,这里有一些潜在的危险。如果在执行其他操作后在会话中调用Sweave,则代码可以使用先前在工作区中的内容,从而使结果无法再生。