Using a modification (adapted from Jason French's blog post here) to the default inline knitr
hook, I am printing numeric output rounded to 3 decimal places. If the value is less than 0.001, it returns "< 0.001".
Here's an MWE showing the hook modification, and how I might use it in practice in R Markdown:
```{r setup, echo=FALSE}
library(knitr)
inline_hook <- function(x) {
if (is.numeric(x)) {
res <- ifelse(x == round(x),
sprintf("%d", x),
sprintf("%.3f", x)
)
res <- ifelse(x < 0.001, '< 0.001', res)
paste(res, collapse = ", ")
} else paste(as.character(x), collapse = ", ")
}
knit_hooks$set(inline = inline_hook)
```
```{r, echo=FALSE}
stat <- 1.2345
p <- 0.000001
```
Blah was significant (test statistic = `r stat`, p = `r p`)
The above renders as:
Blah was significant (test statistic = 1.234, p = < 0.001)
Note the p = < 0.001
. Preferably, this would be p < 0.001
. Is it possible to have knitr
check whether an equals sign precedes the inline expression, and if it does, suppress it when appropriate?
In case it makes a difference, I'm actually knitting a Sweave document, not R Markdown.
Personally, I don't like the
inline_hook
used in the question / the linked blog. Automatically converting output like this seems risky to me. The current question is a case in point: Sometimes the conversion is harmful.There is a trivial and a somewhat complex solution to the issue.
Trivial solution
The hook can be circumvented using
asis_output
:outputs
1e-04
, i.e. the hook doesn't apply. Otherwise the output was< 0.001
.Drawback: You either use the hook, but then you dont know whether you get
[number]
or< [number]
as output (which is a problem when you want output like "p = [number]" / "p < [number]"). Or you useasis_output
; then you know there will be no (in-)equality sign, but you cannot take advantage of the hook.Advanced solution
The following function applies
inline_hook
but adds an additional=
sign if the hook doesn't add<
:This has the following advantages:
Note that
le
is not vectorized.Examples:
Output: