1

I am new to rmarkdown and I would like to show a plot which is normally produced in a new window. This plot is called from a function that displays it in a new window using plot.new() and dev.new() and also adds several plots in that window. How can I return this to the report?

the following does not return the plots...

```{r  fig.keep='all', fig.width=10, fig.height=5}

Draw_matrix_plots(data)

```

and the function skeleton I am calling which draws four plots in a new window:

Draw_matrix_plots <- function(data){
  plot.new()
  dev.new(width=7, height=8)
  layout(matrix(c(1,2,3,4), 2, 2, byrow = TRUE),heights=c(3,3))
  hist(data$A)
  hist(data$B)
  hist(data$C)
  hist(data$D)
}

Thanks

R. Prost
  • 1,958
  • 1
  • 16
  • 21
  • Hi, is this a custom function ? Or some example coming from a package ? For some graphical objects (ggplot) you can use print. I you are working in sweave you can save your plots using `grDevices::savePlot("myplot.png", type = "png")` and then call it using \includegraphics. Here's an [example](https://stackoverflow.com/questions/46920038/how-to-get-figure-floated-surrounded-by-text-etc-in-r-markdown/46962362#46962362) on how to include a figure saved in one of the folders. – Cedric Dec 19 '17 at 19:36
  • Here is a simpler way for markdown https://stackoverflow.com/questions/10517020/how-to-import-local-image-using-knitr-for-markdown – Cedric Dec 19 '17 at 19:40
  • Yes it is a custom function which normally takes my data and makes several plots together in a new window. I would rather not save it as a png first (if possible), but display it directly like you would normally do with a single plot in the main window. I have edited the question to show a skeleton of the function. Thanks – R. Prost Dec 20 '17 at 07:19

1 Answers1

0

I think you just don't need the dev.new(), the following code produce

 ---
title: "Test"
ouptut: pdf_document
---

# R code

```{r  fig.keep='all', fig.width=10, fig.height=5}
Draw_matrix_plots <- function(){
  layout(matrix(c(1,2,3,4), 2, 2, byrow = TRUE),heights=c(3,3))
  hist(rnorm(100))
  hist(rnorm(100))
  hist(rnorm(100))
  hist(rnorm(100))
}
Draw_matrix_plots()

```

test You can check that the figures are created in the /figure folder. I have the same with html_document as an output. Does that help ?

Cedric
  • 2,412
  • 17
  • 31
  • thanks for the followup. All your options are good as work around, and are great help. Either save it first as a picture or removing the dev.new. They both work. Ideally I would like to be able to capture the new window rather than the workspace window though :/. because when I use my code I would have to add the new.dev before every function call... Or is it bad practice to put the dev.new in my function ? Also because of the plot.new I had, it adds a blank graphic to the list of pictures :/.. – R. Prost Dec 20 '17 at 09:26
  • When you call plot.default, it calls first to the `plot.new()` or `frame()`function. This one will finalize the plot device you are in and advance the graphics devices to a new graphic frame. So basically in most cases R will handle that for you and you won't need dev.new(). What [knitr does](https://github.com/yihui/knitr/releases/download/doc/knitr-manual.pdf) is to test in the code if you have plots, and then save them as images (just like we have done manually). The `dev` option in the chunk allows you to choose which type of graphics device will be opened. – Cedric Dec 20 '17 at 10:02
  • 1
    At the end I removed the dev.new in my functions. If I want the results outside of the normal R studio window then I will have to tell it manually. I am used to having multiple windows opened to compare them but I can always do it manually or go left or right with the rstudio arrows. This way it works great for Knitr and I think it might actually be better practice. If someone passes by and has a solution to extract it from the new window please fill free to add an answer :). For now I am good with this answer. Thanks ::thumb_up:: – R. Prost Dec 20 '17 at 12:35
  • Thanks, you can mark [this answer as correct](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) . You will unselect it if you get a better answer. – Cedric Dec 20 '17 at 12:42