0

Can anyone tell me how to use 'try' command for a block of commands? Right now I'm using try for each command like this:

try(m<-MAplot(genes(cuff),args[1],args[2]))

try(png('MA_plot.png'))

try(m)

try(dev.off ())

try(print("MA_plot"))

EDIT:

try({
disp<-dispersionPlot(genes(cuff))
png('dispersion_plot.png')
disp
dev.off ()
print("dispersion_plot")



genes.scv<-fpkmSCVPlot(genes(cuff))
png('SCV_plot.png')
genes.scv
dev.off ()
print("SCV_plot")



dens<-csDensity(genes(cuff))
png('density_plot.png')
dens
dev.off ()
print("density_plot")

})

With this also I'm not getting plots..

1 Answers1

2

You can simple wrap multiple expressions using {} (curly braces). But remember why are you trying to use try and define your purpose clearly.

options(show.error.messages = FALSE)

To save a plot, 1st you need to create the graphics with png() and then plot the plot (while your code has flipped it hence didn't work)

Updated answer to save multiple plots:

try({
  png('dispersion_plot.png');

  disp<-plot(1:100);
  disp;
  dev.off();
  print("disersion_plot");

  cat('success 1');

  png('SCV_plot.png')

  genes.scv<-plot(100:1000)
  genes.scv
  dev.off ()
  print("SCV_plot")

  cat('success 2');

})
amrrs
  • 6,215
  • 2
  • 18
  • 27
  • For multiple PNGs it's not creating images. It's running smoothly though.. :( try({ disp<-dispersionPlot(genes(cuff)) png('dispersion_plot.png') disp dev.off () print("dispersion_plot") genes.scv<-fpkmSCVPlot(genes(cuff)) png('SCV_plot.png') genes.scv dev.off () print("SCV_plot") dens<-csDensity(genes(cuff)) png('density_plot.png') dens dev.off () print("density_plot") }) – Anupriya Verma Feb 01 '18 at 12:13
  • @AnupriyaVerma Can you please update this in the question so that I can see it better? – amrrs Feb 01 '18 at 12:19
  • @AnupriyaVerma And do you want to save multiple plots? – amrrs Feb 01 '18 at 12:19
  • I apologize @amrrs .. I edited my post now. Yes, I want to save multiple plots in multiple files. – Anupriya Verma Feb 01 '18 at 12:56