Format inline output conditional on textual contex

2019-07-18 03:27发布

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.

1条回答
看我几分像从前
2楼-- · 2019-07-18 04:11

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:

`r asis_output(0.0001)`

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 use asis_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 <:

le <- function(x) {
  rel <- if (x >= 0.001) "=" else ""
  return(asis_output(paste(rel, inline_hook(x))))
}

This has the following advantages:

  1. The hook can be used.
  2. There always is an (in-) equality sign.

Note that le is not vectorized.

Examples:

```{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)

le <- function(x) {
  rel <- if (x >= 0.001) "=" else ""
  return(asis_output(paste(rel, inline_hook(x))))
}

```

* p `r le(0.01)` (value: 0.01)
* p `r le(0.001)` (value: 0.001)
* p `r le(0.0001)` (value: 0.0001)
* p `r le(0.00001)` (value: 0.00001)

Output:

p = 0.010 (value: 0.01)
p = 0.001 (value: 0.001)
p < 0.001 (value: 0.0001)
p < 0.001 (value: 0.00001)
查看更多
登录 后发表回答