根据文本上下文设置内联输出的格式 [英] Format inline output conditional on textual context
问题描述
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".
这是一个MWE,它显示了钩子修改,以及在R Markdown中如何在实践中使用它:
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`)
以上呈现为:
Blah was significant (test statistic = 1.234, p = < 0.001)
请注意p = < 0.001
.优选地,这将是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?
以防万一,我实际上是在编织Sweave文档,而不是R Markdown.
In case it makes a difference, I'm actually knitting a Sweave document, not R Markdown.
推荐答案
我个人不喜欢问题/链接博客中使用的inline_hook
.像这样自动转换输出对我来说似乎很冒险.当前的问题就是一个恰当的例子:有时候这种转换是有害的.
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.
可以使用asis_output
绕过钩子:
`r asis_output(0.0001)`
输出1e-04
,即该钩子不适用.否则,输出为< 0.001
.
outputs 1e-04
, i.e. the hook doesn't apply. Otherwise the output was < 0.001
.
缺点:您可以使用钩子,但是随后您不知道是获取[number]
还是< [number]
作为输出(当您希望像"p = [number]"/"p< [数字]").或者您使用asis_output
;那么您知道将没有(不等号)符号,但是您无法利用该钩子.
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.
以下函数适用inline_hook
,但如果挂钩未添加<
,则会添加一个附加的=
符号:
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))))
}
这具有以下优点:
- 可以使用挂钩.
- 总是有一个(中)等号.
请注意,le
没有向量化.
示例:
```{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)
输出:
p = 0.010 (value: 0.01)
p = 0.001 (value: 0.001)
p < 0.001 (value: 0.0001)
p < 0.001 (value: 0.00001)
这篇关于根据文本上下文设置内联输出的格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!