Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

6 Answers 6

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

share|improve this answer
    
Welcome to TeX.sx! –  Peter Jansson Apr 22 '13 at 22:15
add comment

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

share|improve this answer
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
    
Why is your comment being printed out? –  Mien May 4 '12 at 15:42
add comment

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.