Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RMarkdown Chunk - Don't echo last expression

I have a chunk in an RMarkdown document that looks like this:

```{r, echo=-4}
a <- 1
b <- 2
x <- a + b
print(paste(c("`x` is equal to ", x), collapse=""))
```

When I knit this, it doesn't show the 4th expression, as expected and as documented here.

a <- 1
b <- 2
x <- a + b
## [1] "`x` is equal to 3"

However, if I add another line within the chunk, I have to update the echo parameter or it hides the wrong expression:

```{r, echo=-4}
a <- 1
b <- 2
c <- 3
x <- a + b + c
print(paste(c("`x` is equal to ", x), collapse=""))
```

Output:

a <- 1
b <- 2
c <- 3
print(paste(c("`x' is equal to ", x), collapse=""))
## [1] "`x` is equal to 6"

Is there a programmatic way in an RMarkdown Chunk to specify that you don't want to echo the last expression in a chunk without manually counting the total number of lines?

like image 348
Jonathan Dayton Avatar asked Jan 29 '23 09:01

Jonathan Dayton


1 Answers

Here's a hack to do that: use knitr hooks to mark the lines to be removed of the output and remove them when printing output.
The following solution implements a new chunk option named rm.last that removes the n last lines of the source code.
knitr has a built-in chunk for source code, so you have to retrieve it with knitr::knit_hooks$get('source').

---
title: "Hack the output with hooks"
---

```{r setup, include=FALSE}
knitr::opts_hooks$set(rm.last = function(options) {
  options$code <- 
    paste0(
      options$code, 
      c(rep("", length(options$code) - options$rm.last), 
        rep(" # REMOVE", options$rm.last)
      )
    )
  options
})

builtin_source_hook <- knitr::knit_hooks$get('source')

knitr::knit_hooks$set(source = function(x, options) {
  if (!is.null(options$rm.last))  
    x <- grep("# REMOVE$", x, invert = TRUE, value = TRUE)
  if (length(x) > 0) {
    return(builtin_source_hook(x, options))
  } else {
    invisible(NULL)
  }
})
```

```{r, rm.last=1}
a <- 1
b <- 2
x <- a + b
print(paste(c("`x` is equal to ", x), collapse=""))
```

However, if your last line of code is print(...), you obviously have to follow @atiretoo answer.

like image 116
RLesur Avatar answered Jan 30 '23 23:01

RLesur