[R] knitr: Was previously " Sweave: printing an underscore in the output from an R command"

David Epstein David.Epstein at warwick.ac.uk
Tue Sep 3 00:01:25 CEST 2013


Dear Yihui
Thanks very much for drawing my attention to knitr, which I had not heard of before. Also thanks for pointing out the bug in Sweave, which I don't fully understand, but I don't want to spend time and effort on understanding it. So I hope you will find time to report the bug. I was pretty sure there was a bug somewhere that was preventing me from doing what I wanted to do in Sweave, but I misdiagnosed the source of the problem.

I notice you didn't use print() or cat() in your short program for knitr. Is it the case that it's necessary to use print() or cat() with \Sexpr in Sweave, but unnecessary in knitr?

I'll stick to Sweave for my current project, and try out knitr on my next project. I would welcome a list of documents about knitr that I should download, so as to make it as easy as possible to get started. I don't want to understand the internals of knitr, but I am interested in any documents on knitr, written by you or by others, directed at the user, rather than at programmers of packages.

Is it convenient to use vi(m) to produce knitr source? Can vi(m) be integrated into the knitr package? My experience with editors designed specially to work with particular products (like the built-in editor for TeXWorks on the Mac) do not have the power of vi(m) and emacs, and I require this power.

@Duncan: thanks for indicating the use of cat() instead of print(). However, due to the bug in Sweave pointed out by Yihui, replacing print by cat didn't help me.

Thanks
David




On 2 Sep 2013, at 21:11, Yihui Xie wrote:

> I think Thierry meant gsub("_", "\\\\_", version$platform); he just
> typed too quickly. The point is to escape _ using \, but then people
> are often trapped in the dreams of dreams of dreams of backslashes
> like the movie Inception. And then due to a long-standing bug in
> Sweave for \Sexpr{} (sorry I forgot to report to R core), you will be
> so confused that you can never wake up and come back to the reality.
> 
> Dream level 1: when you need a backslash in a character string, you
> need "\\", which really means \; you think "\\_" should be good, but
> no --
> 
> Dream level 2: when you need one literal \ in a regular expression as
> the replacement expression, you need \\
> 
> Combine the two levels of dreams, you end up with "\\\\_". \\\\ in R
> really means \\, which really means \ in regular expressions.
> 
> Now you are good at the regular expression level, but Sweave comes and
> bites you, and that is due to this bug in the regular expression in
> Sweave Noweb syntax:
> 
>> SweaveSyntaxNoweb$docexpr
> [1] "\\\\Sexpr\\{([^\\}]*)\\}"
> 
> It should have been "\\\\Sexpr\\{([^}]*)\\}", i.e. } does not need to
> be escaped inside [], and \\ will be interpreted literally inside [].
> In your case, Sweave sees \ in \Sexpr{}, and the regular expression
> stops matching there, and is unable to see } after \, so it believes
> there is no inline R expressions in your document.
> 
> BTW, knitr does not have this bug and works well in your case:
> 
> \documentclass{article}
> \begin{document}
> \Sexpr{sub("_", "\\\\_", version$platform)}
> \end{document}
> 
> Regards,
> Yihui
> --
> Yihui Xie <xieyihui at gmail.com>
> Web: http://yihui.name
> Department of Statistics, Iowa State University
> 2215 Snedecor Hall, Ames, IA
> 
> 
> On Mon, Sep 2, 2013 at 2:18 PM, David Epstein
> <David.Epstein at warwick.ac.uk> wrote:
>> Dear Thierry,
>> 
>> Your suggestion doesn't work on my version of R. Here's what I get
>>> gsub("_", "\_", print(version$platform)
>> Error: '\_' is an unrecognized escape in character string starting ""\_"
>>> print(gsub("_", "\_", version$platform))
>> Error: '\_' is an unrecognized escape in character string starting ""\_"
>> 
>>> sub("_", "\\_", version$platform)
>> [1] "x86_64-apple-darwin10.8.0"
>> Sweave does not evaluate this expression when \Sexpr is applied and a tex error results
>> 
>>> sub("_", "\\\_", version$platform)
>> Error: '\_' is an unrecognized escape in character string starting ""\\\_"
>> Error message from R
>> 
>>> sub("_", "\\\\_", version$platform)
>> [1] "x86\\_64-apple-darwin10.8.0"
>> R evaluates this. However, the above examples indicate a deficiency/possible bug in the command sub, because sub does not seem to be able to output an expression with a single backslash.
>> 
>> I tried the previous version as follows in my .Rnw document
>> \Sexpr{print(sub("_", "\\\\_", version$platform))}
>> When Sweave is run, this expression is evaluated to illegal LaTeX
>> 
>> David.
>> 
>> 
>> 
>> 
>> On 2 Sep 2013, at 16:47, ONKELINX, Thierry wrote:
>> 
>>> You have to escape the underscore
>>> 
>>> \Sexpr{gsub("_", "\_", print(version$platform))}
>>> 
>>> Best regards,
>>> 
>>> Thierry
>>> 
>>> ________________________________________
>>> Van: r-help-bounces at r-project.org [r-help-bounces at r-project.org] namens David Epstein [David.Epstein at warwick.ac.uk]
>>> Verzonden: maandag 2 september 2013 17:38
>>> Aan: r-help at r-project.org
>>> Onderwerp: [R] Sweave: printing an underscore in the output from an R command
>>> 
>>> I am working with Sweave and would like to print out into my latex document the result of the R command
>>> version$platform
>>> So what I first tried in my .Rnw document was \Sexpr{print(version$platform)}.
>>> 
>>> However, the output from this command is the string "x86_64-apple-darwin10.8.0" (without the quotes). This contains an underscore, which is a special character in tex and so I get an error message from latex.
>>> 
>>> I can get round this by using sub to replace underscore with a space, but I would like to know how to print the underscore if I really wanted to do so.
> 



More information about the R-help mailing list