r - 在R,RStudio中同时显示和保存绘图

标签 r rstudio

我需要将图另存为.png并同时显示图而不重复代码。
有一个优雅的方法吗?
在RStudio for MAC上工作。

我可以让它像波纹管一样工作,但我不喜欢它。

#Step1: save the plot
png("myplot.png")
#plot code
dev.off()

#Step2: to display the plot
#plot code (again!) to display it in RStudio


干杯,
I.M.

最佳答案

我发现之前的答案不完整。深入研究了如何在默认的RStudio“图”窗口中显示图并将图同时保存为“ png”。

万一有一天,有人可能遇到相同的问题,下面是逐行的操作方法:

R:> 
R:> dev.list()                  # fresh start. no graphical devices available at this point
NULL
R:> dev.cur()                   # no current device at this point
null device 
      1 
R:> dev.new()                   # open graphical devices
NULL
R:> dev.list()                  # list them
     RStudioGD quartz_off_screen 
        2                 3 
R:> png("plot50.png")           # open an offscreen device as png. New device should be number 4
R:> dev.list()                  # the list of all graphical devices includes the newly created device, number 4
    RStudioGD quartz_off_screen quartz_off_screen 
          2          3               4 
R:> dev.cur()                   # NOTE: the new created device(number 4) becomes "current" automatically,
quartz_off_screen               # as soon as it has been created
            4 
R:> dev.set(which = 2)          # switch back to device 2 used to display the plot in the default RStudio 
                                # "Plots" window
RStudioGD 
    2 
R:> dev.cur()                   # indeed, RstudioGD becomes the current device after the switch step from above
RStudioGD 
    2 
R:> dev.list()                  # just a check on all available devices. device 4 still in the list after 
                                # the switch
    RStudioGD quartz_off_screen quartz_off_screen 
            2           3                 4 
R:> plot(c(1:100))              # plot an example. It will be displayed in "Plots" window of RStudio
R:> dev.list()                  # just a check on all the available devices
   RStudioGD quartz_off_screen quartz_off_screen 
      2                 3                 4 
R:> dev.copy(which = 4)         # copies from current device(RStudioGD) to device 4. It automatically sets
   quartz_off_screen            # device 4 as current
         4 
R:> dev.cur()                   # indeed , device 4 is the current device
  quartz_off_screen 
         4 
R:> dev.off()                   # close device 4. IMPORTANT: AT THIS POINT the plot is saved as
    RStudioGD                   # png("plot50.png") in the current working directory.
                                # Three actions takes place at this point, all at once:
                                # 1. closes device 4
                                # 2. save the plot as "plot50.png"
                                # 3. sets the de.next() (which is RStudioGD) as the current device
       2 
R:> dev.cur()                   # RStudioGD becomes current as soon as device 4 has been closed down.
   RStudioGD 
       2 
R:> 

关于r - 在R,RStudio中同时显示和保存绘图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23621012/

相关文章:

r - 如何在 RStudio 中查找并替换为新行(不是新行字符)?

java - 我如何使用java输出R图

r - igraph 解决具有不同节点大小 r 的重叠节点

r - tidyr 宽到长 : repeated measures and efficiency

r - 如何在 Windows ubuntu 20.4 中安装 rstudio

git - RStudio:git 克隆另一个分支

R:如何从列表的每个内部元素中删除第一个元素而不将其转换为矩阵?

amazon-web-services - 无法在RStudio中处理大型文件

r - 将 RStudio 中的选择发送到终端的键盘快捷键

c++ - R 崩溃/中止使用带有 NA 输入的 Rcpp