Run an R function inline inside image shorthand ![]()

Does anyone know a trick for doing something like this in RMarkdown:

![alt text](`r my_fun(x)`)

It's the closing bracket of the function call that causes the problem. It thinks it is the end of the image location bracket.

I know a few ways round. Of course I can pre-assign the function output to an object and then just inline r my_obj within the markdown. But that is extra code. I'm trying to keep things tight if possible.

I can just do it in HTML with <img />. But the markdown shorthand is much nicer and more succinct.

I could use knitr::include_graphics(), but that doesn't seem to allow for alt text.

No big deal if not possible, I thought I would just see if anyone knows a neat way to do this.

---
title: "R Notebook"
output:
  html_document
---

```{r}
x <- "http://www.online-image-editor.com//styles/2014/images/example_image.png"
myfunc <- function(x){x}
```

![alt text](`r myfunc(x)`))
2 Likes

Thanks - I was actually misled by RStudio's rendering of that second bracket into thinking that the format was invalid. I should have taken the time to run an example before posting, though.
image

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.