Imaginary units of complex numbers falling on 'knitr'

I ran into a strange problem in "knitr" with an R code containing complex numbers entered directly using the notation "x + yi". For an illustration, see the Minimum Example: http://goo.gl/Yj77kI

The sample code is correctly evaluated both in the R-console and as a code fragment when compiling with Sweave, resulting in:

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

However, trying to use 'knitr' in the same document, the imaginary unit seems to be lost, and I get:

 1^2 ## [1] 1 

Any ideas?

Cheers, Andrzej

SessionInfo() output:

 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 
+4
source share
1 answer

Yihui noticed that this error goes to getParseData() , which cannot display 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 formatR packages (used by knitr for tidy source code) and the highr package (used by knitr to highlight code) depend on getParseData() , so you had to set highlight=FALSE, tidy=FALSE in the piece of code to get the correct results in doc knit() 'ed.

Following a Yihui report on the R-devel mailing list, Duncan Murdoch announced that getParseData() would be fixed in the first R-fixed after the release of R-3.0.2.

+3
source

Source: https://habr.com/ru/post/1502848/


All Articles