-
-
Notifications
You must be signed in to change notification settings - Fork 873
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cache of generic objects? #138
Comments
This seems weird. I cannot reproduce the error. Did you use an interactive R session or run R non-interactively via |
I am using version 0.2.2
and I am running the script via a bash script. R -q -e "library('knitr');knit('$FILENAME')" Sang On Wed, Feb 15, 2012 at 3:51 PM, Yihui Xie <
|
I see the problem now. I will investigate it soon. Thanks! |
The reason is assign('fext3', fext3, envir = knit_env()) A second solution has the same idea, but tackles the problem using a custom chunk hook (looks complicated but it is a good case for learning chunk hooks): \documentclass{article}
\begin{document}
<<>>=
library(methods)
read_chunk('extsource.R')
knit_hooks$set(cache.obj = function(before, options, envir) {
if (!before) {
## after the chunk has been evaluated, copy an object to knitr's environment
obj <- options$cache.obj
assign(obj, get(obj, envir = globalenv()), envir = envir)
return(NULL)
}
})
@
% add an option cache.obj here
<<ext1, cache=TRUE, cache.obj=fext3>>=
@
<<ext2, cache=TRUE, dependson=ext1>>=
@
Run functions:
<<>>=
fext1('a')
fext2('b')
fext3('c')
@
\end{document} A slight advantage of using the hook is there will not be an additional line of code appearing in your source code, but even with the first approach, you can get rid of that line in the output (http://yihui.name/knitr/demo/output/). |
Thank you for your clear explanation. Thanks again. Sang On Wed, Feb 15, 2012 at 6:34 PM, Yihui Xie <
|
Hi, in fact I was trapped by myself and gave you a nasty solution. It turns out to be very easy to keep track of global objects such as those created by Note you need |
Great news! On Sat, Mar 10, 2012 at 8:05 PM, Yihui Xie <
|
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary. |
Hi Yihui,
Thank you for such great work. knitr is so useful and much better than any of "sweave" variations.
Following is the problem I am having.
I have an external source file (extsource.R) with some function definitions.
One of the functions is a generic object (setGeneric)
The steps I have tried:
cache
directory<<ext1,cache=TRUE>>=
<<ext1,cache=TRUE>>=
,<<ext1,cache=FALSE>>=
<<ext1,cache=TRUE>>=
It seems to me that the cache does not save generic objects.
The text was updated successfully, but these errors were encountered: