Knitr: R package check error, object 'opts_chunk' not found

I am getting the following error when checking my R package

> Error: could not find function "locdata"
> Execution halted
> when running code in ‘DFSurvey.Rnw’
>   ...
> 
> > opts_chunk$set(cache = TRUE, fig.path = "DFSurveyImages/", dev = "pdf")
> 
>   When sourcing ‘DFSurvey.R’:
> Error: object 'opts_chunk' not found
> Execution halted

Yihui Xie (knitr developer) said that was because in RStudio, knitr was not set as the method for weaving .Rnw files, https://groups.google.com/forum/?fromgroups#!topic/knitr/9672CBbc8CM. I have knitr set in both the tools and build options, in the R package DESCRIPTION file I have:

VignetteBuilder: knitr
Suggests: knitr

and in the vignette I have:

%\VignetteEngine{knitr}
%\VignetteDepends{knitr,xtable,TSP}

When I use compile the pdf in RStudio or use knit("KNITR.Rnw"), it compiles correctly. When I check the package, I get the above errors for each vignette. I even put

require(knitr)

before my opts_chunk$set statement. That did not help. I have also run the check from the command line and gotten the same error. Thank you for any help.

Knitr is a useful package. I run long simulations in vignettes, and the cache makes it possible to correct errors without running the simulations over each time. It does not have the problem of trying to find the Sweave.sty file either.

Here is my sessionInfo()

> R version 3.0.0 (2013-04-03)
> Platform: x86_64-apple-darwin10.8.0 (64-bit)
> 
> locale:
> [1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
> 
> attached base packages:
> [1] tcltk     grid      stats     graphics  grDevices utils     datasets  methods  
> [9] base     
> 
> other attached packages:
>  [1] DualFrame_0.5         xtable_1.7-1          TSP_1.0-7            
>  [4] maptools_0.8-23       lattice_0.20-15       foreign_0.8-53       
>  [7] spsurvey_2.5          sp_1.0-9              stringr_0.6.2        
> [10] sqldf_0.4-6.4         RSQLite.extfuns_0.0.1 chron_2.3-43         
> [13] gsubfn_0.6-5          proto_0.3-10          RSQLite_0.11.3       
> [16] DBI_0.2-7             knitr_1.2             gpclib_1.5-5         
> 
> loaded via a namespace (and not attached):
> [1] deldir_0.0-22  digest_0.6.3   evaluate_0.4.3 formatR_0.7    MASS_7.3-26   
> [6] rgeos_0.2-17   tools_3.0.0   

put library(knitr) before this opts_chunk$set(cache = TRUE, fig.path = "DFSurveyImages/", dev = "pdf")

Knitr: could not find function %>% error - R Markdown, I am relatively new to using R, and would like to knit my outputs from my RStudio markdown to a html/pdf. I have installed and loaded all packages I am using in the console (this includes Knitr, dplyr and magrittr… I press the "knit" buttom on the top left of the screen, I get the following error: In addition: Warning message : Stack Overflow Public questions and answers; Teams Private questions and answers for your team; Enterprise Private self-hosted questions and answers for your enterprise; Talent Hire technical talent

You have to load the knitr library first, try this:

```{r setoptions, echo=FALSE}
library(knitr)
opts_chunk$set(cache = TRUE, fig.path = "DFSurveyImages/", dev = "pdf")```

6 Deciphering Common R Errors, For references on errors, check out the following two links by Noam Ross here This error usually occurs when a package has not been loaded into R via This error usually occurs when your R Markdown document refers to an object that� Package ‘knitr’ June 23, 2020 Type Package x An R object. Typically a character string, or an object which can be converted (FALSE) and carefully check

For a knitr vignette that you can compile using knit() or with the "Compile PDF" button in RStudio, but that gets an

Error: object 'opts_chunk' not found Execution halted

error when checking or building the package, the package check code is not recognizing that your .Rnw file should be knited and not Sweaveed. Check that you have the following:

  1. The vignettes are in the vignette directory, if you have R 3.0.0 or higher (this was the solution to this post), cran.r-project.org/doc/manuals/r-devel/R-exts.html#Non_002dSweave-vignettes

  2. Include %\VignetteEngine{knitr::knitr} in the vignette metadata, yihui.name/knitr/demo/vignette/

  3. Specify VignetteBuilder: knitr in the package DESCRIPTION file, and

  4. Add Suggests: knitr in DESCRIPTION if knitr is needed only for vignettes

If that does not work add a require(knitr) statement before you set your global options in opts_chunk(), as Ben Bolker, Yuhui and Tyler Rinker suggested.

If in RStudio: In BOTH the Build configuration and Tool options, set the Sweave option to knitr, www.rstudio.com/ide/docs/authoring/rnw_weave

[PDF] Package 'knitr', SystemRequirements Package vignettes based on R Markdown v2 or An R object. chunk labels of the current document to determine which labels are no longer present, error = getOption("knitr.graphics.error", TRUE). let me try a very wild guess: this all started because you tried to compile a .Rmd file, and RStudio told you that in order to do that, it had to install up-to-date versions of packages rmarkdown, knitr, etc.

Changing the Sweave option to knitr in the Tools options worked for me.

cannot find objects when knitting in RStudio � Issue #445 � yihui/knitr , The R chunks work great and produce objects in my workspace. When I knit r x (there are backticks around this), I get an error that x cannot be found, but I Facing the same issues with R markdown and the Knitr package. Automated checking. An important part of the package development process is R CMD check.R CMD check automatically checks your code for common problems. It’s essential if you’re planning on submitting to CRAN, but it’s useful even if you’re not because it automatically detects many common problems that you’d otherwise discover the hard way.

Error in R Markdown : Knit HTML (Object Not found ) � Issue #463 , When I try to knit HTML document, it gives an error (object not found). utils datasets methods base other attached packages: [1] knitr_1.10.5 Error in R Markdown : Knit HTML (Object Not found ) yihui/knitr#1065 But when I try to use mutate on the result of the last computation, I get the following error:. Knitr: R package check error, object 'opts_chunk' not found. 9. non-evaluated vignettes with knitr::rmarkdown_notangle. 7. Enforce PDF package vignette with knitr. 19.

15 Common Problems with rmarkdown (and some solutions , A book created for a 3 hour workshop on rmarkdown. until they work, then move on. knit the document regularly to check for errors. Then The easiest way is to do Cmd+Shift+F, which opens up a global search in your The error message. How to solve. 15.13 I want to include inline R code verbatim to show an example. When we are talking about table generators in R, knitr's kable() function is usually a popular choice because of its ultimate simplicity. Unlike those powerful table rendering engines such as xtable, the philosophy behind knitr::kable() is to make it easy for programmers to use.

Knitr with R Markdown, When you process the R Markdown document with knitr, each of the code in there: warning=FALSE and message=FALSE suppress any R warnings or fig. path='Figs/' makes it so the figure files get placed in the Figs subdirectory. In addition to the chunk options, there are also package options, set with something like:. The printr package is a companion to knitr containing printing methods for some common objects like matrices and data frames. Users only need to load this package to get attractive printed results. A major factor to consider (which has been considered in printr) when defining a printing method is the output format. For example, the table syntax

Comments
  • Can you provide a minimal working example that replicates this problem?
  • @TylerRinker , why did you delete your answer ("add library('knitr') to your code before calling opts_chunk$set()")? Seemed like a good idea to me ...
  • They say they used require before the pots$chunk. I'll undelete it for now.
  • I think require('knitr') or library(knitr) right before opts_chunk$set() should work. Please provide a minimal reproducible example if it does not work.
  • You were asked multiple times to provide the minimal working example yet ignored this. In the future when asked for a minimal example if you provide it you'd likely have had your answer in a fraction of the time.
  • @Ben, I did have it in but the result was the same, opt_chuck not found. I made a simple example to run. I did put in the require statement. Here is the dropbox link, dropbox.com/l/W0iYiBxwxHBwUB7Er43la9. (I was not such how to attach code.) I made a package using --no-vignettes and the .zip file are the files I used to make the test package. Sorry, about the delayed response I thought I had set up to be notified when I get responses. Mark
  • The package really isn't needed. Can you post the contents of the vignette you're making? PS GitHub would be an excellent way to share this. IF you haven't started using it already I'd suggest you give it a try.
  • Thanks for the advice about GitHub, but just showing the vignette is not the problem. The vignette compiles or knits just fine on in its own in RStudio or using knit(). The problem is that is will not compile in a package. I made the RTestPkg as a simple example, not as a useful code project to share.
  • I had the same issue, and this answer seems to be the key addition that fixed the problem. I had the exact error message, but with a .Rmd vignette in the inst/doc/ directory instead of a .Rnw.