我有一个将向量设置为字符串的函数,复制一个具有新名称的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"和打印(...chstud...))函数会生成错误。
我怀疑这是一个环境问题,但我假设因为对Sweave的调用发生在onOK函数中,所以它应该在相同的环境中,并且会看到在该函数中创建的所有对象。如何让Sweave进程查看chstud向量?
谢谢
保罗。
发布于 2010-05-28 17:00:38
好吧,我意识到我最初关于“简单的,自包含的例子”的想法并不是特别简单或有用。因此,我重做了我的示例,并为自己得到了某种答案,尽管我希望有人来解释原因,甚至提出更好的解决方案,以下是我的示例test_sweave.Rnw文件
%
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}
\title{Test Sweave Document}
\author{Paul Hurley}
\maketitle
<<>>=
if(exists("foo")){print(foo)}
ls()
Sys.time()
@
\end{document}如果我运行这段代码;
testFoo<-function(){
foo<-"My Test String"
Sweave("test_sweave.Rnw")
require(tools)
texi2dvi(file = "test_sweave.tex", pdf = TRUE)
}
rm(foo) testFoo()我的结果文件不包含字符串foo的内容。
> if (exists("foo")) {
+ print(foo)
+ }
> ls()
[1] "testFoo"如果我运行这段代码(也就是说,同样的事情,直接运行)
rm(foo)
foo<-"My Test String"
Sweave("test_sweave.Rnw")
require(tools)
texi2dvi(file = "test_sweave.tex", pdf = TRUE)我的结果文件确实包含foo字符串
> if (exists("foo")) {
+ print(foo)
+ }
[1] "My Test String"
> ls()
[1] "foo" "testFoo"如果我运行这段代码
testBar<-function(){
foo<<-"My Test String"
Sweave("test_sweave.Rnw")
require(tools)
texi2dvi(file = "test_sweave.tex", pdf = TRUE)
}
rm(foo)
testBar()我的结果文件还包含foo字符串
> if (exists("foo")) {
+ print(foo)
+ }
[1] "My Test String"
> ls()
[1] "foo" "testBar" "testFoo"因此,sweave似乎是在全局环境中运行,而不是在调用它的环境中运行。这意味着,当从函数运行sweave时,将变量传递给sweave的唯一方法是使用<<-运算符将变量放入全局环境中。(我想)。
还有谁想评论一下谁对环境了解得更多?
发布于 2011-01-25 04:57:59
我也有类似的问题。最终,我找到了一个“对我有效”的变通方法,尽管它可能不是解决这个问题的最优雅的方法。
在我的函数中,在执行'Sweave‘之前,我放了一条语句来全局存储本地环境:
temp <<- environment()使用您的代码示例,它将如下所示:
testFoo<-function(){
foo<-"My Test String"
temp <<- environment()
Sweave("test_sweave.Rnw")
require(tools)
texi2dvi(file = "test_sweave.tex", pdf = TRUE)
}
rm(foo) testFoo()然后,在第一个块的开头要‘LaTeX’的文件中,我恢复了必要的变量,但您也可以使用'temp$foo‘来访问在函数中创建的'foo’变量。这样,我就避免了全局存储多个变量。
%
\documentclass[a4paper]{article}
\usepackage[OT1]{fontenc}
\usepackage{Sweave}
\begin{document}
\title{Test Sweave Document}
\author{Paul Hurley}
\maketitle
<<>>=
if(exists("foo", envir=temp)) { print(temp$foo) }
ls()
Sys.time()
@
\end{document}就像我在上面写的--这是可行的,但不是很优雅。
https://stackoverflow.com/questions/2912270
复制相似问题