5

I am having trouble in evaluating variables inside data.table[...] when using knitr. I have seen answers to this problem stating it is solved, but I am still getting errors. I am using data.table version 1.8.8 on R 3.0.0.

I am using Emacs 24.3 that comes with ESS 13.05. Here's a sample .Rnw file:

\documentclass{article}

\begin{document}  

<<init,eval=TRUE, cache=FALSE>>=
require(data.table)
dt <- data.table(x=1:10, y=11:20)
dt
dt[x > 5]
@

\end{document}

and corresponding (pdf) output:

require(data.table)
## Loading required package: data.table
## data.table 1.8.8  For help type:  help("data.table")
dt <- data.table(x = 1:10, y = 11:20)

dt
##    x  y
## 1: 1 11
## 2: 2 12
## 3: 3 13
## 4: 4 14
## 5: 5 15
## 6: 6 16
## 7: 7 17
## 8: 8 18
## 9: 9 19 
## 10: 10 20

dt[x > 5]
## Error:  object ’x’ not found
Rodrigo
  • 473
  • 4
  • 14
  • Josh, thanks for the tip. I have added a sample .Rnw file. I rely on Emacs to call knitr so I do not call it directly and I'm not sure how it is being called... – Rodrigo Jun 03 '13 at 17:01
  • 1
    I have no problem when I run `knit("sample.Rnw")` and then compile the resulting `"sample.tex"` document. However, when I use the ESS "shortcut" `M-n r` to knit the document, I get the same error as you. **This seems to be a problem with the way ESS calls `knit()`.** Checking my R console after running, `M-n r`, I see that it ran `.ess_weave()`, so to debug, I'd have a look there. Or maybe better, report over on [ESS-help](https://stat.ethz.ch/mailman/listinfo/ess-help), reporting R, Emacs, and ESS version info, and the direct commands that do work alongside the ESS commands that don't – Josh O'Brien Jun 03 '13 at 17:09
  • 1
    I'm not sure. Sounds like an environment issue. I have not used Emacs/ESS for a long while, and I remember it calls Sweave/knitr in `local()`, which may or may not be the reason. – Yihui Xie Jun 03 '13 at 18:40
  • 1
    Yes, it looks like this is an ESS problem. I have 'solved' the problem by redefining the function .ess_weave in my environment (using the exact same code). .ess_weave is defined in the environment and somewhow it prevents it from working appropriately in this case. I will post the problem in ESS-help. Meanwhile, should I reframe my original question? Thanks! – Rodrigo Jun 04 '13 at 08:33
  • 1
    @RogerBill I found the same thing too. The proximate cause seems to be this recent change, from ESS 13.05: "ESS[R]: ESS stores function in 'ESSR' environment to avoid kludging users' global environment and accidental deletion." I half-wonder whether this results in an odd interaction between **knitr**'s use of the **evaluate** package and **data.table**'s somewhat finicky scoping rules. Not sure why having them evaluate calls from functions in an environment attached to the search path (i.e. ESSR) would lead to problems, but there are enough non-standard moving parts there that it's plausible! – Josh O'Brien Jun 04 '13 at 18:14

1 Answers1

1

Most likely an environment issue. This should solve it:

(setq ess-swv-processing-command "%s(%s)")
VitoshKa
  • 8,387
  • 3
  • 35
  • 59