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.
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?
@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
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.
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)
```
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.
---
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.