klmr / klmr/box

Cannot use `S4` and `box` together: `name '.cacheOnAssign' not found in 'env'`

Open
#284 4 comments 0 reactions 0 assignees View on GitHub
⚠️ bug
Dominant language
R
Stars
979
Forks
49
PR merge metrics
No merged PRs in 30d

Description

### Error description

# My set up and minimal example

I have this set up:

- SomeProject/
- bin/
- `someMinimalScript.R`
- utils/
- R/
- `someMinimalModule.R`

Contents of `someMinimalScript.R`:

```r
#!/usr/bin/env Rscript

box::use(./utils/R/someMinimalModule[ someClass ])
```

This is importing a module who's contents are `someMinimalModule.R`:

```r
#' @export
someClass <- setClass(
"someClass",
slots = list(
example_slot = "character"
),
prototype = list(
example_slot = character()
)
)

#' @export
setMethod("show", "someClass", \(object) {
cat("\n")
cat("Example slot:", object@example_slot, "\n")
cat("\n")
})
```

This is a set of tools which is designed to be used from the system command line (CLI tools). This is achieved by exporting the path to the tools in a `.bashrc`:

```bash
export PATH="/Users/someUser/someProject/bin:$PATH"
```

We need to also do this in the project: `chmod 755 ./bin/*`.

# My errors

Once all of this is set up, I am getting this error when calling this tool from the command line:

```bash
someMinimalScript.R
```

```
Error in box::use(./utils/R/someMinimalModule[someClass]) :
could not find function "setClass"
(inside “setClass("someClass", slots = list(example_slot = "character"), ”
“ prototype = list(example_slot = character()))”)
Calls: ... tryCatchList -> tryCatchOne -> -> rethrow -> throw
Execution halted
```

First question, why am I getting this above error? It makes no sense, the `methods` package is part of `R` or it's included in `base`. I solved this by appending `methods::` to my function calls:

```r
#' @export
someClass <- methods::setClass(
"someClass",
slots = list(
example_slot = "character"
),
prototype = list(
example_slot = character()
)
)

#' @export
methods::setMethod("show", "someClass", \(object) {
cat("\n")
cat("Example slot:", object@example_slot, "\n")
cat("\n")
})
```

But now I get this error; I would appreciate any insight anyone could extend to get this resolved:

```bash
someMinimalScript.R
```

```
Error in box::use(./utils/R/someMinimalModule[someClass]) :
name '.cacheOnAssign' not found in 'env'
(inside “env$.cacheOnAssign”)
Calls: ... tryCatchList -> tryCatchOne -> -> rethrow -> throw
Execution halted
```

### R version

```plain text
sessionInfo()
R version 4.2.0 (2022-04-22)
Platform: aarch64-apple-darwin21.3.0 (64-bit)
Running under: macOS Monterey 12.2.1

Matrix products: default
BLAS: /opt/homebrew/Cellar/openblas/0.3.20/lib/libopenblasp-r0.3.20.dylib
LAPACK: /opt/homebrew/Cellar/r/4.2.0/lib/R/lib/libRlapack.dylib

locale:
[1] en_GB.UTF-8/en_GB.UTF-8/en_GB.UTF-8/C/en_GB.UTF-8/en_GB.UTF-8

attached base packages:
[1] stats graphics grDevices utils datasets methods base

loaded via a namespace (and not attached):
[1] compiler_4.2.0 data.table_1.14.2
```

### ‘box’ version

‘1.1.2’

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure with bin/someMinimalScript.R importing utils/R/someMinimalModule.R, using the reported R 4.2.0 and box 1.1.2 versions. Start by tracing box::use handling of methods::setClass and methods::setMethod; done means the minimal CLI example loads without the '.cacheOnAssign' error.

Written by the indexing model from the issue text.

Assessment

Tech stack
r
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.