# How to comment in an R-chunk?

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?

-

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).

-
Welcome to TeX.sx! –  Peter Jansson Apr 22 '13 at 22:15

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

-

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.

-

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.

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

-
Why is your comment being printed out? –  Mien May 4 '12 at 15:42

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.

-

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

-