Membenamkan HTML yang dijana oleh fungsi R
P粉513316221
P粉513316221 2024-04-01 20:51:51
0
1
378

Saya mahu membina html secara dinamik dan menghasilkan html itu Dalam kuarto. Aplikasi sebenar melibatkan memasukkan iFrame, Tetapi untuk menjadikannya mudah, mari buat teg <img> sahaja.

Ini kod .qmd saya:

```{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'>&emsp;%s", iUrl, iText)
    }

let's call the function and see what appears:

```{r echo=FALSE}
imageLink("https://www.united.com/8cd8323f017505df6908afc0b72b4925.svg", "united logo")
```

and now, here's what it's supposed to look like:

<img src = 'https://www.united.com/8cd8323f017505df6908afc0b72b4925.svg'>&emsp;united logo

Ia memaparkan dan fungsinya jelas dipanggil, Tetapi ia menunjukkan kod html dan bukannya imej:

Saya tahu ini mudah tetapi saya tidak dapat menemuinya. Terima kasih banyak-banyak!

P粉513316221
P粉513316221

membalas semua(1)
P粉883973481

Ada dua perkara yang perlu diperhatikan:

  • Pertama sekali, Quarto membungkus sebarang output blok kod dalam

     tag. To get the output asis you need to use the chunk option results: asis.

  • Secondly, sprintf (or even print) returns output enclosed within quotes. So after using results: asis, you would get the html tags but would also get the quotes. So you need to wrap the sprintf with cat to get intended results.

---
format: html
---

```{r}
#| echo: false
imageLink  %s", iUrl, iText))
}
```

```{r}
#| echo: false
#| results: asis
imageLink("https://www.united.com/8cd8323f017505df6908afc0b72b4925.svg", "united logo")
```

and now, here's what it's supposed to look like:

 united logo
secara lalai
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!