15

Is there a way to get R / RStudio to copy a plot to the clipboard with a custom size?

RStudio has this function, but you have to define the size everytime and there is some extra clicking which I am sure is avoidable.

I tried my best with saving as jpeg or else with file="clipboard" and then - after plotting - dev.off(). No error messages, but also nothing in the clipboard.

Here is an example:

data(mtcars)
jpeg(file = "clipboard",width = 800, height = 600, units = "px", pointsize = 12,
     quality = 100,
     bg = "white", res = NA, family = "", restoreConsole = T)
hist(mtcars$mpg)
dev.off()

Any ideas on how this can be achieved?

assylias
  • 321,522
  • 82
  • 660
  • 783
Jochen Döll
  • 383
  • 3
  • 11
  • 1
    I haven't got RStudio, so can't test it, but you should be able to do something like `hist(mtcars$mpg); savePlot("clipboard","wmf")` but this doesn't generalise to other file formats. – Miff Apr 25 '14 at 14:49
  • The correct answer depends on the operating system which so far has no been offered. – IRTFM Apr 25 '14 at 14:53
  • e.g. on os x simply click on the plot window and hit `Cmd-C`. Not sure about Windows. – Simon O'Hanlon Apr 25 '14 at 15:06
  • @SimonO'Hanlon there's a manual "copy" menu item in the Windows GUI, but my impression is that the OP wants a command-line tool. – Carl Witthoft Apr 25 '14 at 15:32
  • @CarlWitthoft I don't know the emoticon for embarrassed smiley, but imagine that's what I'm doing right now. – Simon O'Hanlon Apr 25 '14 at 15:33
  • Thanks for the comments so far. Some more information: - I am aware of the RStudio export. It works, but is too clickyclicky - My OS is windows 7 - savePlot("clipboard","wmf") does not work, I get the error message "Error in savePlot("clipboard", type = "wmf") : kann nur aus 'windows' Devices kopieren" (Last line is something like "can only copy from 'windows' devices") – Jochen Döll Apr 25 '14 at 15:49
  • That's correct: you have to display the plot in a window before that'll work. – Carl Witthoft Apr 25 '14 at 16:53
  • @CarlWitthoft: Ok. How to do this? – Jochen Döll Apr 25 '14 at 19:07
  • relevant: https://stackoverflow.com/questions/47221639/save-plot-without-showing-it-at-all – moodymudskipper Nov 08 '18 at 08:45

3 Answers3

17

The best way would be to be able to control the size in Rstudio, but as you have found out yourself from the Rstudio-website, Rstudio doesn't support that. The following code saves your plot to wmf. There is also a workaround to a save to bitmap, which involves some clicking, but at least you don't have to specify the size any more:

data(mtcars)
windows(800, 600, pointsize = 12) #opens a separate window with the size you want 
hist(mtcars$mpg) #draw to this (active) window
savePlot("clipboard", type="wmf") #saves plot to WMF

Unfortunately, it seems to be impossible to save to jpg format to the clipboard. You can copy it to a bitmap by going to this window, click CTRL-C and the graph is on the clipboard as bitmap with 800:600.

EDIT: The windows command only works on Windows.
For Mac, it should be replaced by: quartz(width=8,height=6,pointsize=12,dpi=100) (width/height in inches!)

For linux try x11(width=8,height=6,pointsize=12,dpi=100) (untested).

RHA
  • 3,677
  • 4
  • 25
  • 48
  • That "windows"-approach did the trick! This avoids the rescaling. Thanks RHA for the support! – Jochen Döll Oct 14 '15 at 13:40
  • Encountered the error `Error: could not find function "windows"`. – Shreta Ghimire Oct 15 '15 at 06:02
  • @ShretaGhimire then you are probably not working on windows, but on mac or linux. On mac, try `quartz(width=800,height=600,pointsize=12)`, but I can't it test today because I have no access to a mac. – RHA Oct 15 '15 at 07:20
  • @RHA i am using RStudio on lInux. – Shreta Ghimire Oct 15 '15 at 07:27
  • @ShretaGhimire I don't know how to open a graphics device in linux, but some googling suggests it may be `x11(width=800,height=600,pointsize=12)` – RHA Oct 15 '15 at 08:10
  • type="wmf" is not needed in savePlot("clipboard", type="wmf"). @RHA – Ven Yao Oct 15 '15 at 12:24
  • @Andrew: You are right, it's the default and to only option for clipboard. I added it to make extra clear that this is a vector format that gets copied. You (or anyone else) wouldn't by any change know how to copy a bitmap (or why it is not possible)? – RHA Oct 15 '15 at 14:19
  • @RHA I wonder how this would work with RStudio Server (on a linux) machine? `x11()` tells me it's `unable to start device X11cairo` – stats-hb Sep 04 '18 at 11:46
  • 1
    @stats-hb I have no idea. The X11 command is apparently recognized, but fails to execute. – RHA Sep 04 '18 at 12:02
2

I know this is an old post, but recently looking to do the same I came across this Gist which worked well:

https://gist.github.com/dpashouwer/4223903d3ed5783158b7e63992155649

library(tidyverse)

gg_to_clipboard <- function(plot = last_plot(), width = 1000, height = 600, pointsize = 40){
    win.graph(width = width, height = height, pointsize = pointsize)
    plot %>% print()
    savePlot("clipboard", type = "wmf")
    dev.off()
}

ggplot(data = mtcars, aes(x = mpg)) + geom_histogram()

gg_to_clipboard()

Making the function makes it very simple.

Nick
  • 179
  • 1
  • 11
0

With Windows and RStudio, you click Export, click Copy Plot to Clipboard, and Copy Plot.

Then, paste into Word or PowerPoint or whatever.

No need to change sizes unless you want to.

This is not command line, but hardly seems onerous.

lawyeR
  • 7,488
  • 5
  • 33
  • 63
  • 6
    Yes, you are right. But the thing in RStudio is that the plot will always have the dimensions of your little "preview"-plot window. This is intended by the creators of RStudio, because most users want an exact copy of the preview plot. But I want a bigger export... and this for data that changes, so a command line "copy to clipboard in 800x600" would be a great help... – Jochen Döll Apr 28 '14 at 13:15