2

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)` 
JoeF
  • 733
  • 1
  • 7
  • 21
  • can't replicate. If I create an `rmd` file with *just* your inline code and run it from the console via `rmarkdown::render("inline.rmd")`, it works. – Ben Bolker Mar 13 '16 at 21:56
  • Thanks. Yeah, I should have tried it on a clean document before posting here. It turns out I had created an online hook for my inline code to round scientific notation to two digits. I'm not sure why that caused problems for non-numeric variables, but it apparently does. – JoeF Mar 13 '16 at 22:14

1 Answers1

0

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)
```
Community
  • 1
  • 1
JoeF
  • 733
  • 1
  • 7
  • 21