Search code examples
rmarkdownr-markdownxaringanremarkjs

Is there a shortcut to increment a Xaringan slide directly in the code?


I just discovered the awesome Xaringan package, and I'd like my presentation to be as incremental as possible.

For instance, my introduction slide looks like this:

```{r intro1, echo=TRUE}
version$version.string #should give 3.6.1
```

--

```{r intro2, echo=TRUE}
class(iris)
```

--

```{r intro3, echo=TRUE}
dim(iris) #row, cols
```

--

```{r intro4, echo=TRUE}
colnames(iris)
```

Though, I find it tedious and not much readable to write it that way.

I tried this but it doesn't work:

```{r , echo=TRUE}
version$version.string
class(iris)

-- 

dim(iris)
colnames(iris)
```

Of course, this consider -- as code.

Is there a way to increment my slide from inside the code?


Solution

  • Yihui pretty much gave the answers in the comments but I elaborate here with one additional tweak which will ensure that the separator code (i.e. knitr::asis_output('\n--\n')) is not visible in the output (this is done by specifying the line number in which the separator appears in the chunk argument as below).

    You can the separator code where you want to separate and just make sure the corresponding line number is not echo-ed.

    ---
    output: 
      xaringan::moon_reader: 
        seal: false
    ---
    
    ```{r, echo = -4}
    version$version.string 
    class(iris)
    
    knitr::asis_output('\n--\n')
    
    dim(iris) 
    colnames(iris)
    ```