Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

knitr displaying digits of an integer without scientific notation

Tags:

when displaying a number with inline-code with more than four digits like

`r 21645` 

the result in a knitted html-file is this: 2.164510^{4} (in reality inside the inline-hook there is a calculation going on which results in 21645). Even though I just want it to print the number, like so: 21645. I can easily fix this for one instance wrapping it inside as.integer or format or print, but how do I set an option for the whole knitr-document so that it prints whole integers as such (all I need is to print 5 digits)? Doing this by hand gets very annoying. Setting options(digits = 7) doesnt help. I am guessing I would have to set some chunk-optionor define a hook, but I have no idea how

like image 710
grrgrrbla Avatar asked Jun 17 '15 10:06

grrgrrbla


People also ask

How do I get rid of E+ in R?

You can disable scientific notation in the entire R session by using the scipen option. Global options of your R workspace. Use options(scipen = n) to display numbers in scientific format or fixed.

How do I print actual values instead of scientific notation in R?

If you want to avoid scientific notation for a given number or a series of numbers, you can use the format() function by passing scientific = FALSE as an argument.

How do I change a number from scientific notation to R?

First of all, create a vector and its plot using plot function. Then, use options(scipen=999) to remove scientific notation from the plot.

How do you cancel scientific notation?

(1) Right-click a cell where you want to remove scientific notation, and (2) choose Format Cells… 2. In the Format Cells window, (1) select the Number category, (2) set the number of decimal places to 0, and (3) click OK. Now the scientific notation is removed.


2 Answers

I already solved it, just including the following line of code inside the setoptions-chunk in the beginning of a knitr document:

options(scipen=999) 

resolves the issue, like one can read inside this answer from @Paul Hiemstra:

https://stackoverflow.com/a/25947542/4061993

from the documentation of ?options:

scipen: integer. A penalty to be applied when deciding to print numeric values in fixed or exponential notation. Positive values bias towards fixed and negative towards scientific notation: fixed notation will be preferred unless it is more than scipen digits wider.

like image 152
grrgrrbla Avatar answered Oct 15 '22 08:10

grrgrrbla


If you don't want to display scientific notation in this instance, but also don't want to disable it completely for your knitr report, you can use format() and set scientific=FALSE:

`r format(21645, scientific=FALSE)` 
like image 38
Megatron Avatar answered Oct 15 '22 08:10

Megatron