Can example() code stop the example without generating an error?

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Can example() code stop the example without generating an error?

Henrik Bengtsson-2
Hi,

does anyone know if it is possible to write example code (in Rd
examples) such that one can stop the example without generating an
error?  Example:

code A
if (cond)
  niceStop()
code B

I know this sounds weird, but I would like some of my Rd examples to
run if and only if another package is available or if a certain large
Affymetrix data file is available.  One can put all of the example in
a function and return from the function if the package is not
available, but then all object assigned are lost. My best/cleanest
solution right now is to use break in a dummy for loop. Examples:

for (z in 1) {

code A
if (cond)
  break
code B

}

Other suggestions?  The solution must off course pass R CMD check.

/Henrik

PS. I know example() calls source(), but I'm not sure how R CMD check
does it. DS.

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Can example() code stop the example without generating an error?

Uwe Ligges
Henrik Bengtsson wrote:

> Hi,
>
> does anyone know if it is possible to write example code (in Rd
> examples) such that one can stop the example without generating an
> error?  Example:
>
> code A
> if (cond)
>   niceStop()
> code B


What about

code A
if(cond){
   code B
}

But maybe I do not get your point.

Uwe Ligges


> I know this sounds weird, but I would like some of my Rd examples to
> run if and only if another package is available or if a certain large
> Affymetrix data file is available.  One can put all of the example in
> a function and return from the function if the package is not
> available, but then all object assigned are lost. My best/cleanest
> solution right now is to use break in a dummy for loop. Examples:
>
> for (z in 1) {
>
> code A
> if (cond)
>   break
> code B
>
> }
>
> Other suggestions?  The solution must off course pass R CMD check.
>
> /Henrik
>
> PS. I know example() calls source(), but I'm not sure how R CMD check
> does it. DS.
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Can example() code stop the example without generating an error?

Brian Ripley
On Tue, 14 Mar 2006, Uwe Ligges wrote:

> Henrik Bengtsson wrote:
>
>> Hi,
>>
>> does anyone know if it is possible to write example code (in Rd
>> examples) such that one can stop the example without generating an
>> error?  Example:
>>
>> code A
>> if (cond)
>>   niceStop()
>> code B
>
>
> What about
>
> code A
> if(cond){
>   code B
> }
>
> But maybe I do not get your point.

That is how many examples in base R do it.  The only thing to remember is
that you have to explicitly print inside any such test.

>
> Uwe Ligges
>
>
>> I know this sounds weird, but I would like some of my Rd examples to
>> run if and only if another package is available or if a certain large
>> Affymetrix data file is available.  One can put all of the example in
>> a function and return from the function if the package is not
>> available, but then all object assigned are lost. My best/cleanest
>> solution right now is to use break in a dummy for loop. Examples:
>>
>> for (z in 1) {
>>
>> code A
>> if (cond)
>>   break
>> code B
>>
>> }
>>
>> Other suggestions?  The solution must off course pass R CMD check.
>>
>> /Henrik
>>
>> PS. I know example() calls source(), but I'm not sure how R CMD check
>> does it. DS.
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

--
Brian D. Ripley,                  [hidden email]
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel