Skip to content
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

Conflict with conflicted library, remove uses of apropos()? #1141

Open
emstruong opened this issue Oct 16, 2024 · 2 comments
Open

Conflict with conflicted library, remove uses of apropos()? #1141

emstruong opened this issue Oct 16, 2024 · 2 comments

Comments

@emstruong
Copy link

emstruong commented Oct 16, 2024

Hello,

It appears that Rstan's use of the apropos() function is causing conflicts with the conflicted library, so I was advised to open an issue here to see if all uses of apropos() could be removed?

It doesn't seem like changing apropos() is feasible and it's not clear if it would be a good idea or even possible to change the conflicted library to make things work. Also, conflicted seems like a really important package for computational reproducibility and considering R's nature.

Please see the attached discussion for further context. Here

Michael

@jgabry
Copy link
Member

jgabry commented Oct 16, 2024

Hi, thanks for reporting this. I didn't even know RStan called apropos but I guess many years ago it was used here on line 82:

# find possibly identical stanmodels
model_re <- "(^[[:alnum:]]{2,}.*$)|(^[A-E,G-S,U-Z,a-z].*$)|(^[F,T].+)"
if(!is.null(model_name))
if(!grepl(model_re, model_name))
stop("model name must match ", model_re)
S4_objects <- apropos(model_re, mode="S4", ignore.case = FALSE)
if (length(S4_objects) > 0) {
e <- environment()
stanfits <- sapply(mget(S4_objects, envir = e, inherits = TRUE),
FUN = is, class2 = "stanfit")
stanmodels <- sapply(mget(S4_objects, envir = e, inherits = TRUE),
FUN = is, class2 = "stanmodel")
if (any(stanfits)) for (i in names(which(stanfits))) {
obj <- get_stanmodel(get(i, envir = e, inherits = TRUE))
if (identical(obj@model_code[1], stanc_ret$model_code[1])) return(obj)
}
if (any(stanmodels)) for (i in names(which(stanmodels))) {
obj <- get(i, envir = e, inherits = TRUE)
if (identical(obj@model_code[1], stanc_ret$model_code[1])) return(obj)
}
}

But I'm not sure what it should be changed to. Is there a recommended alternative to apropos that wouldn't cause this problem? Looking at the RStan code here, it seems like it's trying to find existing S4 objects that are stanmodel objects identical to the one being created, in which case it returns the existing one instead of creating a new one and compiling again (@bgoodri is that right?).

@emstruong
Copy link
Author

Hi, thanks for reporting this. I didn't even know RStan called apropos but I guess many years ago it was used here on line 82:

# find possibly identical stanmodels
model_re <- "(^[[:alnum:]]{2,}.*$)|(^[A-E,G-S,U-Z,a-z].*$)|(^[F,T].+)"
if(!is.null(model_name))
if(!grepl(model_re, model_name))
stop("model name must match ", model_re)
S4_objects <- apropos(model_re, mode="S4", ignore.case = FALSE)
if (length(S4_objects) > 0) {
e <- environment()
stanfits <- sapply(mget(S4_objects, envir = e, inherits = TRUE),
FUN = is, class2 = "stanfit")
stanmodels <- sapply(mget(S4_objects, envir = e, inherits = TRUE),
FUN = is, class2 = "stanmodel")
if (any(stanfits)) for (i in names(which(stanfits))) {
obj <- get_stanmodel(get(i, envir = e, inherits = TRUE))
if (identical(obj@model_code[1], stanc_ret$model_code[1])) return(obj)
}
if (any(stanmodels)) for (i in names(which(stanmodels))) {
obj <- get(i, envir = e, inherits = TRUE)
if (identical(obj@model_code[1], stanc_ret$model_code[1])) return(obj)
}
}

But I'm not sure what it should be changed to. Is there a recommended alternative to apropos that wouldn't cause this problem? Looking at the RStan code here, it seems like it's trying to find existing S4 objects that are stanmodel objects identical to the one being created, in which case it returns the existing one instead of creating a new one and compiling again (@bgoodri is that right?).

Hi, perhaps something like ls() or objects() might be made to be more appropriate? apropos() calls grep() on ls() internally, anyways. I'm not deep enough into S4 objects and the RStan codebase to know if this would be satisfactory.

The 'real issue' is that because apropos() internally invokes search(), this ends up eventually triggering conflicted when vapply() is applied onto the .conflicted (mis-spelling?) object, which brings us to this issue and the advice to not use apropos in libraries.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants