title: Save complicated plot() to object
body: <p>I have a series of commands that create a vibration of effects plot. Now, I want to assign the plot to an object (to later make it downloadable via Shiny). However, that does not seem possible. When I try to save the plot to an object, the object returns &quot;Null&quot; and likewise if I try to save it it saves an empty .png file.</p>
  <p>See below for the function and some example data.</p>
  <pre><code>#some packages
  if (!require(&quot;pacman&quot;)) install.packages(&quot;pacman&quot;)
  pacman::p_load(MASS, tidyverse, ggplot2, dplyr, shiny, here, BayesFactor, ggpubr, effsize, DescTools, rqPen)
  
  #plot of p value vs effect size vibration plot
  #https://figshare.com/articles/Code_data_and_analysis_script_for_A_Traveler_s_Guide_to_the_Multiverse_Promises_Pitfalls_and_a_Framework_for_the_Evaluation_of_Analytic_Decisions_/12089736 main source
  
  multiverse.vibration &lt;- function(effsize, statistic, alpha = 0.05, threshold = 6, type = c(&quot;frequentist&quot;)){
    
    #assign colours schemes
    point.color &lt;-  rgb(0,76,153, alpha=80, maxColorValue=255)
    contour.color = rgb(60,130,180, alpha=130, maxColorValue=255)
    #vibrations
    vibrations &lt;-  kde2d(effsize, -log10(statistic), n=50)
    
    if (type == &quot;frequentist&quot;){
      
      #do the plotting. 
      plot(effsize, -log10(statistic), type=&quot;n&quot;, las=1, xlab=expression(paste(&quot;Effect size&quot;)), ylab=expression(paste(&quot;-log&quot;[10],&quot;(&quot;,italic(&quot;p&quot;),&quot;-value)&quot;)), main=&quot;&quot;, cex.lab=1.35, cex.axis=1.2 ) ####the label of the y axis gets cut off by the picture for no reason whatsoever####
      #add quantile lines
      abline(v=as.numeric(quantile(effsize, probs=0.5)), lty=3, lwd=1.8, col=&quot;gray70&quot;) 
      abline(h=-log10(as.numeric(quantile(statistic, probs=0.5))), lty=3, lwd=1.8, col=&quot;gray70&quot;) 
      #add data points
      points(effsize, -log10(statistic), pch=16, col=point.color, cex=1.5) 
      #add &quot;vibrations&quot;
      contour(vibrations, drawlabels=FALSE, nlevels=5, lwd=1.7, col=contour.color, add=TRUE) 
      text(as.numeric(quantile(effsize, probs=0.5)), max(-log10(statistic)), &quot;50&quot;, pos=2, col=&quot;gray40&quot;, cex=1) 
      text(max(effsize), -log10(as.numeric(quantile(statistic, probs=0.5))), &quot;50&quot;, pos=3, col=&quot;gray40&quot;, cex=1) 
      #add alpha line and label
      abline(h=-log10(alpha), lty=3, lwd=1.5, col=&quot;red&quot;) 
      text(min(effsize), -log10(alpha), expression(paste(alpha)), pos = 1, cex = 1, col = &quot;red&quot;)
     
    }
  #...function simplified
  }
  
  
  #and below some data
  
  df_multiverse &lt;- structure(list(transformation = structure(c(1L, 1L, 1L, 1L, 1L, 
  1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c(&quot;square&quot;, 
  &quot;squareroot&quot;), class = &quot;factor&quot;), datatrimming = structure(c(2L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c(&quot;mad&quot;, 
  &quot;notrimming&quot;), class = &quot;factor&quot;), fixedtrimming = structure(c(1L, 
  1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = &quot;nofixedtrimming&quot;, class = &quot;factor&quot;), 
      min = c(NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
      NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
      NA_real_, NA_real_, NA_real_, NA_real_, NA_real_), max = c(NA_real_, 
      NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
      NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, NA_real_, 
      NA_real_, NA_real_, NA_real_), DispersionMeasure = c(NA, 
      2, 2.5, 3, 3.5, 4, 4.5, 5, NA, 2, 2.5, 3, 3.5, 4, 4.5, 5), 
      NumberOfTrials = c(2481, 2017, 2089, 2152, 2202, 2235, 2271, 
      2292, 2481, 2017, 2089, 2152, 2202, 2235, 2271, 2292), df = c(21, 
      21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21, 21
      ), t.value = c(0.834352731211477, -1.89143806501942, -2.06164045172582, 
      -2.29402139720537, -2.20170894686594, -1.30874979649765, 
      -1.46580636234517, -0.933033039387291, -0.381340656529586, 
      -2.65553835404059, -2.70367808996487, -2.88191068442976, 
      -2.89698876130645, -2.31203065738409, -2.40524937843272, 
      -1.99997820996895), p.value = c(0.413473232348569, 0.0724397922282673, 
      0.0518359697127152, 0.0322027617938105, 0.0390026786336539, 
      0.204761347160827, 0.157515139319996, 0.361407402521166, 
      0.706781450011369, 0.0147953018060795, 0.013300947944711, 
      0.00892256290108781, 0.0086233125398353, 0.0310102245266004, 
      0.0254623057912856, 0.0586025361696588), estimate = c(0.0513517727014905, 
      -0.138440596771433, -0.152826845040145, -0.172473124495872, 
      -0.150035258885051, -0.106059860414446, -0.0904972867538278, 
      -0.0636909905658258, -0.0224006885730891, -0.132591874705722, 
      -0.141473579509691, -0.162307800901886, -0.156924178280938, 
      -0.138723145332572, -0.124862443444392, -0.109932966289113
      )), row.names = c(&quot;df&quot;, &quot;df1&quot;, &quot;df2&quot;, &quot;df3&quot;, &quot;df4&quot;, &quot;df5&quot;, 
  &quot;df6&quot;, &quot;df7&quot;, &quot;df8&quot;, &quot;df9&quot;, &quot;df10&quot;, &quot;df11&quot;, &quot;df12&quot;, &quot;df13&quot;, &quot;df14&quot;, 
  &quot;df15&quot;), class = &quot;data.frame&quot;)
  
  #and below a call
  object &lt;- multiverse.vibration(df_multiverse$estimate, df_multiverse$p.value, type = &quot;frequentist&quot;)
  
  #Now I try to save it
  svg(file = &quot;Figure 1.svg&quot;, width = 9, height = 9, antialias = &quot;gray&quot;)
  object
  dev.off()
  #empty file, does not save plot.
  </code></pre>
  <p>My goal is to save the plot to an object in a way that later allows me to download the object via some command.</p>