How to comment out lines with knitr::spin

拈花ヽ惹草 提交于 2019-12-07 17:47:16

问题


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.


回答1:


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



回答2:


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


来源:https://stackoverflow.com/questions/17664401/how-to-comment-out-lines-with-knitrspin

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!