How to include only the Console or the Graph in RMarkdown or Rnotebook

rnotebook

#1

There are certain function in R that produce both output on console and output on graph. If I need to include such functions in Notebook. How do I choose between console output, graph and both.

like this code

library(DescTools)
Desc(mtcars)

This produces 11 console output and 11 graphs or

a simple one

library(DescTools)
Desc(mtcars$mpg)

would produce at least a 1 console output and 1 Graph. Now How would I choose only the graph to be displayed in Markdown document or only the console output to be displayed or if I want to include both of them.

Please let me know how Should I approach it..


#2

I'm not exactly sure what you mean by console and graph? The best way to approach this will depend on the structure of your document, so could you please include a reprex, or example that's sufficiently similar as to be useful to you?


#3

@Anantadinath there are a few different options that might help:

  • {r, results="hide"} - The chunks is run but all results are hidden. The code shows in the doc, however.
  • {r, include=FALSE} - the code is run but neither the code or the results are shown
  • {r, echo=FLASE} - The code is not shown, but results are

If you want a code chunk to run and not produce console output but you DO want to see the resulting graphs, you can sink() to /dev/null` like the following:

sink("/dev/null")
Desc(mtcars)
sink(); 

sinking to /dev/null, for some reason has always struck me as funny. So much so that I named a boat /dev/null

Boston Whaler's are advertised as "unsinkable" so I always found having an unsinkable named /dev/null amusing. I am easily amused.


#4

Thanks for replying @mara. I already gave a simple code to explain the situation.

Just run this command on your computer and you will see some output on your console like mean median and stuff and a graph generated as well..

library(DescTools)
Desc(mtcars)

I hope this is the simplest I could explain.


#5

Thanks for replying. It works fine mostly this is exactly what I needed but if I need only the console output and not the graph it doesn't help me with that.

Please guide me what should I do


#6

Yep, I guess that's the one permutation I left out of my list above. Here's all you need:

{r, fig.show = 'hide'}

cheers.


#7

Thanks for this tip. Now I feel like I am more advanced than yesterday

thanks again.

:grinning::grinning::grinning::grinning::grinning:


#8

Hi @jdlong,

I am really sorry for replying on this old thread but I faced the same problem and I was looking at the code again today. And it didn't work.

even though I have involved fig.show='hide' it still shows me figures.


#9

Huh, that's odd. When I have the following in an RMarkdown document:

---
title: "`fig.show` Test"
output: html_document
---

This chunk should produce a plot:
```{r}
plot(cars)
```

This chunk should produce console output, but no plots:

```{r, fig.show='hide'}
library(DescTools)
Desc(cars)
```

I get exactly what's expected:
http://rpubs.com/jcblum/community-7440

Right now I can't see how your Rmd is different from mine, but it's tough to read the source from a screenshot. Could you post it?


#10

knit the document. Yes, if you run the code in the block manually it will show the result in the viewer pane. But it will not include it in the document when you knit it.


#11
---
title: "R Notebook"
output: html_notebook
---

This is an [R Markdown](http://rmarkdown.rstudio.com) Notebook. When you execute code within the notebook, the results appear beneath the code. 

Try executing this chunk by clicking the *Run* button within the chunk or by placing your cursor inside it and pressing *Ctrl+Shift+Enter*. 

```{r}
plot(cars)

plot(mtcars)

Add a new chunk by clicking the Insert Chunk button on the toolbar or by pressing Ctrl+Alt+I.

When you save the notebook, an HTML file containing the code and output will be saved alongside it (click the Preview button or press Ctrl+Shift+K to preview the HTML file).

The preview shows you a rendered HTML copy of the contents of the editor. Consequently, unlike Knit, Preview does not run any R code chunks. Instead, the output of the chunk when it was last run in the editor is displayed.

library(DescTools)
Desc(cars)


this is the code and thanks @jdlong you are right it doesn't print output when I knit it but it shows it in viewer pane in notebook. It's a small difference in markdown and notebook.

thanks again. can I also choose which plot to show in Markdown when It creates multiple plot.