R namespace problems with 'logging' package - r

I'm writing an R package that uses the logging package from CRAN. In my code, I initialize logging when my package is loaded:
#' #importFrom logging basicConfig
.onAttach <- function(libname, pkgname) {
logging::basicConfig()
}
#' #importFrom logging setLevel
#' #export
setLogLevel <- function(level) {
setLevel(level)
}
And in my DESCRIPTION file I declare the dependency:
Depends:
R (>= 3.0.2),
Rcpp (>= 0.11.2)
Imports:
FNN,
plyr,
geosphere,
logging
However, it looks like R CMD check (which I invoke via "Check Package" in RStudio) doesn't like something about the namespaces:
* checking whether the package can be loaded with stated dependencies ... WARNING
Loading required package: Rcpp
Error : .onAttach failed in attachNamespace() for ‘ABC’, details:
call: UseMethod("updateOptions")
error: no applicable method for ‘updateOptions’ applied to an object of class "Logger"
Error: package or namespace load failed for ‘ABC’
Execution halted
It looks like this package (or one of its dependent packages) has an
unstated dependence on a standard package. All dependencies must be
declared in DESCRIPTION.
See the information on DESCRIPTION files in the chapter ‘Creating R
packages’ of the ‘Writing R Extensions’ manual.
...
* checking for missing documentation entries ... ERROR
Error: package or namespace load failed for ‘ABC’
Call sequence:
2: stop(gettextf("package or namespace load failed for %s", sQuote(package)),
call. = FALSE, domain = NA)
1: library(package, lib.loc = lib.loc, character.only = TRUE, verbose = FALSE)
Execution halted
Exited with status 1.
The updateOptions() method it's complaining about is part of the logging::basicConfig() function I call in my initialization:
> logging::basicConfig
function (level = 20)
{
rootLogger <- getLogger()
updateOptions(rootLogger, level = namedLevel(level))
rootLogger$addHandler("basic.stdout", writeToConsole, level = namedLevel(level))
invisible()
}
<environment: namespace:logging>
d> logging::updateOptions
function (container, ...)
UseMethod("updateOptions")
<environment: namespace:logging>
Thanks for any insight anyone can provide.

Related

Failed to load library tidyr in R (version 4.1)

I need to use the functions spread and gather in the package of tidyr, and I installed this package. I also installed two other packages. However, when I tried to use library(tidyr), it gave me this long error message, basically saying tidyr was not successfully loaded. By the way, my R is a newer version (4.1). Thanks!
install.packages("tidyr")
install.packages("tidyverse")
install.packages("devtools")
Here's the error message:
Error: package or namespace load failed for ‘tidyr’ in namespaceExport(ns, exports):
undefined exports: align, char, colonnade, ctl_new_pillar, ctl_new_pillar_list, dim_desc, expect_known_display, extra_cols, format_glimpse, format_type_sum, get_extent, get_max_extent, glimpse, is_vector_s3, new_ornament, new_pillar, new_pillar_component, new_pillar_shaft, new_pillar_shaft_simple, new_pillar_title, new_pillar_type, num, obj_sum, pillar, pillar_component, pillar_shaft, scale_x_num, scale_y_num, set_char_opts, set_num_opts, size_sum, squeeze, style_bold, style_na, style_neg, style_num, style_subtle, style_subtle_num, tbl_format_body, tbl_format_footer, tbl_format_header, tbl_format_setup, tbl_sum, type_sum
In addition: Warning messages:
1: package ‘tidyr’ was built under R version 4.1.2
2: S3 methods ‘as_tbl.data.frame’, ‘ctl_new_pillar.tbl’, ‘ctl_new_pillar_list.tbl’, ‘extra_cols.pillar_squeezed_colonnade’, ‘format.pillar’, ‘format.pillar_1e’, ‘format.pillar_capital’, ‘format.pillar_char’, ‘format.pillar_char_attr’, ‘format.pillar_colonnade’, ‘format.pillar_continuation_shaft’, ‘format.pillar_num’, ‘format.pillar_num_attr’, ‘format.pillar_ornament’, ‘format.pillar_rif_shaft’, ‘format.pillar_rif_title’, ‘format.pillar_rif_type’, ‘format.pillar_shaft’, ‘format.pillar_shaft_decimal’, ‘format.pillar_shaft_simple’, ‘format.pillar_squeezed_colonnade’, ‘format.pillar_tbl_format_setup’, ‘format.pillar_title’, ‘format.pillar_type’, ‘format.tbl’, ‘format_glimpse.character’, ‘format_glimpse.default’, ‘format_glimpse.factor’, ‘format_glimpse.list’, ‘format_type_sum.AsIs’, ‘format_type_sum.default’, ‘glimpse.data.frame’, ‘glimpse.default’, ‘glimpse.tbl’, ‘obj_print_data.pillar_vctr’, ‘obj_print_footer.pillar_vctr’, ‘obj_print_header.pillar_vctr’, ‘obj_sum.AsIs’, ‘obj_sum.default’, ‘ [... truncated]

How to write unit tests for zzz.R file in R package

I am currently developing an R package and used a zzz.R file to set an option value when the package is loaded and also to return a load message. The content of my zzz.R file is as follows:
.onLoad <- function(libname, pkgname) {
op <- options()
op.mypackage <- list(
mypackage.small_neg_threshold = -1e-6
)
toset <- !(names(op.mypackage) %in% names(op))
if (any(toset)) options(op.mypackage[toset])
}
.onAttach <- function(libname, pkgname) {
packageStartupMessage(
"
Hello World!!!
"
)
}
I wondering how one could write a test case in such that entire zzz.R file would be captured as being covered (after running something like devtools::test_coverage()

Problem in loading library for VIM package

Using R version R.4.0.3. Already installed the VIM package. When I try to load the library function as library(VIM), it fails to load and gives me the following error message:
Error: package or namespace load failed for ‘VIM’ in namespaceExport(ns, exports):
undefined exports: as_factor, fct_anon, fct_c, fct_collapse, fct_count, fct_cross, fct_drop, fct_expand, fct_explicit_na, fct_infreq, fct_inorder, fct_inseq, fct_lump, fct_lump_lowfreq, fct_lump_min, fct_lump_n, fct_lump_prop, fct_match, fct_other, fct_recode, fct_relabel, fct_relevel, fct_reorder, fct_reorder2, fct_rev, fct_shift, fct_shuffle, fct_unify, fct_unique, first2, last2, lvls_expand, lvls_reorder, lvls_revalue, lvls_union
In addition: Warning message:
S3 methods ‘as_factor.character’, ‘as_factor.factor’, ‘as_factor.logical’, ‘as_factor.numeric’ were declared in NAMESPACE but not found

Can't load dplyr package

I am very new to R programming and When I have tried to run "require(dplyr)", I get this error. I can't figure out why
Loading required package: dplyr
Error: package or namespace load failed for ‘dplyr’ in library.dynam(lib, package, package.lib):
shared object ‘tibble.so’ not found
Warning message:
S3 methods ‘$.tbl_df’, ‘[.tbl_df’, ‘[[.tbl_df’, ‘row.names<-.tbl_df’, ‘as.data.frame.tbl_df’, ‘as_data_frame.NULL’, ‘as_data_frame.data.frame’, ‘as_data_frame.default’, ‘as_data_frame.list’, ‘as_data_frame.matrix’, ‘as_data_frame.table’, ‘as_data_frame.tbl_df’, ‘as_tibble.NULL’, ‘as_tibble.data.frame’, ‘as_tibble.default’, ‘as_tibble.list’, ‘as_tibble.matrix’, ‘as_tibble.poly’, ‘as_tibble.table’, ‘as_tibble.tbl_df’, ‘as_tibble.ts’, ‘check_names_before_after.character’, ‘check_names_before_after.default’, ‘check_names_df.character’, ‘check_names_df.default’, ‘check_names_df.logical’, ‘check_names_df.numeric’, ‘format.tbl’, ‘format.tbl_df’, ‘format.trunc_mat’, ‘format_v.character’, ‘format_v.default’, ‘format_v.list’, ‘glimpse.data.frame’, ‘glimpse.default’, ‘glimpse.tbl’, ‘print.tbl’, ‘print.tbl_df’, ‘print.trunc_mat’, ‘quote_n.cha [... truncated]
It is not an elegant solution but have you tried
install.packages('tidyverse')

How to check platform in .onLoad in the R package

I am trying to check if the package is run on Windows during the loading of the package and load some additional files. For some reason this doesn't work (added to my zzz.R):
.onLoad <- function(libname, pkgname){
if(.Platform$OS.type == "windows") {
# do specific task here
}
}
How to make it work, is there a better way to do it?
EDIT to correct a wrong previous answer
After loading, loadNamespace looks for a hook function named .onLoad and calls it. The functions loadNamespace is usually called implicitly when library is used to load a package. However you can be useful at times to call these functions directly. So the following(essentially your code) works for me under windows platform.
.onLoad <- function(libname, pkgname ) {
if(.Platform$OS.type == "windows") print("windows")
else print("others")
}
Then you test it using:
loadNamespace("loadpkg")
[1] "windows"
<environment: namespace:loadpkg>
Note also you can have the same thing using library, but you should unload the namespace before (I guess it checks if the package is already loaded and don't call all hooks):
unloadNamespace("loadpkg")
library("loadpkg")
[1] "windows"
don't use this , it is a wrong but accepted solution
You should initialize function parameters:
.onLoad <- function(libname = find.package(pkg_name), pkgname = pkg_name){
if(.Platform$OS.type == "windows") {
# do specific task here
}
}

Resources