Leaving blank space in RMarkdown with an inline function call

#1

I want to leave empty space in my documents, both pdf and html. I use non-breaking spaces followed by 2 spaces (  )to do this:

---
title: "Test of inline function with non-breaking spaces"
author: "Drew Tyre"
date: "2019-04-14"
output: html_document
---

I want to leave empty space in my documents, both pdf and html. I use
non-breaking spaces followed by 2 spaces to do this:

   
   
   
   
   
   
   
   

And that's fine, but I thought I could get smarter so I made this function

```{r}
blank_lines <- function(n = 10){cat(rep("&nbsp;  ",n), sep="\n")}
blank_lines(2)
```

But why does this work:

```{r, results = "asis"}
blank_lines()
```

leaving a bunch of empty space, but this

`r blank_lines()`

doesn't?

I've looked in the markdown produced, and the output of the inline code simply 
isn't there.
0 Likes

#2

From this SO post, it seems to be due to the way inline code is evaluated.

> knitr::knit_hooks$get("inline")
function (x) 
{
    if (is.numeric(x)) 
        x = round_digits(x)
    paste(as.character(x), collapse = ", ")
}
<environment: namespace:knitr>

If you run blank_lines() through the hook, it returns an empty vector:

> paste(as.character(blank_lines(2)), collapse = ", ")
&nbsp;  
&nbsp;  
[1] ""

Which is also why you don't see anything in the md output.

0 Likes