Knitr中非数字变量的内联代码块 [英] Inline code chunk for non-numeric variables in knitr
本文介绍了Knitr中非数字变量的内联代码块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用内联R Markdown代码访问因子的第一级.如果使用块,则可以使它工作,但是如果内联地使用,则不能.
I'm trying to use inline R Markdown code to access the first level of a factor. I can get it to work if I use a chunk but not if I do it inline.
所以这可行:
```{r}
as.character(iris$Species[1])
```
这不是:
`r as.character(iris$Species[1])`
如果我保存了缓存并编织了两次文档,则可以运行内联版本.我发现这有点奇怪,因为数字变量的行为不同.因此,例如,这种方法无需编织两次即可
I could get the inline version to run if I saved the cache and knitted the document twice. I just found this a bit odd because numeric variables behave differently. So, for instance, this works without having to knit it twice
`r mean(iris$Sepal.Length)`
推荐答案
对不起.我发现了问题所在.我从>此处导致问题的原因:
Sorry. I found out what the problem was. I found an inline hook that I had taken from here that turned out to cause the problem:
```{r, echo = FALSE}
inline_hook <- function(x){
if(is.numeric(x)){
paste(format(x,digits = 2))
}
}
knitr::knit_hooks$set(inline=inline_hook)
```
这篇关于Knitr中非数字变量的内联代码块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文