6

I'm quite new to both R and LaTeX, but I'm trying to work with both, using Sweave.

I know you can comment in R using # and in LaTeX by using %. However, when I write a #comment in an R-chunk (between <<>>= and @) I get an error:

Error: 'filename.Rnw' is not ASCII and does not declare an encoding

Execution halted

I also tried it with % but that obviously didn't work (same error btw).

How can I put a comment in an R-chunk?

Mien
  • 171
  • 1
  • 3
  • 7

6 Answers6

3

Late in the game, but if you copied text into the Rnw file from another document type (e.g., Word), symbols such as " are coded differently. Retype any symbols that you copied over (in entire document, not just R chuck).

2

Try keep.source=TRUE in Sweave options. Or \SweaveOpts{keep.source=TRUE} in the preamble. Source: Sweave customization

Ariel
  • 1,569
  • 1
  • 12
  • 27
2

I think the real problem is that you have non-ASCII characters in your document, but you did not declare the encoding. Sweave was looking for a line like \usepackage[foo]{inputenc} in your source document; see ?Sweave for details.

Yihui Xie
  • 2,805
1

I use comments in R chunks without a problem. Here is mwe.

\documentclass{article}
\begin{document}
Test of Sweave without comment
<<>>=
2+2
@
Test of Sweave with single \# for comment
<<>>=
2+2
# Comment
@
\end{document}

And the output. enter image description here

Reference this Include Sweave input as verbatim for example of getting the exact Sweave chunk in your output. (including the <<>>= and @)

0

As suggested by user29484, this sometimes occurs when you cut and paste into the Sweave document. It has happened to me several times when cutting and pasting from a pdf file in particular.

Text Wrangler (for Mac) has a tool called Zap Gremlins... in the Text menu. I open the Rnw file in Text Wrangler, apply this tool save the file and re-open in RStudio and am back in business.

0

I would seriously consider using knitr rather then Sweave. It is very easy to use inside of RStudio or through your own editor. I think it does a better job with options like this and more.

http://yihui.name/knitr/

https://www.rstudio.com/ide/docs/authoring/overview