Skip to content

.onAttach fails with '$ operator is invalid for atomic vectors' on any R version mismatch #25

@jsvaishali

Description

@jsvaishali

library(arm) fails to load with the following error:

Warning in packageDescription("arm", lib.loc = mylib): no package 'arm' was found
Error: package or namespace load failed for 'arm':
 .onAttach failed in attachNamespace() for 'arm', details:
   call: packageDescription("arm", lib.loc = mylib)$Version
   error: $ operator is invalid for atomic vectors

The .onAttach function uses a variable mylib which is hardcoded at build time. When the package is loaded on a machine running a different R version than the one it was built on, packageDescription("arm", lib.loc = mylib) cannot find the package and returns NA instead of a list. Calling $Version on NA then crashes with the above error.

Environment:

  • R version: 4.5.1
  • arm version: 1.15-3 (built under R 4.5.3)
  • OS: Windows Server 2022 x64

Suggested Fix:
Add a null/NA check in .onAttach before calling $Version:

desc <- packageDescription("arm", lib.loc = mylib)
if (!is.list(desc)) desc <- packageDescription("arm")
if (is.list(desc)) packageStartupMessage(paste("arm (Version", desc$Version, "built:", desc$Date))

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