我想动态地构建html并以夸托格式呈现,实际的应用程序需要插入一个iFrame,但为了简单起见,我们只做一个<img>
标记。
下面是我的.qmd代码:
```{r}
source("awash-functions.r")
How do you inject html text produced in an r function into a quarto document?
In R markdown, I had the function sprintf
a string. That doesn't seem to work here!
Here is awash-functions.r
:
imageLink <- function(iUrl, iText) {
sprintf("<img src = '%s' width='24'> %s", iUrl, iText)
}
let's call the function and see what appears:
imageLink("https://www.united.com/8cd8323f017505df6908afc0b72b4925.svg", "united logo")
and now, here's what it's supposed to look like:
united logo
它会进行渲染,并且函数会被调用,但是它显示的是html代码,而不是图像:
![](https://i.stack.imgur.com/OgqT5.png)
我知道这是简单的东西,但我找不到。非常感谢!
1条答案
按热度按时间xytpbqjk1#
有两点需要注意:
<pre><code>
标签中。要获得输出,您需要使用块选项results: asis
。sprintf
(甚至print
)返回用引号括起来的输出,所以在使用results: asis
之后,你会得到html标记,但也会得到引号,所以你需要用cat
Packagesprintf
以得到预期的结果。and now, here's what it's supposed to look like:
united logo