Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting HTML code from inside R markdown

Tags:

markdown

r

knitr

In my code I am checking a condition and I'd like R markdown to show an image when that condition is TRUE. Here is what I have

```{r}
if (LOGICAL.Condition)
  {
     cat("<img src='http://imgc.allpostersimages.com/images/P-473-488-90/60/6071/B4XD100Z/posters/now-stop-and-hammer-time.jpg'>")
  }
```

The output of R that shows up on my HTML page is a text as bellow but I want it to be an image

## <img src='http://imgc.allpostersimages.com/images/P-473-488-90/60/6071/B4XD100Z/posters/now-stop-and-hammer-time.jpg'>
like image 363
Mark Avatar asked Oct 26 '12 22:10

Mark


People also ask

How do I export HTML from R Markdown?

To transform your markdown file into an HTML, PDF, or Word document, click the “Knit” icon that appears above your file in the scripts editor. A drop down menu will let you select the type of output that you want. When you click the button, rmarkdown will duplicate your text in the new file format.

Can Markdown render HTML?

in HTML will appear as <div class=Heading 1> Markdown also allows for raw HTML. There is no need to put any syntax around the code, but it needs to stand alone in the source document with no content above or below. A good example of using raw HTML in a Markdown document is when embedding a Youtube video.

How do I put HTML code in Markdown?

For any markup that is not covered by Markdown's syntax, you simply use HTML itself. There's no need to preface it or delimit it to indicate that you're switching from Markdown to HTML; you just use the tags. The only restrictions are that block-level HTML elements — e.g. <div> , <table> , <pre> , <p> , etc.

How do I show output in R Markdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .


1 Answers

Option asis will simply write the raw results from R into the output document (which is what you'd like). Otherwise it throws that ## in front.

```{r results="asis"}
if (LOGICAL.Condition)
  {
     cat("<img src='http://imgc.allpostersimages.com/images/P-473-488-90/60/6071/B4XD100Z/posters/now-stop-and-hammer-time.jpg'>")
  }
```
like image 170
Maiasaura Avatar answered Sep 21 '22 18:09

Maiasaura