title: Return plot from R function
body: <p>Why won't my R function return or print a plot? The code is below. All of the code seems to work fine, except for the plot. No matter what I do, I can't get R to create the plot when the function is called. Looking around online, I can't find any reason why this wouldn't work.</p>
  
  <pre><code>powerc.fun &lt;- function(n,sigma,r){
  
  a &lt;- 0.05
  d &lt;- seq(-20,20,2)
  
  power &lt;- rep(NA,length(d))
  p.lab &lt;- rep(NA,length(d))
  
  for (j in 1:length(d)){
  
    mu1 &lt;- 110
    mu2 &lt;- mu1-d[j]  
  
    reject &lt;- rep(NA,r)
  
    for (i in 1:r){
  
      sample1 &lt;- rnorm(n,mu1,sigma)
      sample2 &lt;- rnorm(n,mu2,sigma)
  
      sample.t &lt;- t.test(sample1,sample2)
      p.val &lt;- sample.t[3]
  
      reject[i] &lt;- p.val&lt;a 
  
      power[j] &lt;- sum(reject)/length(reject)
      p.lab[j] &lt;- paste('d=',d[j],sep='')
  
    }}
  
  d.power &lt;- cbind(d,power)
  
  return(d.power)
  
  p.plot &lt;- plot(d.power[,1], d.power[,2], type="l", xlab=bquote(H[a]), ylab="Power", main="Power Calculations for Two Sample T Test")
  
  print(p.plot)
  return(p.plot)
  
  }
  </code></pre>
  
  <p>Any ideas?</p>