include tikz commands via Sexpr

84 views Asked by At

I want to use tikz to implement basic graphics in an automated report created by Sweave. Therefore, I created an R function, returning the tikz command, based on the data. My code looks similar to this:

<<echo=FALSE>>=
y <- 20
code <- cat(paste("\\draw (0pt, ",y,"pt) circle (5pt)", sep=""))
@
\begin{tikzpicture}
\Sexpr{code}
\end{tikzpicture}

Which does not work ... I get the string instead of the drawing.

1

There are 1 answers

0
user2554330 On

Don't use \Sexpr, just put the chunk in the tikzpicture environment, and add the chunk option results=tex. For example,

\begin{tikzpicture}
<<echo=FALSE, results=tex>>=
y <- 20
cat(paste0("\\draw (0pt, ",y,"pt) circle (5pt)", sep=""))
@
\end{tikzpicture}

This isn't tested, so you may need to tweak it a little.