Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hiding the R code in Rmarkdown/knit and just showing the results

In my R Markdown documents, I sometimes want to just generate a report without showing the actual code (specially when I send it to my boss). How can I hide the R code and just show the graph and results?

For example:

--- output: html_document ---  ```{r fig.width=16, fig.height=6} plot(cars) ``` 

This shows both the commands and the plot. How can I remove the commands from my HTML report?

like image 721
Mark Avatar asked Oct 26 '12 16:10

Mark


People also ask

How do I hide code in R Markdown but show output?

The initial line in a code chunk may include various options. For example, echo=FALSE indicates that the code will not be shown in the final document (though any results/output would still be displayed). You use results="hide" to hide the results/output (but here the code would still be displayed).

How do you hide code in R Markdown knitting?

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 R Markdown?

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 stop R from running a chunk?

The shortcut to interrupt a running process in R depends on the R software and the operating system you are using. However, if you are using RStudio on a Windows computer, you can usually use Esc to stop a currently executing R script. Then, we can press Esc to interrupt the loop.


2 Answers

Sure, just do

```{r someVar, echo=FALSE} someVariable ``` 

to show some (previously computed) variable someVariable. Or run code that prints etc pp.

So for plotting, I have eg

### Impact of choice of .... ```{r somePlot, echo=FALSE} plotResults(Res, Grid, "some text", "some more text") ``` 

where the plotting function plotResults is from a local package.

like image 97
Dirk Eddelbuettel Avatar answered Dec 02 '22 02:12

Dirk Eddelbuettel


Might also be interesting for you to know that you can use:

{r echo=FALSE, results='hide',message=FALSE} a<-as.numeric(rnorm(100)) hist(a, breaks=24) 

to exclude all the commands you give, all the results it spits out and all message info being spit out by R (eg. after library(ggplot) or something)

like image 27
Geoffrey Stoel Avatar answered Dec 02 '22 02:12

Geoffrey Stoel