Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
512 views
in Technique[技术] by (71.8m points)

r - Knitr: opts_chunk$set() not working in Rscript command

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")
```

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")'

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")'

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 kniting with Rscript at the command line?

Thanks.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I think you need to add

library("knitr")

to the chunk (you might want to set message=FALSE in the chunk options for that chunk).

The problem is that when you do

Rscript -e 'knitr::knit("myfile.Rmd")'

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.

  • Using knitr::opts_chunk might work too ...
  • as you suggested, so does Rscript -e 'library("knitr"); knit("myfile.Rmd")'

When you click the button in RStudio, RStudio automatically loads knitr in the environment in which it runs knit().


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...