Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to knit inline image that align to multiple lines of text using r-markdown?

I want to knit a report such that the header is similar to the image below.

  • the square at the very left is an icon
  • rectangles are text

I encountered three problems currently

  1. how to insert an icon with url (online image)?
  2. how to format the icon so that it is inline with 3 rows of text
  3. how to change the font size and line spacing between rows

enter image description here

please find the minimal reproducible example below (note that I have commented the online image because I don't know how to correctly code it)

---
output: pdf_document
geometry: margin=0.25in
classoption:
  - landscape
---

```{r, echo=FALSE, results='asis'}
for (i in unique(iris$Species)) {
  cat("\\newpage")
  # cat('\n![](https://bookdown.org/yihui/rmarkdown/images/hex-rmarkdown.png)\n')
  cat("\n#", "Iris Species Summary", "\\hfill", paste0("Exhibit ", grep(i, unique(iris$Species)), ".1"))
  cat("\n##", i, "\\hfill", "(Gross)")
  cat("\n######", "(000's)", "\\hfill", as.character(Sys.Date()))
}
```
like image 400
Kevin Ho Avatar asked Jan 21 '20 14:01

Kevin Ho


People also ask

How do I align an image in R Markdown?

Centering Images You can use the knitr include_graphics() function along with the fig. align='center' chunk option. This technique has the benefit of working for both HTML and LaTeX output. You can add CSS styles that center the image (note that this technique works only for HTML output).

How do you knit in R Markdown?

There are two ways to render an R Markdown document into its final output format. If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it. Note that both methods use the same mechanism; RStudio's “Knit” button calls rmarkdown::render() under the hood.

How is inline code embedded in R Markdown?

Code results can be inserted directly into the text of a . Rmd file by enclosing the code with `r ` .

What is inline code in R Markdown?

Inline code enables you to insert R code into your document to dynamically updated portions of your text.


1 Answers

All your three questions can be solved by including html elements.

The RMarkdown is essentially a Markdown, you can include html elements in the file.

you can either use html by:

```{r echo=FALSE}
knitr::asis_output(htmltools::htmlPreserve("
<div>
    <div>block 2
    </div>
</div>
"))
```

Or

<!--html_preserve-->
<div>
    <div>block 3
    </div>
</div>
<!--/html_preserve-->

Then you can use the html to change the layout and font in the documents, basically you need to learn to write simple html code, not hard just a lot of googling.

Sorry for not write out the code for you with your examples, it should be straightforward. The reference is here, which is a closed GitHub issue: https://github.com/rstudio/rmarkdown/issues/326.

Hope this helps.

like image 133
Bill Chen Avatar answered Oct 23 '22 15:10

Bill Chen