Bryan Hanson
Bryan Hanson

Reputation: 6213

How to comment out lines with knitr::spin

When using spin in package knitr, how does one simply comment things out to make them invisible to spin? roxygen style lines (#') are taken as lines to appear in the report. The usual R comment # is taken as an R comment and appears in a code block. Lines that are just text, with no special character at the beginning, cause an error. Lines beginning withe the LaTeX comment % cause an error. However, plain lines that follow the start of a chunk are taken to be part of the chunk and appear in the code block (#+ or #-). So is there a character/symbol that functions to mark a comment line in the true sense of the word?

EDIT: If it has to be invented, the LaTeX comment character % would be quite handy. Just saying.

Upvotes: 3

Views: 2526

Answers (2)

Yihui Xie
Yihui Xie

Reputation: 30114

Update: I added the feature of comments for spin() in knitr 1.3.2 (see its Github repos for installation instructions). Now you can

#' normal text
#' 

# /* a comment here
runif(10)
# and here */

rnorm(5)
#' text continues

Old answer:

That depends on the output format. For example, for LaTeX, you use %:

#' normal text
#' 
#' % a LaTeX comment here
rnorm(5)
#' text continues

For HTML, it is <!-- -->:

#' normal text
#' 
#' <!-- an HTML comment here -->
rnorm(5)
#' text continues

Upvotes: 3

Roman Luštrik
Roman Luštrik

Reputation: 70643

A long winded solution would be to "echo and eval out" the offending line via the corresponding echo and eval arguments.

#' Regular text

#+ first_chunk, echo = -2, eval = -2
rnorm(10)
runif(20)

#' Text after the chunk

Upvotes: 0

Related Questions