Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
250 views
in Technique[技术] by (71.8m points)

html - R Markdown htmltools with cat function adds additional not needed code

I need to generate a grid of thumbnails with texts and links. Something like YouTube.

To do so I am using Markdown and htmltool package.

This is a testing code

```{r, echo=FALSE, warning=FALSE, results='asis'}
  library(htmltools)
  
  thumbnail <- function(title, feedtitle, url, thumbs) {
    
    return(
      div(class = "col-sm-4",
        p(
          title
        ),
        a(class = "thumbnail", title = feedtitle, href = url,
               img(src = thumbs),
               div(class = "caption", title)
               
        )
      )
    )
  }
  
  thumbnail("Title",
            "feedtitle",
            "url",
            "https://i4.ytimg.com/vi/KQ80oD_boBM/hqdefault.jpg"
          )
  
  
  cat(
    as.character(
              thumbnail("Title Cat",
                  "feedtitle",
                  "url",
                  "https://i4.ytimg.com/vi/KQ80oD_boBM/hqdefault.jpg"
                )
    )
  )
  
  for (i in 1:2){
    #thumbnail(videos[t,])
    cat(
      as.character(
                thumbnail(paste0("Title loop ",i),
                    "feedtitle",
                    "url",
                    "https://i4.ytimg.com/vi/KQ80oD_boBM/hqdefault.jpg"
                  )
      )
    )
  }

As a result I get this page enter image description here

There is an addition under every block with cat function. This is an example of the block

<div class="col-sm-4">
<p>
Title loop 2
</p>
<a class="thumbnail" title="feedtitle" href="url"> <img src="data:image/jpeg;base64,/9j/4AAQSkZJRgABA<long image string that I removed>" />
<div class="caption">
Title loop 2
</div>
<p></a></p>
</div>

There is additional <p></a></p> It doesn't appear if I just run code with cat in the Markdown document. But in the resulted html file after knitting it is added.

If I use the call to the function without cat() the result is correct. But I need the cat() to run the function in a loop. Without cat() in a loop the result doesn't show the html block at all.

Can anyone explain me why it is like that there and how to remove it?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...