Ben S.
Ben S.

Reputation: 3545

RStudio can't knit results that are in scientific notation

This code produces a nice PDF upon clicking "knit" in RStudio:

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

My favorite number is `r 1 - 0.999`

But the following code produces an error

---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

My favorite number is `r 1 - 0.9999`

The error produced:

! Missing $ inserted.
<inserted text> 
                $
l.85 My favorite number is 10\times

pandoc: Error producing PDF
Error: pandoc document conversion failed with error 43

It looks like it's because the output of 1 - 0.999 is 0.001 but the output of 1 - 0.9999 is 1e-04, a different representation. What can be done?

Upvotes: 0

Views: 229

Answers (1)

jay.sf
jay.sf

Reputation: 72673

What about one of these guys?

---
title: "Untitled"
output: pdf_document
---

My favorite number is `r formatC(1 - 0.9999, format="s")`

My favorite number is `r as.character(1 - 0.9999)`

My favorite number is `r formatC(1 - 0.9999, format="g")`

My favorite number is `r as.character(round(1 - 0.9999, 5))`

Yields:

enter image description here

Upvotes: 1

Related Questions