Canovice
Canovice

Reputation: 10441

misinterpreting echo = false using Rstudio in an rmarkdown file / codechunk

I have a fairly basic question. Let's say I have the following code block in an rmarkdown file, that does nothing but resets my global environment, loads in several libraries, and includes 1 print statement.

```{r, echo = FALSE}
rm(list = ls())

# data manipulation
library(jsonlite); library(plyr); library(dplyr); library(data.table); 

# data scraping
library(RSelenium); library(rvest); library(XML); 

# parallel computing / faster code
library(foreach); library(doParallel); library(microbenchmark); 

print("this")

# machine learning
library(rpart.plot); library(glmnet); library(rpart); library(gbm); 
library(nnet); library(caret); library(randomForest); 

# lineup optimization
library(lpSolve);  

# data visualization
library(ggplot2); library(reshape2);
```

when I run this, i get the following output in the console

> rm(list = ls())
> 
> # data manipulation
> library(jsonlite); library(plyr); library(dplyr); library(data.table); 
> 
> # data scraping
> library(RSelenium); library(rvest); library(XML); 
> 
> # parallel computing / faster code
> library(foreach); library(doParallel); library(microbenchmark); 
> 
> print("this")
[1] "this"
> 
> # machine learning
> library(rpart.plot); library(glmnet); library(rpart); library(gbm); 
> library(nnet); library(caret); library(randomForest); 
>  
> # lineup optimization
> library(lpSolve);  
> 
> # data visualization
> library(ggplot2); library(reshape2);

however, I thought the purpose of setting echo = false was that everything besides [1] "this" was supposed to be suppressed in the console, which is what I would like. What is setting echo = FALSE actually doing? And what do I actually have to do to to suppress the repetition of code in the console? The rest of my code has important print statements that confirm my code is operating correctly, but it is being drowned out in the console by the code repetition.

Thanks,

Upvotes: 2

Views: 6879

Answers (2)

Phil
Phil

Reputation: 4444

Knitr is not designed to alter what's displayed in the console. The R console and history keep a perfect, verbatim copy of all commands and output and, as far as I know, there's no way to alter this behaviour. I strongly advise you shouldn't try to change it either, because imagine you ran some analyses and forgot to run a code chunk: you'd have no audit trail of your commands to check against.

Knitr is designed to create documents (html, docx, or pdf for the most part) and changing chunk options such as echo affects what is displayed in the resulting document, not the console. How to control output is well documented on the knitr webpage: http://yihui.name/knitr/demo/output/

Having established that we're not going to get exactly what you want, here are a few options that might help you separate code from output:

Use ctrl/cmd + L

This simply clears the console, so you can step through, clear what you don't need, and run sections of code with results. This requires a lot of manual intervention though; not recommended.

Use sink()

sink() allows you to save output to a text file, so the flow of your .R script might look something like:

...    
# parallel computing / faster code
library(foreach); library(doParallel); library(microbenchmark); 

sink(file = "test.txt")
print("this")
sink()

# machine learning
library(rpart.plot); library(glmnet); library(rpart); library(gbm); 
library(nnet); library(caret); library(randomForest);

sink(file = "test.txt", append = TRUE)
print("that")
sink()
...

The contents of test.txt now looks like this:

[1] "this"
[1] "that"

Don't forget the append = TRUE otherwise the contents will get overwritten which is probably not what you want.

sink() works, but there are easier and more elegant solutions now...

Use knitr (as it was intended)

I use knitr all the time for precisely the reason you're looking for a solution, namely to separate execution code from results.

Separate out your chunks and the following example (when knit!) will get you a document along the lines of what you're looking for:

---
title: "test"
author: "Canovice"
date: "22 December 2016"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(
    echo = FALSE,
    message = FALSE,
    warning = FALSE
)
```

```{r}
# rm(list = ls())
# you don't need this
# by default, each time you knit, knitr uses a clean session

# data manipulation
library(jsonlite); library(plyr); library(dplyr); library(data.table)

# data scraping
library(RSelenium); library(rvest); library(XML)

# parallel computing / faster code
library(foreach); library(doParallel); library(microbenchmark)
```

```{r}
print("this")
```

```{r}
# machine learning
library(rpart.plot); library(glmnet); library(rpart); library(gbm); 
library(nnet); library(caret); library(randomForest); 

# lineup optimization
library(lpSolve);  

# data visualization
library(ggplot2); library(reshape2)
```

The resulting document just displays the result of the code:

test

Canovice

22 December 2016

## [1] "this"

Use R Notebook

In RStudio versions > 1.0 you can use the new R Notebook format which works the same way as Rmarkdown knitr documents, except there's a handy button which allows you to display or hide code as you wish.

Have a look at this for example: https://rpubs.com/pssguy/220641

At the top of the page, and for each individual section, there is an option to show or hide the code and just leave the result.

Upvotes: 1

Adam Bethke
Adam Bethke

Reputation: 1038

Note that chunk options only affect the display of output when knitting. Sourcing code independent of knitting would require a different approach.

When you load packages, what you're seeing most of the time are messages, not source code. So to suppress things like ## Attaching package: 'dplyr' ..., you have to also add the option message = FALSE, so the chunk instructions would be {r , echo = FALSE, message = FALSE} to suppress both message and source code in the knit document.

Upvotes: 3

Related Questions