[R] Clash between alr3 and AlgDesign. (Was: Re: Second & subsequent calls to function fails. Please help debug.)

Duncan Murdoch murdoch at stats.uwo.ca
Sun Mar 30 19:36:15 CEST 2008


On 30/03/2008 10:06 AM, Michael Kubovy wrote:
> I've been trying to dynamically detach and attach things in my Sweave,  
> in order to circumvent the problem.
> 
> Here is my first attempt:
> pkg <- 'package:AlgDesign'
> p <- is.na(match(pkg, search()))
> ifelse(p  == FALSE, detach(pkg), NA)

You can't use detach this way.  It is fine to say 
detach("package:AlgDesign") or detach(AlgDesign), but the way detach is 
written, you're attempting to detach something named "pkg".

This rewrite of your code is untested, but I think it should work:

pkg <- 'package:AlgDesign'
p <- match(pkg, search())
if (!is.na(p)) detach(pos=p)
require(alr3)

I've also changed your ifelse() to if (), and not just because I don't 
need an else clause:  ifelse() is designed to do computations on 
vectors, if() is designed for flow control.  I think we want flow 
control here.

Duncan Murdoch

> require(alr3)
> 
> My first two runs show that I've done something wrong:
> 
>  > Sweave('20080331.Rnw')
> Writing to file 20080331.tex
> Processing code chunks ...
>   1 : term hide (label=setup)
>   2 : echo term verbatim (label=oatvar)
>   3 : echo term verbatim (label=oatvar1)
>   4 : echo term verbatim (label=oat2wt)
>   5 : echo term verbatim (label=oat2wt)
>   6 : echo term verbatim (label=lm)
>   7 : echo term verbatim (label=diag)
>   8 : echo term verbatim (label=tukey)
>   9 : echo term verbatim (label=nonad)
> 10 : echo term verbatim (label=efficiency)
> 11 : echo term verbatim (label=wear)
> 12 : echo term verbatim (label=wearX)
> 13 : echo term verbatim (label=gyd)
> Loading required package: crossdes
> Loading required package: AlgDesign
> 14 : echo term verbatim (label=2wt)
> 15 : echo term verbatim (label=ablm)
> 16 : echo term verbatim (label=abaov)
> 17 : echo term verbatim (label=abTukey)
> 18 : echo term verbatim (label=effAb)
> 19 : echo term verbatim (label=rabbit)
> 20 : echo term verbatim (label=rabbit2)
> 21 : echo term verbatim (label=rabbit2wt)
> 
> You can now run LaTeX on '20080331.tex'
> There were 42 warnings (use warnings() to see them)
>  > Sweave('20080331.Rnw')
> Writing to file 20080331.tex
> Processing code chunks ...
>   1 : term hide (label=setup)
>   2 : echo term verbatim (label=oatvar)
>   3 : echo term verbatim (label=oatvar1)
>   4 : echo term verbatim (label=oat2wt)
>   5 : echo term verbatim (label=oat2wt)
>   6 : echo term verbatim (label=lm)
>   7 : echo term verbatim (label=diag)
>   8 : echo term verbatim (label=tukey)
> 
> Error:  chunk 8 (label=tukey)
> Error in detach(pkg) : invalid name
>  > pkg
> [1] "package:AlgDesign"
> 
> (1) I should learn how to fix the above code.
> 
> (2) In general, in preparing scripts, perhaps one should just ditch  
> all the loaded packages at the beginning of each run, so as to insure  
> that users won't get bitten by packages I had loaded but didn't do so  
> explicitly in the Sweave script, *and* it would solve the current  
> problem as well. Is there a straightforward way to do that?
> 
>



More information about the R-help mailing list