1

I understand from this question that it's possible to push a variable to an Rnw file from the R script that calls it by storing that variable in the global environment within the R script. I've tried to do that but I can't get the Sweave file to recognize the variable.

For example, if I have an R script (say, test.R) that looks like:

TESTGLOBALVARIABLE <<- "XXXXXXXXXTESTGLOBALVARIABLEXXXXXXXXXXX"
system("R CMD Sweave /foo/bar/test.Rnw"))

and test.Rnw looks like:

\documentclass{article}
\usepackage[absolute]{textpos}
\setlength{\TPHorizModule}{30mm}
\setlength{\TPVertModule}{\TPHorizModule}
\textblockorigin{10mm}{10mm} % start everything near the top-left corner
\setlength{\parindent}{0pt}

\begin{document}

\begin{textblock}{3}(0,0)
The test global variable is: \Sexpr{TESTGLOBALVARIABLE}
\end{textblock}

\begin{textblock}{2}(2,1)
\textblocklabel{block two}
Here is another, slightly narrower, block, at position (2,1) on the page.
\end{textblock}

\end{document}

When I run test.R, I get an error Error: at test.Rnw:13, object 'TESTGLOBALVARIABLE' not found Execution halted.

I've checked similar questions like this one. Is it possible to do this without brew?

0

Does it work when you source your file.R?

\documentclass{article}
\usepackage[absolute]{textpos}
\setlength{\TPHorizModule}{30mm}
\setlength{\TPVertModule}{\TPHorizModule}
\textblockorigin{10mm}{10mm} % start everything near the top-left corner
\setlength{\parindent}{0pt}

\begin{document}

<<sourcing_r, include=FALSE, echo=FALSE, results=hide>>=
source(file = "/foo/bar/test.R")
@


\begin{textblock}{3}(0,0)
The test global variable is: \Sexpr{TESTGLOBALVARIABLE}
\end{textblock}

\begin{textblock}{5}(2,1)
\textblocklabel{block two}
Here is another, slightly narrower, block, at position (2,1) on the page.
\end{textblock}

\end{document}
  • If I source the original "/foo/bar/test.R" script that calls the R CMD Sweave command, obviously it hangs. But I'll try sourcing it from a different R script. – wtrs Mar 1 '18 at 14:12
  • It works if I remove the R CMD Sweave command, but that defeats the purpose. If I create another script (say, /foo/bar/set_vars.R) with only the TESTGLOBALVARIABLE <<- "XXXTESTGLOBALVARIABLEXXXX" line, that wouldn't work either, because I need to set the value of TESTGLOBALVARIABLE within test.R, so I can't have it hardcoded in set_vars.R. If there is some way to edit set_vars.R within test.R, that could work. I'll play around with file.edit() and see if I can get it to work. – wtrs Mar 1 '18 at 14:42
0

With some inspiration from nebi's answer, I was able to do it by creating a separate R script set_vars2.R written by test.R:

If test.R is:

directory <- "/foo/bar/"
setwd(directory)

file.create(paste0(directory, "set_vars2.R", open = "r"))
fileConn <- file(paste0(directory, "set_vars2.R"))
writeLines(c("TESTGLOBALVARIABLE <<- \"XXXXTESTGLOBALVARIABLEXXXXX\"",""), fileConn)
close(fileConn)

system(paste0("R CMD Sweave ", directory, "test.Rnw"))
system(paste0("pdflatex ",     directory, "test.tex"))

and test.Rnw is:

\documentclass{article}

\usepackage[absolute]{textpos}

\setlength{\TPHorizModule}{30mm}
\setlength{\TPVertModule}{\TPHorizModule}
\textblockorigin{10mm}{10mm} % start everything near the top-left corner
\setlength{\parindent}{0pt}

\begin{document}

<<sourcing_r, include=FALSE, echo=FALSE, results=hide>>=
source("/foo/bar/set_vars2.R")
@

\begin{textblock}{3}(0,0)
The test global variable is \Sexpr{TESTGLOBALVARIABLE}
\end{textblock}

\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.