Search code examples
rforeachnamespaces

Namespace error while doing R CMD check --as-cran with "foreach" package functions


So I'm trying to run a CMD check for a package I want to upload to CRAN but when running it on my Windows 11 cmd I get the following error:

* using log directory 'C:/Users/Zick/Documents/YEAB/YEAB-master/YEAB.Rcheck'
* using R version 4.3.3 (2024-02-29 ucrt)
* using platform: x86_64-w64-mingw32 (64-bit)
* R was compiled by
    gcc.exe (GCC) 12.3.0
    GNU Fortran (GCC) 12.3.0
* running under: Windows 11 x64 (build 22631)
* using session charset: UTF-8
* using option '--as-cran'
* checking for file 'YEAB/DESCRIPTION' ... OK
* this is package 'YEAB' version '0.1.0'
* package encoding: UTF-8
* checking CRAN incoming feasibility ... WARNING
Maintainer: 'Emmanuel Alcala <[email protected]>'

New submission

Non-FOSS package license (CC-BY-4.0 + file LICENSE)

Unknown, possibly misspelled, fields in DESCRIPTION:
  'Extdata'

Strong dependencies not in mainstream repositories:
  rethinking

The Date field is over a month old.
* checking package namespace information ... OK
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking if there is a namespace ... OK
* checking for executable files ... OK
* checking for hidden files and directories ... OK
* checking for portable file names ... OK
* checking serialization versions ... OK
* checking whether package 'YEAB' can be installed ... ERROR
Installation failed.
See 'C:/Users/Zick/Documents/YEAB/YEAB-master/YEAB.Rcheck/00install.out' for details.
* DONE

Status: 1 ERROR, 1 WARNING
See
  'C:/Users/Zick/Documents/YEAB/YEAB-master/YEAB.Rcheck/00check.log'
for details.

And when I open the "00install.out" file I get this error:

* installing *source* package 'YEAB' ...
** Using the staged installation
** R
** data
** inst
** byte-compile and prepare package for lazy loading
Error: 'library' is not an exported object from 'namespace:foreach'
Error: Unable to load R code in package 'YEAB'
Execution halted
ERROR: lazy loading failed for package 'YEAB'
* removing 'C:/Users/Zick/Documents/YEAB/YEAB-master/YEAB.Rcheck/YEAB'

The problem is that I'm not importing any function named "library" from the "foreach" namespace. What is more, if I'm not wrong, the "library" function comes from the "base" package, so I don't understand why I'm getting this error. My NAMESPACE file looks like this:

export(KL_div)
export(ab_range_normalization)
export(addalpha)
export(balci2010)
export(berm)
export(biexponential)
export(box_dot_plot)
export(bp_km)
export(bp_opt)
export(ceiling_multiple)
export(curv_index_fry)
export(curv_index_int)
export(den_histogram)
export(entropy_kde2d)
export(eq_hyp)
export(event_extractor)
export(exhaustive_lhl)
export(exhaustive_sbp)
export(exp_fit)
export(f_table)
export(fleshler_hoffman)
export(fwhm)
export(gaussian_fit)
export(gell_like)
export(get_bins)
export(hist_over)
export(hyperbolic_fit)
export(ind_trials_opt)
export(mut_info_discret)
export(mut_info_knn)
export(n_between_intervals)
export(objective_bp)
export(read_med)
export(sample_from_density)
export(trapezoid_auc)
export(unity_normalization)
export(val_in_interval)
importFrom(foreach,"%dopar%")
importFrom(foreach,foreach)
importFrom(MASS,kde2d)
importFrom(Polychrome,createPalette)
importFrom(cluster, clusGap)
importFrom(dplyr,between)
importFrom(dplyr,lag)
importFrom(infotheo,discretize)
importFrom(infotheo,mutinformation)
importFrom(magrittr,"%>%")
importFrom(minpack.lm,nls.lm)
importFrom(rmi,knn_mi)
importFrom(scales,show_col)
importFrom(sfsmisc,integrate.xy)
importFrom(zoo,rollapply)
importFrom(ggplot2, ggplot, aes, geom_point)
importFrom(grid, unit)
importFrom(gridExtra, grid.arrange)
importFrom(rethinking, HPDI)
importFrom(stats, median, optim, coef, fitted, approx, integrate, quantile)
importFrom("grDevices", "col2rgb", "grey", "rgb")
importFrom("graphics", "abline", "arrows", "axis", "box", "boxplot",
             "grid", "hist", "layout", "lines", "mtext", "par",
             "polygon", "stripchart", "text")
importFrom("stats", "approx", "bw.SJ", "coef", "cor", "density",
             "fitted", "integrate", "lm", "loess", "median", "na.omit",
             "nls", "nls.control", "optim", "pnorm", "quantile",
             "rbinom", "runif")
importFrom("utils", "read.table", "stack", "tail", "write.csv")
importFrom(grid, unit, gpar, grid.polygon, grid.text, grid.layout, grid.newpage, pushViewport, viewport, grid.rect, grid.points, grid.xaxis, grid.yaxis, grid.segments)
importFrom(minpack.lm, "nls.lm.control")
importFrom(utils, "stack", "write.csv")
importFrom(ggplot2, theme_gray, element_line, element_blank, element_text)
importFrom(cluster, pam)
importFrom(dplyr, group_by, summarise)

And my DESCRIPTION file looks like this:

Package: YEAB
Title: Package to Analyze Data from Analysis of Behavior Experiments
Version: 0.1.0
Authors@R: c(
    person("Emmanuel", "Alcala", , "[email protected]", role = c("aut", "cre")),
    person("Rodrigo", "Sosa", , "[email protected]", role = "aut")
  )
Description: This is a collection of functions aimed at analyzing data from
    behavioral experiments from MED output and others. It also has
    functions to fit exponential or hyperbolic models from delay
    discounting tasks, exponential mixtures to IRTs, Gaussian plus ramp
    model for peak procedures data, etc.
License: CC0
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports: 
    cluster,
    doParallel,
    rethinking,
    dplyr,
    foreach,
    ggplot2,
    grid,
    gridExtra,
    infotheo,
    ks,
    magrittr,
    minpack.lm,
    Polychrome,
    rmi,
    scales,
    sfsmisc,
    VGAM,
    MASS,
    zoo
License: CC-BY-4.0 + file LICENSE
Extdata: inst/extdata/fi60_raw.csv, inst/extdata/fi60_raw
Author: 'Emmanuel Alcala [aut, cre], Rodrigo Sosa [aut]'
Maintainer: Emmanuel Alcala <[email protected]>
Date: 2023-09-27

I've looked all over the internet for a solution and tried to update the "foreach" package to it's latest version but to no avail. What is more strange is that if I delete these lines:


importFrom(foreach,"%dopar%")
importFrom(foreach,foreach)

from the NAMESPACE file I still get the same 'library' is not an exported object from 'namespace:foreach' error.

Have somebody else found this problem?

I'm quite new in R package development so I'm sorry if I turn out to be making a rookie mistake here.


Solution

  • The error tells you that {foreach} package has no function called library. That is true. The package::function syntax in R means call function from package.

    You shouldn't load packages by using library in a package. Instead, as you already did, use import or importFrom for a specific function.

    I'm pretty certain that you can remove the line that contains foreach::library(foreach) from your package and that should fix the problem without any consequences.