Function can not be found while knitting (even if in NAMESPACE and works fine while not knitted)

Hi!

I am building an R package with the help of Hadley Wickham book. However I encounter an issue while knitting a vignette. I get this error (I rewrote the names of the vignette and function):

"Quitting from lines 208-212 (nameofthevignette.Rmd)
Error in foo(argument1, argument2, argument3 :
"foo" function can not be found
Calls: ... handle -> withCallingHandlers -> withVisible -> eval -> eval
Execution halted"

It appears that I have a problem with the "foo" function and that it can not be found. However, it is exported and listed in the NAMESPACE file and the "foo" function works if I run it. The problem appears only when knitting the vignette using it. In this vignette I also call other functions from my package before calling the "foo" function and there is no problem with them.

Do you have any idea of what can cause such an issue?

Thank you for your help!

This may be related to the fact that RMarkdown runs in a separate R session, and your package may not be attached at the time of the foo function call.

A quick check would be to refer to foo function via double colon - your_package::foo() - if it works you are missing a library call, if it does not there is something wrong with your function.

I have already tried and it does not work so I don't think that the problem comes from there. In fact, in my vignette other function calls on lines before the "foo" call work... The issue is that the function works if I use it outside the vignette or even if I knit only the chunk related to it in my vignette but does not work if I want to knit the entire vignette.
Thank you for your help!

Well, it was not quite a rocket science... You may also check whether the function is not by chance listed as internal (but then it would not be exported in your NAMESPACE, so likely not) and whether you don't have to rebuild your package.
This would depend on the way you are knitting your vignette - if it is via R CMD Check then it is run against your current source, but if it is via regular Knit button on your Rmd document it would be ran against your currently installed version (which may be lower).

I don't know how to check that a function is an internal one otherwise than checking the NAMESPACE file. I can tell that it is listed in the NAMESPACE file so I guess it is not an internal function but if you know a better way to check this...
The error appears when I knit the vignette using the regular Knit button on the .Rmd document so it is possible that my currently installed version is lower. I have another problem with another vignette, so I keep you updated when I succeed in rebuilding a new version of the package

That may be it - consider running a package check (either by clicking the little green checkmark button on the "build" pane or via devtools::check() in your console window).

It will (among other things) try to rebuild the package vignette using the current codebase. Rebuilding the vignette is one of the first steps / it will then continue to stuff like running your unit tests etc.

Very handy & highly recommended.

Yes I have already checked my package using devtools::check() (I often do it to check that everything is alright with my code and documentation) and I have no warnings or errors.

I have rebuilt my package but I still get the same error when individually knitting the vignette. Whereas, as I said, no error when devtools::check() or devtools::build() or running the vignette chunk by chunk

Curiouser and curiouser... In that case I would try "install and restart" (the little hammer like icon on the left of the Build pane) if I were in your place. That should ensure you have the last version actually installed (in your R packages directory), as opposed to development version as a project.
If that does not make your vignette behave as it should then I don't know what would.