开发者

Sweave: interpret R's output as LaTeX code

I have written some custom R code that wraps a third-party binary. One of that binary's features is to produce a LaTeX document with a figure and some text. One of my code's features is to parse that document and return the LaTeX code for the figure.

The goal is to embed my R code in an Rnw document. When Sweave is run, I want my code to produce a document using the third-party binary, then extract the LaTeX code for the figure and drop it into Sweave's .tex output. Then when I run latex against that output the figure that was generated by the third-party binary should appear, automagically and nicely formatted, 开发者_StackOverflow社区in my report.

Instead, the LaTeX code is printed out like this:

[1] " %\\begin{landscape}"
[1] " \\begin{center}"
[1] "\\psset{linecolor=black,tnsep=2pt,tnheight=0cm,treesep=.3cm,levelsep=40pt,radius[1] "% \\def\\psedge#1#2{\\ncangle{#2}{#1}}"
[1] "% \\pstree[treemode=R]"
[1] " \\pstree{\\Tcircle{ 1 }~[tnpos=l]{\\shortstack[r]{nwsprec\\\\$\\leq$ 1.93}}}{"

And so on...

Is there a way to make Sweave treat R's output as LaTeX code?

Thanks in advance. -Wesley


I figured it out! The Sweave code needs to look like this:

<<echo=False, results=tex, include=True>>=
...R code goes here...
@

Where the option results=tex is the crucial change that tells Sweave to interpret the output from R as LaTeX code.

And to get rid of the quotes and line numbers, I needed to use cat command in R rather than print. Specifically, I changed print(line) to cat( paste(line, "\n", sep='') ).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜