[R] resume on error

R. Michael Weylandt michael.weylandt at gmail.com
Mon Mar 12 13:42:22 CET 2012


 Close the device anyways; test for an error as Sarah (and the help
pages) describe and use unlink() to delete the resultant (empty) file
if the plot call fails.

Michael

On Mon, Mar 12, 2012 at 8:30 AM, Alaios <alaios at yahoo.com> wrote:
> Thanks a lot for the answer..
> As you can see my chain is the following
> open a pdf device.
> plot and
>
> close the device
>
> pdf(file=filename, width=width)
> try(plot_per_band_ (list(get(varlist[[k]])), i, j,meanPlot=TRUE))
> dev.off()
>
> I would like if plot fails (so try returns try-error) the file to not be created. How should I think doing that in R?
>
> B.R
> Alex
>
>
>
> ________________________________
>  From: Sarah Goslee <sarah.goslee at gmail.com>
>
> Cc: R help <R-help at r-project.org>
> Sent: Monday, March 12, 2012 12:24 PM
> Subject: Re: [R] resume on error
>
>
>> Thanks a lot.
>> How I can use try though for plots. There are times where the plot function will fail
>>
>>  pdf(file=filename, width=width)
>>  try(plot_per_band(list(get(varlist[[k]])), i, j, l, datalabels=seq(1:length(varlist))))
>> dev.off()
>>
>> the code above does not print anything.
>
> That's a long way from a reproducible example.
>
> What do you expect to happen if plot() fails? Assign the output of
> try() to an object.
> Check whether that object is of class "try-error" and if so, do
> whatever you want
> the failure case to be.
>
> Sarah
>
>> B.R
>> Alex
>>
>>
>>
>> ________________________________
>>  From: R. Michael Weylandt <michael.weylandt at gmail.com>
>>
>> Cc: R help <R-help at r-project.org>
>> Sent: Sunday, March 11, 2012 12:56 AM
>> Subject: Re: [R] resume on error
>>
>> ? try or ? tryCatch
>>
>> Michael
>>
>>
>>> Dear all,
>>> I would like to ask you how I can catch an error on R and then ask it to resume.
>>>
>>> For example I have a large for loop and I know for a small number inside that loop there will be errors. How I can ask in that case from R just to ignore it and return back to the loop?
>>>
>>> I would like to thank you in advance fro your help
>>>
>>> B.R
>>> Ale
>>>
> --
> Sarah Goslee
> http://www.functionaldiversity.org
>        [[alternative HTML version deleted]]
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list