knitR and user defined libraries ("could not find function")

I am developing a package. My functions work fine standalone. But when I use them in my Latex document in code chunks and use "Compile PDF", it gives the error "could not find function.." I included "?myfun1" in a code chunk, it shows help on console. Also, when I run code chunks on the Latex document, it works fine. I want to get some sample runs of my functions on the pdf document.

Did you source your functions in the LaTeX document or did you use library(mypackage) in that same document?
When you knit a document it will be done in a R environment that is different from the one that you get when using the standard RStudio environment.

Thanks, but that's what I did after googling. As a last step I even tried using " library(mypackage)" inside my code chunks too.

Please make it clearer for us!
For compactness sake I assume your are working on package mypackage with a function myfunction.

I am developing a package. My functions work fine standalone.
I understand that to mean that after Session | Restart R (Ctrl+Shift+F10) the line

mypackage::myfunction(any parameters)

does what you would like it to do? If not, there could be an error in your package

But when I use them in my Latex document in code chunks and use "Compile PDF", it gives the error "could not find function.."
I assume that the function that is not found, belongs to your package.
Create a small example of your document: one that compiles correctly without your function but fails when you insert your function. Show both the document and the error messages in your reply. (Enclose the document in four backticks /backquotes ( ```` ) to show the backticks in your document.)

Also, when I run code chunks on the Latex document, it works fine. I want to get some sample runs of my functions on the pdf document.
I am sure what you mean by this.

Please let us know. And if you solved the problem in the meantime, indicate this for this thread.

I finally figured out my mistake. I didn't have the line #'@export in my function (R script) files, so roxygen could not generate proper entries in the NAMESPACE file. Basically I didn't have export{myfunction1} line in NAMESPACE file.

Thanks HanOostdijk for trying to help.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.