4

I wrote a bash script in Mac OS that takes my .rnw file, knit it and then makes a .pdf. To knit my file, I'm using the command

Rscript -e "library(knitr); knit('file.rnw', encoding='utf8')"

and everything works fine. However, I don't want the ## characters in my final document. I tested, using R prompt, the following commands

library(knitr)
opts_chunk$set(comment=NA)
knit('file.rnw', encoding='utf8')

and they give me exactly what I want: the document without comments in the R commands output. But if I try to run

Rscript -e "library(knitr); opts_chunk$set(comment=NA); knit('livro.rnw', encoding='utf8')"

I get

Error: could not find function "opts_chunk"
Execution halted

as result. What am I doing wrong? How can I ask for options for my chunk in knitr using the command line?

Marcus Nunes
  • 851
  • 1
  • 18
  • 33
  • [cross-posted in R-help](https://stat.ethz.ch/pipermail/r-help/2013-March/350285.html) and answered there; I hope someone can move the answer here. – Yihui Xie Mar 26 '13 at 18:19
  • You can instead put the following in your RMD file for example... `knitr::opts_chunk$set(echo=TRUE)` inside an `r chunk`. – agent18 Feb 28 '19 at 19:40

1 Answers1

2

I'm not sure what Yihui meant when he said "move the answer here" but here is Duncan Murdoch's R-help answer that Yihui ratified:

That looks like a bash problem: it appears to be replacing $set with a blank string. Use appropriate quoting or escaping to tell it not to do that. (I think using single quotes around the command will work; you'll need double quotes within it.)


And Yihui added:

Yes I believe that was the problem. Same question asked here: https://github.com/yihui/knitr/issues/162#issuecomment-9017997

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
IRTFM
  • 258,963
  • 21
  • 364
  • 487