Skip to content

Problem with unexported deriv function and S3 method  #11

Open
@agila5

Description

@agila5

Dear package maintainers, I created this issue since I would like to highlight a small problem within the package (which is discussed in greater detail here) but, first of all, I would like to thank you for your work since I've recently started learning about FDA and this package (with the companion book) is extremely useful.

Reprex of the problem (using the CRAN version of fda) taken from the scripts within this package:

library(fda)
#> Loading required package: splines
#> Loading required package: fds
#> Loading required package: rainbow
#> Loading required package: MASS
#> Loading required package: pcaPP
#> Loading required package: RCurl
#> Loading required package: deSolve
#> 
#> Attaching package: 'fda'
#> The following object is masked from 'package:graphics':
#> 
#>     matplot

# Create an fd object represeting the weather in 4 canadian stations
fig1.11Stns = c('Montreal', 'Edmonton', 'Pr. Rupert', 'Resolute')
fig1.11Temp = CanadianWeather$dailyAv[
  , 
  fig1.11Stns, 
  'Temperature.C'
]

Temp.fourier   = create.fourier.basis(c(0, 365), 13)
fig1.11Temp.fd = Data2fd(day.5, fig1.11Temp, Temp.fourier)
class(fig1.11Temp.fd)
#> [1] "fd"

# Compute the first derivative
deriv(fig1.11Temp.fd, 1) # fails
#> Error in deriv.default(fig1.11Temp.fd, 1): invalid variable names

# I need to explicitly call the S3 method: 
deriv.fd(fig1.11Temp.fd, 1) |> plot()

#> [1] "done"

Created on 2024-08-29 with reprex v2.0.2

Basically, according to the discussion in the SO post, it looks like there is a problem in the registration of the S3 method for the generic deriv since, currently, there is also an (unexported) deriv function defined inside the package,

fda/R/deriv.fd.R

Lines 1 to 2 in 04f5616

deriv <- function(expr, ...) UseMethod('deriv')

and, currently, the S3 method defined here

fda/R/deriv.fd.R

Lines 3 to 4 in 04f5616

deriv.fd <- function(expr, Lfdobj=int2Lfd(1), ...)
{

and registered here

fda/NAMESPACE

Line 205 in 04f5616

S3method(deriv, fd)

is erroneously assigned to the (unexported) fda:::deriv function. Therefore, based on my understanding, when we call deriv on a fd object, R internally calls the S3 generic stats::deriv and that function has no idea about the existence of fda::deriv.fd since that was registered to fda:::deriv. IMO, the easiest approach to solve the problem (and permit the automatic S3 dispatch on fd objects) is to delete the definition of the (generic) deriv function defined in this package. Happy to create a PR if you want.

btw: if you want to test these changes, I made a fork of this repo and commented out the definition of deriv. After that and after reinstalling the package, everything works fine with the S3 dispatch.

Session info
sessioninfo::session_info()
#> ─ Session info ───────────────────────────────────────────────────────────────
#>  setting  value
#>  version  R version 4.3.1 (2023-06-16 ucrt)
#>  os       Windows 11 x64 (build 22631)
#>  system   x86_64, mingw32
#>  ui       RTerm
#>  language (EN)
#>  collate  English_United Kingdom.utf8
#>  ctype    English_United Kingdom.utf8
#>  tz       Europe/Rome
#>  date     2024-08-29
#>  pandoc   3.1.1 @ C:/Program Files/RStudio/resources/app/bin/quarto/bin/tools/ (via rmarkdown)
#> 
#> ─ Packages ───────────────────────────────────────────────────────────────────
#>  package     * version   date (UTC) lib source
#>  bitops        1.0-7     2021-04-24 [1] CRAN (R 4.3.1)
#>  cli           3.6.2     2023-12-11 [1] CRAN (R 4.3.3)
#>  cluster       2.1.4     2022-08-22 [2] CRAN (R 4.3.1)
#>  colorspace    2.1-0     2023-01-23 [1] CRAN (R 4.3.1)
#>  curl          5.2.1     2024-03-01 [1] CRAN (R 4.3.3)
#>  deSolve     * 1.38      2023-09-05 [1] CRAN (R 4.3.1)
#>  digest        0.6.35    2024-03-11 [1] CRAN (R 4.3.3)
#>  evaluate      0.24.0    2024-06-10 [1] CRAN (R 4.3.3)
#>  fastmap       1.2.0     2024-05-15 [1] CRAN (R 4.3.3)
#>  fda         * 6.1.8     2024-03-09 [1] CRAN (R 4.3.1)
#>  fds         * 1.8       2018-10-31 [1] CRAN (R 4.3.1)
#>  fs            1.6.4     2024-04-25 [1] CRAN (R 4.3.3)
#>  glue          1.7.0     2024-01-09 [1] CRAN (R 4.3.2)
#>  hdrcde        3.4       2021-01-18 [1] CRAN (R 4.3.1)
#>  highr         0.11      2024-05-26 [1] CRAN (R 4.3.3)
#>  htmltools     0.5.8.1   2024-04-04 [1] CRAN (R 4.3.3)
#>  KernSmooth    2.23-21   2023-05-03 [2] CRAN (R 4.3.1)
#>  knitr         1.48      2024-07-07 [1] CRAN (R 4.3.3)
#>  ks            1.14.1    2023-08-10 [1] CRAN (R 4.3.1)
#>  lattice       0.21-8    2023-04-05 [2] CRAN (R 4.3.1)
#>  lifecycle     1.0.4     2023-11-07 [1] CRAN (R 4.3.2)
#>  magrittr      2.0.3     2022-03-30 [1] CRAN (R 4.3.1)
#>  MASS        * 7.3-60    2023-05-04 [2] CRAN (R 4.3.1)
#>  Matrix        1.6-1.1   2023-09-18 [1] CRAN (R 4.3.1)
#>  mclust        6.0.0     2022-10-31 [1] CRAN (R 4.3.1)
#>  mvtnorm       1.2-3     2023-08-25 [1] CRAN (R 4.3.1)
#>  pcaPP       * 2.0-3     2022-10-24 [1] CRAN (R 4.3.1)
#>  pracma        2.4.2     2022-09-22 [1] CRAN (R 4.3.1)
#>  purrr         1.0.2     2023-08-10 [1] CRAN (R 4.3.1)
#>  R.cache       0.16.0    2022-07-21 [1] CRAN (R 4.3.1)
#>  R.methodsS3   1.8.2     2022-06-13 [1] CRAN (R 4.3.1)
#>  R.oo          1.25.0    2022-06-12 [1] CRAN (R 4.3.1)
#>  R.utils       2.12.2    2022-11-11 [1] CRAN (R 4.3.1)
#>  rainbow     * 3.7       2022-10-09 [1] CRAN (R 4.3.1)
#>  RCurl       * 1.98-1.12 2023-03-27 [1] CRAN (R 4.3.1)
#>  reprex        2.0.2     2022-08-17 [1] CRAN (R 4.3.1)
#>  rlang         1.1.3     2024-01-10 [1] CRAN (R 4.3.1)
#>  rmarkdown     2.27      2024-05-17 [1] CRAN (R 4.3.3)
#>  rstudioapi    0.16.0    2024-03-24 [1] CRAN (R 4.3.1)
#>  sessioninfo   1.2.2     2021-12-06 [1] CRAN (R 4.3.1)
#>  styler        1.10.2    2023-08-29 [1] CRAN (R 4.3.1)
#>  vctrs         0.6.5     2023-12-01 [1] CRAN (R 4.3.2)
#>  withr         3.0.0     2024-01-16 [1] CRAN (R 4.3.1)
#>  xfun          0.45      2024-06-16 [1] CRAN (R 4.3.3)
#>  xml2          1.3.6     2023-12-04 [1] CRAN (R 4.3.2)
#>  yaml          2.3.9     2024-07-05 [1] CRAN (R 4.3.3)
#> 
#>  [1] C:/Users/user/AppData/Local/R/win-library/4.3
#>  [2] C:/Program Files/R/R-4.3.1/library
#> 
#> ──────────────────────────────────────────────────────────────────────────────

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions