4

I came across a strange issue in 'knitr' with R code containing complex numbers entered directly using the "x + yi" notation. For an illustration see the minimal example: http://goo.gl/Yj77kI

The sample code evaluates correctly, both in R console and as a code chunk when compiled with Sweave, resulting in:

> 1i^2
[1] -1+0i

However, when trying to use 'knitr' on the same document, the imaginary unit seems to get lost and what I get is:

1^2
## [1] 1

Any ideas?

Cheers, Andrzej

Output of SessionInfo():

R version 3.0.0 (2013-04-03)
Platform: x86_64-unknown-linux-gnu (64-bit)

locale:
[1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8          LC_COLLATE=en_US.UTF-8     LC_MONETARY=en_US.UTF-8   
[6] LC_MESSAGES=en_US.UTF-8    LC_PAPER=C                 LC_NAME=C                  LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
[1] knitr_1.4.1

loaded via a namespace (and not attached):
[1] digest_0.6.3   evaluate_0.4.7 formatR_0.9    highr_0.2.1    stringr_0.6.2  tools_3.0.0   
Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
aoles
  • 1,525
  • 10
  • 17
  • It works for me: I get `(0+1i)^2` and `## [1] -1+0i`. Could you add the output of `sessionInfo()`? – Vincent Zoonekynd Sep 18 '13 at 16:12
  • Thank you for your quick response! I've added the output of `sessionInfo()` as requested. – aoles Sep 18 '13 at 16:28
  • 3
    Update: it seems to me that this might be related to the `tidy.source()` function from the 'formatR' package; when run on `1i^2` the result is `1^2`. – aoles Sep 18 '13 at 16:43
  • 2
    That is indeed a bug of the formatR package, which is really a bug of R: https://stat.ethz.ch/pipermail/r-devel/2013-September/067523.html Before it is fixed, you will have to use the chunk option `tidy=FALSE` to disable formatR. – Yihui Xie Sep 18 '13 at 17:51
  • 1
    @Yihui -- I already tried that, and setting `tidy=FALSE` fixed the printing of the results but not the source (which still came out as `1^2`) Weird. – Josh O'Brien Sep 18 '13 at 18:30
  • 1
    @JoshO'Brien the same bug is affecting the highr package, which also uses getParseData(), so `highlight=FALSE` – Yihui Xie Sep 18 '13 at 20:18

1 Answers1

3

Yihui observed that this bug traces to getParseData(), which fails to render the i in its text column:

getParseData(parse(text="3i"))
#   line1 col1 line2 col2 id parent     token terminal text
# 1     1    1     1    2  1      2 NUM_CONST     TRUE    3
# 2     1    1     1    2  2      0      expr    FALSE  

Both the formatR package (used by knitr to "tidy" source code) and the highr package (used by knitr to highlight code) depend on getParseData(), so one had to set highlight=FALSE, tidy=FALSE in a code chunk to get the proper results in a knit()'ed document.

Following Yihui's report to the R-devel mailing list, Duncan Murdoch announced that getParseData() will be fixed in the first R-patched following the release of R-3.0.2.

Josh O'Brien
  • 159,210
  • 26
  • 366
  • 455