-
-
Notifications
You must be signed in to change notification settings - Fork 45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Cannot use S4
and box
together: name '.cacheOnAssign' not found in 'env'
#284
Comments
Note this error also occurs when trying to run code interactively. It seems that Module: #' @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")
}) box::use(./utils/R/someModule)
|
S4
and box
in executable R
script: name '.cacheOnAssign' not found in 'env'
S4
and box
together: name '.cacheOnAssign' not found in 'env'
This error also occurs when creating new methods in
methods::setGeneric("is.empty", \(x, ...) standardGeneric("is.empty"))
methods::setMethod("is.empty", "data.frame", \(x, ...) {
return(identical(dim(x), c(0, 0)))
}) box::use(./utils/R/someMethod)
Unfortunately after a lot of testing it seems that the whole of the |
Hi, In the order from easiest to hardest: 1.
That’s normal; first off, ‘methods’ isn’t included in ‘base’, and it isn’t by default loaded/attached in R except in interactive sessions. So this isn’t specific to ‘box’ (you’d have the same error in any script run non-interactively). But in addition, ‘box’ modules don’t attach any packages (except ‘base’), even in interactive sessions — see this FAQ. Your fix works but the “preferred” solution is to use box::use(methods)
#' @export
someClass <- methods$setClass(
… 2.
This can be fixed by adding 3.Unfortunately S4 does not, and (I believe) fundamentally cannot ever work with ‘box’. The reason for that is that S4 explicitly only supports defining classes and methods in packages or the R global environment, and nothing else. See also #95. In particular, S4 internally tries to load the code that defines an S4 class as a package (i.e. via Maybe (!) it is possible to work around this limitation and cobble together some half-assed support for S4 in ‘box’ but unfortunately it isn’t even obvious whether that’s possible at all, and the documentation of S4 is notoriously poor, as noted in Advanced R:
Worse, what little S4 documentation exists is riddled with errors, both in the prose (which makes some sentences unintelligible) and in the actual example code! My general recommendation (regardless of ‘box’!), echoing Advanced R, is to avoid S4 as much as possible. I’d go even further than Advanced R: don’t use S4 at all. Use S3 or R6, as appropriate, or build your own object system where neither of these works well. After all, R allows doing this, and it’s e.g. what ‘ggplot2’ did. S4 has many good ideas but its implementation is rather problematic. |
Thank you very much for all that @klmr I really appreciate it. It's very unfortunate as I really am a fan of As I understood getting this really fixed would require making changes to how |
Error description
My set up and minimal example
I have this set up:
someMinimalScript.R
someMinimalModule.R
Contents of
someMinimalScript.R
:This is importing a module who's contents are
someMinimalModule.R
: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
: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:
First question, why am I getting this above error? It makes no sense, the
methods
package is part ofR
or it's included inbase
. I solved this by appendingmethods::
to my function calls:But now I get this error; I would appreciate any insight anyone could extend to get this resolved:
R version
‘box’ version
‘1.1.2’
The text was updated successfully, but these errors were encountered: