Skip to content

Second & subsequent calls to function fails. Please help debug.

10 messages · Duncan Murdoch, Gabor Grothendieck, Michael Kubovy +1 more

#
On 29/03/2008 9:47 PM, Michael Kubovy wrote:
I don't see anything obvious, so I guess it's the usual advice:

Use traceback() to see what function was trying to do the parsing. 
Debug that function, and see why it was trying to parse that string.
If that doesn't make it obvious, put together a minimal reproducible 
version that others can try, and see if anyone else can spot the problem.

Duncan Murdoch
#
Thanks, Duncan,

I started a new session under the suspicion that packages were clashing.
#########
# RUN 1
#########
require(faraway)
# Loading required package: faraway
data(oatvar)
ov.lm <- lm(yield ~ variety + block, oatvar)
require(alr3)
# Loading required package: alr3
out <- tukey.nonadd.test(ov.lm)
#########
# RUN 1 OK
# RUN 2
#########
require(HH)
#Loading required package: HH
#Loading required package: lattice
#
#Attaching package: 'lattice'
#
#
#	The following object(s) are masked from package:faraway :
#
#	 melanoma
#
#Loading required package: grid
#Loading required package: multcomp
#Loading required package: mvtnorm
#
#Attaching package: 'HH'
#
#
#	The following object(s) are masked from package:alr3 :
#
#	 residual.plots
#
#
#	The following object(s) are masked from package:faraway :
#
#	 vif,
#	 vif.default,
#	 vif.lm
#
out <- tukey.nonadd.test(ov.lm)
#########
# RUN 2 OK
# RUN 3
#########
require(crossdes)
#Loading required package: crossdes
#Loading required package: AlgDesign
#Loading required package: gtools
#
#Attaching package: 'gtools'
#
#
#	The following object(s) are masked from package:faraway :
#
#	 logit
#
#Loading required package: MASS
#Loading required package: grDevices
out <- tukey.nonadd.test(ov.lm)
#Error in parse(text = x) :
#  unexpected symbol in "yield ~ variety + block + preds(yield+variety 
+block+preds.sq)sq"
#########
# RUN 3 FAILS
#########

#########
# CHECKING FOR INTERACTION AMONG PACKAGES
#########
require(MASS)
#Loading required package: MASS
#Loading required package: grDevices
out <- tukey.nonadd.test(ov.lm)
require(gtools)
#Loading required package: gtools
#
#Attaching package: 'gtools'
#
#
#	The following object(s) are masked from package:faraway :
#
#	 logit
#
#
out <- tukey.nonadd.test(ov.lm)
#########
# alr3 pays well with MASS, grDevices, gtools
#########
require(AlgDesign)
#Loading required package: AlgDesign
out <- tukey.nonadd.test(ov.lm)
#Error in parse(text = x) :
#  unexpected symbol in "yield ~ variety + block + preds(yield+variety 
+block+preds.sq)sq"
require(crossdes)
#Loading required package: crossdes
out <- tukey.nonadd.test(ov.lm)
#Error in parse(text = x) :
#  unexpected symbol in "yield ~ variety + block + preds(yield+variety 
+block+preds.sq)sq"
#########
# BAD INTERACTION BETWEEN alr3 and AlgDesign
#########

What now?

_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/
#
On 30/03/2008 8:43 AM, Michael Kubovy wrote:
...
There's not much you can do; this is up to the package authors to fix. 
Some advice for them:

alr3, crossdes, faraway, and HH don't use a namespace.  This makes them 
very vulnerable to this sort of interaction, because they don't get to 
choose where the functions they use come from.  They should add one.

In my opinion, all packages should have namespaces, and I'd like to 
create a default one if the author doesn't.  (The default would be: 
import what is listed in the Depends clause, export everything.)  I 
don't think this will happen for 2.7.0, though package.skeleton might 
start creating one.

Duncan Murdoch
#
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)
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?
On Mar 30, 2008, at 9:43 AM, Duncan Murdoch wrote:

            
_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/
#
On Sun, Mar 30, 2008 at 9:43 AM, Duncan Murdoch <murdoch at stats.uwo.ca> wrote:
Namespaces are such a pain when you don't really need them though.
Its nice just to be able to source a new version of a function and have
it immediately work with the package.  Particularly for packages that
are changing or for which there is substantial code changes in the
devel version this is very convenient and namespaces interefere with that.

Although its possible to have namespace problems as discussed in this
thread I have found that it rarely actually happens and certainly not enough
to impose it.
#
On 30/03/2008 11:31 AM, Gabor Grothendieck wrote:
I would say there is no such case.  Packages all import functions. 
Having the imports change because of unrelated code means that there is 
no way to write reliable code.
That sounds like a problem with the way we handle package code, making 
it too hard to reload a package that has had small changes made.  We 
should make it easy to make a small change to a package and reload the 
modified version.
I suspect it happens more than people realize, and that in some cases 
they just get the wrong answers, without any helpful error message to 
let them know about it.

Duncan Murdoch
#
On 30/03/2008 10:06 AM, Michael Kubovy wrote:
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
#
Hi Duncan,

Yes, I came to realize that ifelse was not the way to go. Here is how  
I dealt with it:

if(sum('AlgDesign' == (.packages())) > 0) detach('package:AlgDesign')

It requires me (and my students) to learn only the (.packages())  
construct (and avoid knowing anything about search(), match() or pos).  
As a former student of mine (who has long since gone into business)  
once said: "I avoid learning experiences like the plague."

Thanks again,
Michael
On Mar 30, 2008, at 1:36 PM, Duncan Murdoch wrote:

            
_____________________________
Professor Michael Kubovy
University of Virginia
Department of Psychology
USPS:     P.O.Box 400400    Charlottesville, VA 22904-4400
Parcels:    Room 102        Gilmer Hall
         McCormick Road    Charlottesville, VA 22903
Office:    B011    +1-434-982-4729
Lab:        B019    +1-434-982-4751
Fax:        +1-434-982-4766
WWW:    http://www.people.virginia.edu/~mk9y/
#
Dear all,

thanks for the advice. The latest version of crossdes (1.0-8) on CRAN  
now has namespaces. Hope this helps.

Regards
Oliver Sailer
 

Duncan Murdoch schrieb: