3

I'm using tidy to format the Chunks of my rMarkdown file. How can I prevent that numbers, especially long numberes, are formatted by tidy? For example:

set.seed(3585)
n=10000 
loginkomens_prog1 = c(rep(10,0.3*n), rnorm(0.7*n, mean=11, sd=1))
inkomens_prog1 = exp(loginkomens_prog1) 
inkomens_prog1[inkomens_prog1>100000]=100000
par(mfrow=c(1,2)) 
hist(inkomens_prog1,main="Programma1",xlim=c(0,200000)) 

Tidy reformat it, like this:

set.seed(3585)  
n = 10000 
loginkomens_prog1 = c(rep(10, 0.3 * n), rnorm(0.7 * n, mean = 11, 
    sd = 1))  
inkomens_prog1 = exp(loginkomens_prog1)  
inkomens_prog1[inkomens_prog1 > 1e+05] = 1e+05  
par(mfrow = c(1, 2))  
hist(inkomens_prog1, main = "Programma1", xlim = c(0, 2e+05))  

I would prefer that it stays 100000 instead of 1e+05. Is it possible? Or is it better that I set tidy=FALSE for that chunk (and that I format it manually)?

aosmith
  • 34,856
  • 9
  • 84
  • 118
Karting06
  • 33
  • 5

1 Answers1

1

tidy_source from the formatR package is used to tidy up the contents, and what it does, ultimately, is to cat() the output.

So, to prevent your number from getting converted to scientific notation, you have to set the scipen option appropriately (see e.g. How to disable scientific notation in R?).

Example:

temp <- tempfile(fileext = ".R")
cat("inkomens_prog1[inkomens_prog1>100000]=100000", file = temp)

formatR::tidy_source(temp)
# inkomens_prog1[inkomens_prog1 > 1e+05] = 1e+05

options(scipen = 99)
formatR::tidy_source(temp)
# inkomens_prog1[inkomens_prog1 > 100000] = 100000
Community
  • 1
  • 1
Weihuang Wong
  • 12,868
  • 2
  • 27
  • 48
  • Thanks, I didn't thought about scientific notation! To make things look cleaner, I added `tidy.opts=options(scipen = 99)` to my R chunk. – Karting06 Jan 10 '17 at 09:32