>  Q&A  >  본문

R 함수로 생성된 HTML 삽입

동적으로 HTML을 빌드하고 해당 HTML을 렌더링하고 싶습니다. 4분할에. 실제 애플리케이션에는 iFrame 삽입이 포함됩니다. 하지만 간단하게 유지하기 위해 <img> 태그를 만들어 보겠습니다.

내 .qmd 코드는 다음과 같습니다.

으아아아

렌더링되고 함수가 분명히 호출됩니다. 하지만 이미지 대신 HTML 코드가 표시됩니다.

간단한 건 알지만 찾을 수가 없어요. 매우 감사합니다!

P粉513316221P粉513316221181일 전380

모든 응답(1)나는 대답할 것이다

  • P粉883973481

    P粉8839734812024-04-02 11:25:43

    주의해야 할 두 가지 사항이 있습니다:

    • 우선, Quarto는 기본적으로 모든 코드 블록 출력을

       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 <- function(iUrl, iText) {
      cat(sprintf(" %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
    로 래핑합니다.

    회신하다
    0
  • 취소회신하다