Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show code but hide output in RMarkdown?

I want my html file to show the code, but not the output of this chunk:

```{r echo=True, include=FALSE} fun <- function(b)     {     for(a in b)         {print(a)         return(a * a)}     } y <- fun(b) ``` 

When I run the code, i need the print to see the progress (it is quite a long function in reality).

But in the knitr file, I use the output in a further chunk, so I do not want to see it in this one (and there's no notion of progress, since the code has already been run).

This echo=True, include=FALSE here does not work: the whole thing is hidden (which is the normal behavior of include=FALSE).

What are the parameters I could use to hide the prints, but show my code?

like image 326
Laurent Avatar asked Dec 08 '17 08:12

Laurent


People also ask

How do I display code but not result in R markdown?

R Markdown still runs the code in the chunk, and the results can be used by other chunks. echo = FALSE prevents code, but not the results from appearing in the finished file. This is a useful way to embed figures. message = FALSE prevents messages that are generated by code from appearing in the finished file.

How do I hide source code in R markdown?

Hide source code: ```{r, echo=FALSE} 1 + 1 ``` Hide text output (you can also use `results = FALSE`): ```{r, results='hide'} print("You will not see the text output.") ``` Hide messages: ```{r, message=FALSE} message("You will not see the message.") ``` Hide warning messages: ```{r, warning=FALSE} # this will generate ...

How do I show output in Rmarkdown?

If you prefer to use the console by default for all your R Markdown documents (restoring the behavior in previous versions of RStudio), you can make Chunk Output in Console the default: Tools -> Options -> R Markdown -> Show output inline for all R Markdown documents .

How do I hide code lines in R?

In latest R Studio (I work with v1. 0+), you can also click Ctrl + Shift + R , or go to menu Code > Insert Section . It will show a popup for your section name and add it. The title will then have a tiny arrow to hide the entire section until the next title.


1 Answers

As @ J_F answered in the comments, using {r echo = T, results = 'hide'}.

I wanted to expand on their answer - there are great resources you can access to determine all possible options for your chunk and output display - I keep a printed copy at my desk!

You can find them either on the RStudio Website under Cheatsheets (look for the R Markdown cheatsheet and R Markdown Reference Guide) or, in RStudio, navigate to the "Help" tab, choose "Cheatsheets", and look for the same documents there.

Finally to set default chunk options, you can run (in your first chunk) something like the following code if you want most chunks to have the same behavior:

```{r setup, include=FALSE} knitr::opts_chunk$set(echo = T,                       results = "hide") ``` 

Later, you can modify the behavior of individual chunks like this, which will replace the default value for just the results option.

```{r analysis, results="markup"} # code here ``` 
like image 161
Nova Avatar answered Sep 29 '22 04:09

Nova