# [R] sweave question

Nordlund, Dan (DSHS/RDA) NordlDJ at dshs.wa.gov
Thu Feb 14 00:50:25 CET 2013

> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Mark Leeds
> Sent: Wednesday, February 13, 2013 2:59 PM
> To: r-help at r-project.org
> Subject: [R] sweave question
>
> Hi Everyone: I was having trouble getting the plot size correct when
> putting a plot in an sweave document. So, I searched on the internet
> and I
> found a clever solution where the person uses
> the cat function to write latex code so he can include the cat code in
> the
> chunk and place the plot using width and height. The solution is here:
> https://stat.ethz.ch/pipermail/r-help/2009-April/195094.html.
>
> But the solution uses a backslash before the ending quotes in the cat
> statements  that might be windows relevant possibly ? I'm not sure
> that part but I'm on linux and when I tried my way to mimic that
> solution
> in the latex/sweave code below ( I think it's the same solution but
> without
> the backslash on the closing quotes. I tried that also but got a
> different
> error ) , Sweave doesn't like the start of the includegraphics command.
> The
> error I get from Sweave is
>
> #=======================================================
> Error:  chunk 9 (label = fig3)
> Error : '\i' is an unrecognized escape in character string starting
> "\i"
> Execution halted
> :~/research/equity/projects/tree_returns/report>
> #=====================================================
>
> The code is below but if anyone is interested and thinks they need the
> whole file to look at the problem, I can send it. I just didn't want to
> send the whole file here. Definitely the error has to do with the chunk
> below for sure ( even though it says chunk 9 because there is no chunk
> 9
> ).  Thanks a lot to anyone who can be bothered investigating because
> I've
> spent pretty much the whole day on this and think it's
> time to surrender !!!!!!!!!! or atleast give up for now and come back
> to it
> with a fresher brain. Have a good night.
>
> # THE LATEX/SWEAVE CODE
> #================================================================
>
> cal.tree is created in a previous chunk and that's fine.
>
> <<chunk08,label=fig3, fig=FALSE,result = tex>>=
> wid <- 5
> hei <- 5
> pdf("reg_tree-fig3.pdf", width = wid, height = hei)
> plot(cal.tree,uniform=TRUE,compress=TRUE,margin=0.0)
> text(cal.tree, all=TRUE,fheight=1.0,fwidth=1.0)
> dev.off()
> cat("\begin{figure}[ht!]")
> cat("\begin{center}")
> cat("\includegraphics[width = ", wid, ", height = ", hei,
> "]{reg_tree-fig3}")
> cat("\end{center}")
> cat("\caption{The resulting unpruned regression tree of the california
> real
> estate data}")
> cat("\label{FIG:figthree}")
> cat("\end{figure}")
> @
>

Mark,

I am not an sweave expert, but I think the problem is actually that you need to double all of your backslash characters that you actually want to write out as a backslash.  The error message says that it doesn't recognize the escape character '\i'.

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA 98504-5204