[R] Sweave: printing an underscore in the output from an R command

Duncan Murdoch murdoch.duncan at gmail.com
Mon Sep 2 22:02:07 CEST 2013


On 13-09-02 3:18 PM, David Epstein 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.

The final result has a single backslash.  The print() function doubles 
it so you can tell it from an escape of the next letter.  If you use 
cat() instead of the implicit print() you'll see the text that is there.
>
> 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


You need to give more details, e.g. what actually appeared in the .tex 
file and in what context, if you want help with this.

Duncan Murdoch



More information about the R-help mailing list