[R] Setting inline hook to a function identical to default in knitr turns of exponential formatting

Yihui Xie xie at yihui.name
Mon Jan 28 18:28:08 CET 2013


This is a little bit hard to explain because there are two levels of
default hooks (the system default and the document-format-specific
default). The best way to explain it is probably the source code:
https://github.com/yihui/knitr/blob/master/R/output.R#L183-L189

In short, the "default" hooks you mentioned are not really used by
knitr at all; they are only placeholders in the system. If you do not
modify them in advance, knitr will set the appropriate hooks according
to the document format, e.g. the inline hook for LaTeX will use
scientific notation for numbers.

The absolute default inline hook does not do scientific notation, as
you can see from the its source:

## function (x)
## {
##    if (is.numeric(x))
##        x = round(x, getOption("digits"))
##    paste(as.character(x), collapse = ", ")
## }

I recommend you to ignore these system default hooks, unless you want
to study the technical implementation of this package. For the default
hooks according to the output format, see
http://yihui.name/knitr/hooks and ?render_latex in the documentation.

Regards,
Yihui
--
Yihui Xie <xieyihui at gmail.com>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Mon, Jan 28, 2013 at 4:42 AM, mlell08 <mlell08 at gmail.com> wrote:
> Hello List,
>
> while dealing with a questin of 'xiaodao' (
> http://r.789695.n4.nabble.com/Problem-with-large-small-numbers-in-knitr-tp4653986.html)
>
> I noticed that copying the default inline hook function obtained by
> knit_hooks$get("inline")
> into a knit_hook$set(inline = <...>) call turns off exponential
> fomatting in the resulting .tex file.
>
> I used a stripped version of 'xiaodao's example:
> \documentclass{article}
> \begin{document}
>
> <<>>=
> a<-1e-13
> b<-2.5e-10
> ssrr<-123456.12
> ssru<-123400.00
> @
>
> $
> c=\Sexpr{a}/\Sexpr{b}
> f=\Sexpr{ssrr-ssru}/\Sexpr{ssru}
> $
>
> \end{document}
>
> so:
> knit_hooks$restore()
> knit_hooks$get("inline")
>
> ## yields:
> ## function (x)
> ## {
> ##    if (is.numeric(x))
> ##        x = round(x, getOption("digits"))
> ##    paste(as.character(x), collapse = ", ")
> ## }
> ## <environment: namespace:knitr>
>
> knit("FILENAME.Rnw")
>
> ## .tex-file:
> ## c=$10^{-13}$/$2.5\times 10^{-10}$
> ## f=56.12/$1.234\times 10^{5}$
>
>
> ## then run knit_hooks$set() with exactly the same function:
> knit_hooks$set(inline= function (x)
> {
>         if (is.numeric(x))
>                 x = round(x, getOption("digits"))
>         paste(as.character(x), collapse = ", ")
> }
> )
> knit("FILENAME.Rnw")
>
> ## .tex-File
> ## c=0/0
> ## f=56.12/123400
>
> knit_hooks$get("inline")
> ## function (x)
> ## {
> ##      if (is.numeric(x))
> ##              x = round(x, getOption("digits"))
> ##      paste(as.character(x), collapse = ", ")
> ## }
>
>
> The only thing that changed is no <environment: namespace:knitr> is
> printed anymore
>
> Why does knitr change its output?
>
> Regards, Moritz
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list