andrewhooker / PopED

Population Experimental Design (PopED) in R
https://andrewhooker.github.io/PopED/
GNU Lesser General Public License v3.0
33 stars 21 forks source link

Weird failure with dev evaluate #73

Closed hadley closed 1 month ago

hadley commented 2 months ago

When I run create.poped.database() inside evaluate, I get an error:

library(PopED)
sfg <- function(x,a,bpop,b,bocc){
  c(
    V=bpop[1]*exp(b[1]),
    KA=bpop[2]*exp(b[2]),
    CL=bpop[3]*exp(b[3])*bpop[5]^a[3], # add covariate for pediatrics
    Favail=bpop[4],
    isPediatric = a[3],
    DOSE=a[1],
    TAU=a[2]
  )
}

evaluate::evaluate("create.poped.database(
    ff_fun=ff.PK.1.comp.oral.md.CL,
    fg_fun=sfg,
    fError_fun=feps.add.prop,
    bpop=c(V=72.8,KA=0.25,CL=3.75,Favail=0.9,pedCL=0.8),
    notfixed_bpop=c(1,1,1,0,1),
    d=c(V=0.09,KA=0.09,CL=0.25^2),
    sigma=c(0.04,5e-6),
    notfixed_sigma=c(0,0),
    m=2,
    groupsize=20,
    xt=c( 1,8,10,240,245),
    bUseGrouped_xt=1,
    a=list(c(DOSE=20,TAU=24,isPediatric = 0),
           c(DOSE=40, TAU=24,isPediatric = 0))
  )
")
#> <evaluation>
#> Source code: 
#>   create.poped.database(
#>       ff_fun=ff.PK.1.comp.oral.md.CL,
#>       fg_fun=sfg,
#>       fError_fun=feps.add.prop,
#>       bpop=c(V=72.8,KA=0.25,CL=3.75,Favail=0.9,pedCL=0.8),
#>       notfixed_bpop=c(1,1,1,0,1),
#>       d=c(V=0.09,KA=0.09,CL=0.25^2),
#>       sigma=c(0.04,5e-6),
#>       notfixed_sigma=c(0,0),
#>       m=2,
#>       groupsize=20,
#>       xt=c( 1,8,10,240,245),
#>       bUseGrouped_xt=1,
#>       a=list(c(DOSE=20,TAU=24,isPediatric = 0),
#>              c(DOSE=40, TAU=24,isPediatric = 0))
#>     )
#> Condition: 
#>   Error in poped.db$parameters$bpop[, 2, drop = F]:
#>   incorrect number of dimensions

Created on 2024-08-21 with reprex v2.1.0

But if I run this code directly, it works.

This is a problem because knitr uses evaluate to run code, which means that your vignette breaks with dev evaluate. I think this is most like'y because you're looking in some evaluation frame that you shouldn't, but unfortunately I don't know enough about PopED to diagnose the problem (and if there's anything I can do in evaluate to fix it).

andrewhooker commented 2 months ago

Hi Hadley,

When I run the above code, in the released version of both packages (PopED and evaluate), I can't reproduce your error. I don't get an error with any combination of released and latest development versions either. I am using the latest R version on an M3 Mac with the latest OS. What versions of the packages were you using? What environment are you using?

Best regards, Andy

hadley commented 1 month ago

I'm using dev evaluate and CRAN versions of everything else.

andrewhooker commented 1 month ago

Hi

I have successfully reproduced the issue. Running the code line-by-line in an interactive R session does not produce any errors. However, when I knit the code in a vignette/R Markdown document or use reprex, I encounter the error shown below. Although I haven’t pinpointed the exact cause yet, I can now experiment further to understand the problem.

Best regards, Andy

library(PopED)
packageVersion("PopED")
#> [1] '0.6.0'
packageVersion("evaluate")
#> [1] '0.24.0.9000'

sfg <- function(x,a,bpop,b,bocc){
  c(
    V=bpop[1]*exp(b[1]),
    KA=bpop[2]*exp(b[2]),
    CL=bpop[3]*exp(b[3])*bpop[5]^a[3], # add covariate for pediatrics
    Favail=bpop[4],
    isPediatric = a[3],
    DOSE=a[1],
    TAU=a[2]
  )
}

create.poped.database(
  ff_fun=ff.PK.1.comp.oral.md.CL,
  fg_fun=sfg,
  fError_fun=feps.add.prop,
  bpop=c(V=72.8,KA=0.25,CL=3.75,Favail=0.9,pedCL=0.8),
  notfixed_bpop=c(1,1,1,0,1),
  d=c(V=0.09,KA=0.09,CL=0.25^2),
  sigma=c(0.04,5e-6),
  notfixed_sigma=c(0,0),
  m=2,
  groupsize=20,
  xt=c( 1,8,10,240,245),
  bUseGrouped_xt=1,
  a=list(c(DOSE=20,TAU=24,isPediatric = 0),
         c(DOSE=40, TAU=24,isPediatric = 0))
)
#> Error in poped.db$parameters$bpop[, 2, drop = F]: incorrect number of dimensions

evaluate::evaluate("create.poped.database(
    ff_fun=ff.PK.1.comp.oral.md.CL,
    fg_fun=sfg,
    fError_fun=feps.add.prop,
    bpop=c(V=72.8,KA=0.25,CL=3.75,Favail=0.9,pedCL=0.8),
    notfixed_bpop=c(1,1,1,0,1),
    d=c(V=0.09,KA=0.09,CL=0.25^2),
    sigma=c(0.04,5e-6),
    notfixed_sigma=c(0,0),
    m=2,
    groupsize=20,
    xt=c( 1,8,10,240,245),
    bUseGrouped_xt=1,
    a=list(c(DOSE=20,TAU=24,isPediatric = 0),
           c(DOSE=40, TAU=24,isPediatric = 0))
  )
")
#> <evaluation>
#> Source code: 
#>   create.poped.database(
#>       ff_fun=ff.PK.1.comp.oral.md.CL,
#>       fg_fun=sfg,
#>       fError_fun=feps.add.prop,
#>       bpop=c(V=72.8,KA=0.25,CL=3.75,Favail=0.9,pedCL=0.8),
#>       notfixed_bpop=c(1,1,1,0,1),
#>       d=c(V=0.09,KA=0.09,CL=0.25^2),
#>       sigma=c(0.04,5e-6),
#>       notfixed_sigma=c(0,0),
#>       m=2,
#>       groupsize=20,
#>       xt=c( 1,8,10,240,245),
#>       bUseGrouped_xt=1,
#>       a=list(c(DOSE=20,TAU=24,isPediatric = 0),
#>              c(DOSE=40, TAU=24,isPediatric = 0))
#>     )
#> Condition: 
#>   Error in poped.db$parameters$bpop[, 2, drop = F]:
#>   incorrect number of dimensions

Created on 2024-09-06 with reprex v2.1.1

hadley commented 1 month ago

I can no longer reproduce this with the latest dev version of evaluate so 🤞 I've fixed the problem.

andrewhooker commented 1 month ago

Fantastic!

hadley commented 1 month ago

I asked Kurt to run an informal series of checks and it still appears on CRAN 😞 Unfortunately there are only 7 failures out of all ~9000 packages I checked so it seems likely that it's something subtly wrong with your package, not an evaluate bug. I'm planning to submit to CRAN on Sept 16.

andrewhooker commented 1 month ago

Thanks for all your efforts! I'll keep investigating then!

andrewhooker commented 1 month ago

I am using capture.output in my package, which seems to perform differently when in an interactive session and when I knit the code in a vignette/R Markdown document or use reprex. The new version of evaluate seems to change the way capture.output performs in these non-interactive settings. This in turn has broken a set of regular expressions I am using to find specific terms in a function (I'm looking for all instances of bpop[*] for example). A fix to the regular expressions I use in PopED is on the way.

andrewhooker commented 1 month ago

Updated in the latest dev version.