Problem with large/small numbers in knitr

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Problem with large/small numbers in knitr

xiaodao
I have problems with very large numbers using knitr. In the following, my a and b are extremely small and ssrr and ssru are extremely large. Knitr delivers error messages. Scaling ssrr and ssru by 1000 resolved the problem:
ssrr <-ssrr/1000 ;  ssru<-ssru/1000
Any clue as to how I might resolve the problem?
BTW, the same program does run in another computer. I am wondering whether my installation of Scientific Work place interferes, and if so, what else I might do to avoid the problem.

\documentclass{article}
\begin{document}

<<setup, echo=FALSE, cache=FALSE>>=
## numbers >= 10^5 will be denoted in scientific notation,
## and rounded to 2 digits
options(scipen = 1, digits = 2)

<<>>=
a<-1e-13
b<-2.5e-10
ssrr<-123456.12
ssru<-123400.00
@

$
c=\Sexpr{a}/\Sexpr{b} % either this formula or the following formula will has error message "missing $" after click "complie" in Rstudio.
f=\Sexpr{ssrr-ssru}/\Sexpr{ssru}
$

\end{document}
Reply | Threaded
Open this post in threaded view
|

Re: Problem with large/small numbers in knitr

Yihui Xie-2
(The same question was cross-posted in the knitr mailing list, so cc'ed as well)

According to the other email from Steven, this problem was solved by
reinstalling R. I cannot reproduce the problem under either Ubuntu or
Windows (regardless of 32-bit or 64-bit R), so I have no idea of what
happened. All I can suggest in this case is to update R and run
update.packages() as indicated by FAQ1:
https://github.com/yihui/knitr/blob/master/FAQ.md

Regards,
Yihui
--
Yihui Xie <[hidden email]>
Phone: 515-294-2465 Web: http://yihui.name
Department of Statistics, Iowa State University
2215 Snedecor Hall, Ames, IA


On Wed, Dec 26, 2012 at 6:28 PM, xiaodao <[hidden email]> wrote:

> I have problems with very large numbers using knitr. In the following, my a
> and b are extremely small and ssrr and ssru are extremely large. Knitr
> delivers error messages. Scaling ssrr and ssru by 1000 resolved the problem:
> ssrr <-ssrr/1000 ;  ssru<-ssru/1000
> Any clue as to how I might resolve the problem?
> BTW, the same program does run in another computer. I am wondering whether
> my installation of Scientific Work place interferes, and if so, what else I
> might do to avoid the problem.
>
> \documentclass{article}
> \begin{document}
>
> <<setup, echo=FALSE, cache=FALSE>>=
> ## numbers >= 10^5 will be denoted in scientific notation,
> ## and rounded to 2 digits
> options(scipen = 1, digits = 2)
>
> <<>>=
> a<-1e-13
> b<-2.5e-10
> ssrr<-123456.12
> ssru<-123400.00
> @
>
> $
> c=\Sexpr{a}/\Sexpr{b} % either this formula or the following formula will
> has error message "missing $" after click "complie" in Rstudio.
> f=\Sexpr{ssrr-ssru}/\Sexpr{ssru}
> $
>
> \end{document}
>
>
>
> --
> View this message in context: http://r.789695.n4.nabble.com/Problem-with-large-small-numbers-in-knitr-tp4653986.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> [hidden email] 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.

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: Problem with large/small numbers in knitr

mlell08
In reply to this post by xiaodao
On 26.12.2012 23:28, xiaodao wrote:
> I have problems with very large numbers using knitr. In the following, my a
> and b are extremely small and ssrr and ssru are extremely large.

>
> \documentclass{article}
> \begin{document}
>
> <<setup, echo=FALSE, cache=FALSE>>=
> ## numbers >= 10^5 will be denoted in scientific notation,
> ## and rounded to 2 digits
> options(scipen = 1, digits = 2)
>
> <<>>=
> a<-1e-13
> b<-2.5e-10
> ssrr<-123456.12
> ssru<-123400.00
> @
>
> $
> c=\Sexpr{a}/\Sexpr{b} % either this formula or the following formula will
> has error message "missing $" after click "complie" in Rstudio.
> f=\Sexpr{ssrr-ssru}/\Sexpr{ssru}
> $
>
> \end{document}
>

I copied your file to "abc.Pnw", then opened an R session and run
setwd("<Path_to_file>")
library(knitr)
knit("abc.Rnw")

I got the following text in the resulting abc.tex file:


$
c=$10^{-13}$/$2.5\times 10^{-10}$ % either this formula or ...
f=56.12/$1.23\times 10^{5}$
$

knitr converts large numbers in Sexpr to the exponential notation, so
you've got a nested math environment here.

You can convert your numbers to strings with as.character()

Alternative:
if you run knit_hooks$get("inline") you get the function used for inline
output procession


https://github.com/yihui/knitr/issues/33


-> if you write
knit_hooks$set()$inline
function (x)
{
    if (is.numeric(x))
        x = round(x, getOption("digits"))
    paste(as.character(x), collapse = ", ")
}

in your setup chunk (did you forget the closing @ in your setup chunk?),
it also works. (which is strange, because this function is exactly what
is predefinded by knitr but that's another question

Regards, Moritz
--
GnuPG Key: 0x7340821E

______________________________________________
[hidden email] 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.