Knitr:opts_chunk $ set()在Rscript命令中不起作用 [英] Knitr: opts_chunk$set() not working in Rscript command
问题描述
我正在使用knitr从Rmd创建一个markdown文件,并且在.Rmd脚本的顶部设置了以下选项以隐藏所有结果和绘图:
I'm using knitr to create a markdown file from Rmd and I have the following option set at the top of my .Rmd script to hide all results and plots:
```{r, echo=FALSE}
opts_chunk$set(results="hide", fig.show="hide")
```
当我在RStudio中单击编织HTML"按钮时,此方法有效-我得到的输出中没有结果和图形.但是,如果我从命令行运行:
When I hit the Knit HTML button in RStudio, this works - I get output without the results and figures. But if I run from the command line:
Rscript -e 'knitr::knit("myfile.Rmd")'
似乎未读取opts_chunk$set()
行,并且在.md输出中获得了结果和图.我通过在Rscript命令中指定以下选项来解决此问题:
It appears the opts_chunk$set()
line isn't read, and I get results and plots in my .md output. I've worked around the problem by specifying these options in the Rscript command:
Rscript -e 'library(knitr); opts_chunk$set(results="hide", fig.show="hide"); knit("myfile.Rmd")'
但是我宁愿让所有选项都从我正在使用的文件中读取,而不是在命令行中指定.在命令行中使用Rscript knit
进行操作时,如何在.Rmd文件中读取选项?
But I'd rather keep all the options read from the file I'm using rather than specified at the command line. How do I get the options read in the .Rmd file when knit
ing with Rscript at the command line?
谢谢.
推荐答案
我认为您需要添加
library("knitr")
(该块(您可能要在该块的块选项中设置message=FALSE
).
to the chunk (you might want to set message=FALSE
in the chunk options for that chunk).
问题在于,当您这样做
Rscript -e 'knitr::knit("myfile.Rmd")'
您实际上并没有附加knitr
软件包,这意味着它不在函数的搜索路径中,这意味着R找不到opts_chunk
对象.
you're not actually attaching the knitr
package, which means it isn't in the search path for functions, which means that R can't find the opts_chunk
object.
- 使用
knitr::opts_chunk
可能也可以... - 如您所建议,
Rscript -e 'library("knitr"); knit("myfile.Rmd")'
- Using
knitr::opts_chunk
might work too ... - as you suggested, so does
Rscript -e 'library("knitr"); knit("myfile.Rmd")'
当您单击RStudio中的按钮时,RStudio将在运行knit()
的环境中自动加载knitr
.
When you click the button in RStudio, RStudio automatically loads knitr
in the environment in which it runs knit()
.
这篇关于Knitr:opts_chunk $ set()在Rscript命令中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!