An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130213/d57efedc/attachment.pl>
sweave question
8 messages · Nordlund, Dan (DSHS/RDA), Duncan Mackay, Daniel Nordlund +2 more
-----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 about 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'. Hope this is helpful, 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
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130213/dda33071/attachment.pl>
Hi
Dan is correct
The alternative is to use a chunk to create the plot
eg
<<reg3print, results=hide, echo=FALSE>>=
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()
@
an call the file in the latex figure in the usual way
\begin{figure}[!h]
\centering
\includegraphics[width=...,%
clip=true,%
trim=0in 0in 0in 0in,%
keepaspectratio=true]%
{....reg_tree-fig3.pdf}
...
\end{figure}
Regards
Duncan
Duncan Mackay
Department of Agronomy and Soil Science
University of New England
Armidale NSW 2351
Email: home: mackay at northnet.com.au
At 09:50 14/02/2013, you wrote:
-----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 about 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'. Hope this is helpful, 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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Mark,
just to be clear, since R uses the backslash character as an escape character inside strings, then any backslash that you want to appear in your latex code must be escaped, i.e. it must be doubled. But any backslash character that is being used as an escape character should remain as a single character.
I looked at the solution you linked to, and it looks to me like the post got a little garbled in transmission. Here is what I saw.
<<label=fig1, fig=FALSE, result = tex>>=
pdf("fig1.pdf", width = wid, heigth = hei)
plot(1:10)
dev.off()
cat("\begin{figure}\")
cat("\includegraphics[width = ", wid, ", height = ", hei, "]{proj1-fig1}\"}
cat("\end{figure}")
@
In the first cat(), the "\begin{figure}\" is not going to be interpreted properly. the \b will be interpreted as an escape character, and so what will be printed is something like this (where '_' represents the escape sequence \b that I didn't look up)
_egin{figure}
In addition, you want to be writing the latex commands on separate lines, which requires that the string be ended with '\n', i.e. a newline character. But it looks like the 'n' got stripped out. Also, the second cat statement in the example above ends with a '}' instead of a ')'. So I suspect that the example should have looked like this
<<label=fig1, fig=FALSE, result = tex>>=
pdf("fig1.pdf", width = wid, heigth = hei)
plot(1:10)
dev.off()
cat("\\begin{figure}\n")
cat("\\includegraphics[width = ", wid, ", height = ", hei, "]{proj1-fig1}\n")
cat("\\end{figure}\n")
@
If I have got any of this wrong, I am sure someone will come along and correct me.
Hope this is helpful,
Dan
Daniel Nordlund
Bothell, WA USA
-----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 4:18 PM To: Nordlund, Dan (DSHS/RDA) Cc: r-help at r-project.org Subject: Re: [R] sweave question thanks dan. I'll try the double slash on both sides when I get back later tonight. In the solution on the net, he only had the backslash on the end quotes ( well there was a backslash at the beginning but that's a real backslash needed for latex commands ). I'll let you know how it works out. thanks again. On Wed, Feb 13, 2013 at 6:50 PM, Nordlund, Dan (DSHS/RDA) < NordlDJ at dshs.wa.gov> wrote:
-----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 about 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'. Hope this is helpful, 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
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
Other people in this thread have explained the rule of double backslashes. I just want to add one more comment: That clever solution was in 2009 when knitr has not come out, and I have used this picture several times to show how much I dislike it: http://yihui.name/en/2012/06/enjoyable-reproducible-research/ With knitr, you can easily write your big code chunk (with lots of "cats" jumping around) in a much more compact way: <<figthree, fig.pos="ht!", fig.align="center", out.width="5in", out.height="5in", fig.cap="The resulting unpruned regression tree of the California real estate data">>= plot(cal.tree,uniform=TRUE,compress=TRUE,margin=0.0) text(cal.tree, all=TRUE,fheight=1.0,fwidth=1.0) @ Note the number "5" alone is not a legal LaTeX length; you should add the unit such as "5in" or "5cm", etc. This chunk in knitr will generate everything for you: - the figure environment - center the figure - add the figure position ht! - add the figure caption - add figure width and height - add the figure label fig:figthree You, as the author, only write the R code (instead of both R and LaTeX); other people, as the readers, only see the R code. Everything else should go behind the scene. Regards, Yihui -- Yihui Xie <xieyihui at gmail.com> Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA
On Wed, Feb 13, 2013 at 4:59 PM, Mark Leeds <markleeds2 at gmail.com> wrote:
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 about 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}") @ [[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
An embedded and charset-unspecified text was scrubbed... Name: not available URL: <https://stat.ethz.ch/pipermail/r-help/attachments/20130214/11ed95ce/attachment.pl>
Hi Mark I need pdfs for Sweave etc, my colleagues want wmfs or the ilk and for publishing eps/tiff so I just add a section for each with in the 'print' chunk Duncan
At 02:49 15/02/2013, you wrote:
Hi Dan, Duncan and Yihui: I apologize for my late response.
Dan: Your backslash explanation was great but yes, I think there was a
transmission problem in the link I
sent. I made the changes and everything compiled without a glitch but the
actual tex file ends
up with many begin{Schunk} and end{Schunk} statements and somehow I think
this causes the pdf to
not get put in the document. But atleast I learned I now understand the
backslash issue and why my attempt wasn't working. So, you solved the
problem that I had but another different issue now lurks
but, given below, I'm gonna let it go. Thanks.
Duncan: Your solution worked. I'm not sure why I didn't think of
seperating the two processes.
That was a nice idea. I guess I got caught up in the cat method once I saw
it. Thanks.
Yihui: I'm starting to learn Rstudio by going through the recent book out
on it. There's a section
about knitR in the book so I hope to go over that and then turn to many of
your other documents.
Thanks for recommending knitR ( I loved the kitten statement at the link,
especially since I have cats ) and it is something I plan on learning, in
addition to 20 other thousand things on my list of "to be learned".
Mark
On Wed, Feb 13, 2013 at 9:00 PM, Yihui Xie <xie at yihui.name> wrote:
Other people in this thread have explained the rule of double backslashes. I just want to add one more comment: That clever solution was in 2009 when knitr has not come out, and I have used this picture several times to show how much I dislike it: http://yihui.name/en/2012/06/enjoyable-reproducible-research/ With knitr, you can easily write your big code chunk (with lots of "cats" jumping around) in a much more compact way: <<figthree, fig.pos="ht!", fig.align="center", out.width="5in", out.height="5in", fig.cap="The resulting unpruned regression tree of the California real estate data">>= plot(cal.tree,uniform=TRUE,compress=TRUE,margin=0.0) text(cal.tree, all=TRUE,fheight=1.0,fwidth=1.0) @ Note the number "5" alone is not a legal LaTeX length; you should add the unit such as "5in" or "5cm", etc. This chunk in knitr will generate everything for you: - the figure environment - center the figure - add the figure position ht! - add the figure caption - add figure width and height - add the figure label fig:figthree You, as the author, only write the R code (instead of both R and LaTeX); other people, as the readers, only see the R code. Everything else should go behind the scene. Regards, Yihui -- Yihui Xie <xieyihui at gmail.com> Phone: 515-294-2465 Web: http://yihui.name Department of Statistics, Iowa State University 2215 Snedecor Hall, Ames, IA On Wed, Feb 13, 2013 at 4:59 PM, Mark Leeds <markleeds2 at gmail.com> wrote:
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 about 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}")
@
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide
http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.