0

Can anybody help me understand how to turn off caching in LyX/knitr when I submit the following type of function calls to R via knitr:

\Sexpr{rnorm(1, 6, 1.1)}

or in a chunk:

<<echo=FALSE, cache=FALSE>>=
rnorm(1, 6, 1.1)
@

Each time I process the document I just get the exact same value as the first time. I know this must be simple, but it's driving me nuts. Thanks a lot, -Gary

g5m
  • 21
  • 2
  • Are you using set.seed anywhere in the document? – Dason Aug 02 '12 at 16:44
  • Thanks for asking - no I'm not. This document is just a test, so there's really nothing else in it besides this simple command. -G – g5m Aug 02 '12 at 16:57
  • Do you have the same problem if you just compile everything without using Lyx? I can't reproduce that issue using RStudio. – Dason Aug 02 '12 at 17:21
  • That's interesting. I can recreate this in RStudio by creating a new Sweave document processed via knitr, and of course the rnorm() function in the console returns a unique value each time as expected. If I change one of the args to the function, it gives a new number the first time, and the same number each subsequent time the document is created. So it's a knitr issue, not a LyX issue - that's useful information. – g5m Aug 02 '12 at 17:38
  • I cannot reproduce it with LyX 2.0.4 + knitr 0.7. – Yihui Xie Aug 04 '12 at 00:45

1 Answers1

2

Solved. There was a set.seed() call in my .Rprofile. When I remove it, this problem goes away in LyX. It threw me because in an R session, only the first call to rnorm() would be determined by the set.seed(), not subsequent calls to rnorm(), whereas from LyX a new session gets created each time a document is created. Since my example only had one call, I always got the same value. Thanks everybody - especially Yi Hui for making knitr.

g5m
  • 21
  • 2
  • 1
    actually subsequent calls are also determined by that seed; you can run `set.seed(123); rnorm(1); rnorm(1)` twice to see what happens – Yihui Xie Aug 16 '12 at 23:07