当我使用knitr和Rscript与opts_chunk时执行停止

时间:2021-06-17 04:48:09

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

我在Mac OS中写了一个bash脚本,它接受我的.rnw文件,编织它然后制作.pdf。要编织我的文件,我正在使用该命令

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

一切正常。但是,我不想在我的最终文档中使用##字符。我使用R提示符测试了以下命令

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

他们给了我我想要的东西:R命令输出中没有注释的文件。但如果我试着跑

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?

结果。我究竟做错了什么?如何使用命令行在knitr中为我的块请求选项?

1 个解决方案

#1


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:

我不确定当他说“在这里提出答案”时,一回意味着什么,但这里是邓肯默多克的R帮助回答:易辉批准了:

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

这看起来像一个bash问题:它似乎是用空字符串替换$ set。使用适当的引用或转义告诉它不要这样做。 (我认为在命令周围使用单引号会起作用;你需要在它内部加双引号。)


And Yihui added:

并且Yihui补充说:

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

是的我相信这就是问题所在。同样的问题在这里问:https://github.com/yihui/knitr/issues/162#issuecomment-9017997

#1


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:

我不确定当他说“在这里提出答案”时,一回意味着什么,但这里是邓肯默多克的R帮助回答:易辉批准了:

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

这看起来像一个bash问题:它似乎是用空字符串替换$ set。使用适当的引用或转义告诉它不要这样做。 (我认为在命令周围使用单引号会起作用;你需要在它内部加双引号。)


And Yihui added:

并且Yihui补充说:

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

是的我相信这就是问题所在。同样的问题在这里问:https://github.com/yihui/knitr/issues/162#issuecomment-9017997