Inline code chunk for non-numeric variables in knitr

核能气质少年 提交于 2020-01-04 06:23:12

问题


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.

So while this works:

```{r}
as.character(iris$Species[1])
```

This does not:

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

回答1:


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


来源:https://stackoverflow.com/questions/35976324/inline-code-chunk-for-non-numeric-variables-in-knitr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!