如果从函数运行,Sweave 无法看到向量?
我有一个函数,它将向量设置为字符串,使用新名称复制 Sweave 文档,然后运行该 Sweave。在Sweave文档中我想使用我在函数中设置的向量,但它似乎没有看到它。
(编辑:我按照 Dirk 的建议更改了此函数以使用 tempdir(())
我创建了一个 sweave 文件 test_sweave.rnw;
%
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}
\title{Test Sweave Document}
\author{gb02413}
\maketitle
<<>>=
ls()
Sys.time()
print(paste("The chosen study was ",chstud,sep=""))
@
\end{document}
并且我有此函数;
onOK <- function(){
chstud<-"test"
message(paste("Chosen Study is ",chstud,sep=""))
newfile<-paste(chstud,"_report",sep="")
mypath<-paste(tempdir(),"\\",sep="")
setwd(mypath)
message(paste("Copying test_sweave.Rnw to ",paste(mypath,newfile,".Rnw",sep=""),sep=""))
file.copy("c:\\local\\test_sweave.Rnw",
paste(mypath,newfile,".Rnw",sep=""), overwrite=TRUE)
Sweave(paste(mypath,newfile,".Rnw",sep=""))
require(tools)
texi2dvi(file = paste(mypath,newfile,".tex",sep=""), pdf = TRUE)
}
如果我直接从该函数运行代码,则生成的文件具有此输出ls();
> ls()
[1] "chstud" "mypath" "newfile" "onOK"
但是,如果我调用 onOK() 我会得到此输出;
> ls()
[1] "onOK"
并且 print(...chstud...)) 函数会生成错误。
我怀疑这是一个环境问题,但我假设因为对 Sweave 的调用发生在 onOK 函数内,所以它将位于相同的环境中,并且会看到该函数内创建的所有对象。如何让 Sweave 进程查看 chstud 矢量?
谢谢
保罗。
I have a function that sets a vector to a string, copies a Sweave document with a new name and then runs that Sweave. Inside the Sweave document I want to use the vector I set in the function, but it doesn't seem to see it.
(Edit: I changed this function to use tempdir(() as suggested by Dirk)
I created a sweave file test_sweave.rnw;
%
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}
\title{Test Sweave Document}
\author{gb02413}
\maketitle
<<>>=
ls()
Sys.time()
print(paste("The chosen study was ",chstud,sep=""))
@
\end{document}
and I have this function;
onOK <- function(){
chstud<-"test"
message(paste("Chosen Study is ",chstud,sep=""))
newfile<-paste(chstud,"_report",sep="")
mypath<-paste(tempdir(),"\\",sep="")
setwd(mypath)
message(paste("Copying test_sweave.Rnw to ",paste(mypath,newfile,".Rnw",sep=""),sep=""))
file.copy("c:\\local\\test_sweave.Rnw",
paste(mypath,newfile,".Rnw",sep=""), overwrite=TRUE)
Sweave(paste(mypath,newfile,".Rnw",sep=""))
require(tools)
texi2dvi(file = paste(mypath,newfile,".tex",sep=""), pdf = TRUE)
}
If I run the code from the function directly, the resulting file has this output for ls();
> ls()
[1] "chstud" "mypath" "newfile" "onOK"
However If I call onOK() I get this output;
> ls()
[1] "onOK"
and the print(...chstud...)) function generates an error.
I suspect this is an environment problem, but I assumed because the call to Sweave occurs within the onOK function, it would be in the same enviroment, and would see all the objects created within the function. How can I get the Sweave process to see the chstud vector ?
Thanks
Paul.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有类似的问题。最终我找到了一种“适合我”的解决方法,尽管它可能不是解决这个问题的最优雅的方法。
在我的函数中,在执行“Sweave”之前,我放置了一条语句来全局存储本地环境:
使用您的代码示例,它看起来像这样:
然后在要在第一个块的开头进行“Sweaved”的 LaTeX 文件中恢复必要的变量,但您也可以使用“temp$foo”来访问在函数中创建的“foo”变量。这样我就可以避免全局存储多个变量。
就像我上面写的那样 - 这可以工作,但不是很优雅。
I have similar problem. Eventually I found a work-around that "works for me", although it may not be the most elegant way to solve this issue.
Within my function, before 'Sweave' is executed, I put a statement to globally store the local environment:
Using your code example it would look something like:
Then in the LaTeX file to be 'Sweaved' in the beginning of the first chunk I restore the necessary variables, but you can also use 'temp$foo' to access the 'foo' variable that was created within the function. This way I avoid globally storing multiple variables.
Like I wrote above - this works, but is not very elegant.
好吧,我意识到我最初关于“简单、独立的示例”的想法并不是特别简单或有用。因此,我重新编写了我的示例,并为自己找到了一种答案,尽管我希望有人解释原因,甚至提出更好的解决方案,
这是我的示例 test_sweave.Rnw 文件
如果我运行此代码;
我的结果文件不包含字符串 foo 的内容。
如果我运行这段代码(即相同的东西,直接运行),
我的结果文件确实包含 foo 字符串
,如果我运行这段代码,
我的结果文件也包含 foo 字符串
所以,看起来 sweave 在全局环境中运行,不在它被调用的环境中。这意味着当 sweave 从函数运行时将变量传递给 sweave 的唯一方法是使用 <<- 运算符将变量放入全局环境中。 (我认为)。
还有谁想评论一下谁对环境更了解?
OK, I realise that my initial ideas of a 'simple, self contained example' wasn't particularly simple or usefull. So I redid my example and got a sort of answer for myself, although I'd love someone to exaplain why and even suggest a better solution,
Here's my example test_sweave.Rnw file
If I run this code;
my resulting file does NOT contain the contents of the string foo.
If I run this code (i.e, the same thing, just run directly)
my resulting file does contain the foo string
and if I run this code
My resulting file also contains the foo string
So, it seems that sweave runs in the global environment, not in the environment it was called from. This means the only way to pass variables to sweave when sweave is run from a function is to use the <<- operator to put the variable in the global environment. (I think).
Anyone else want to comment who knows more about environments ?