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

does not work if dialr is not loaded #16

Closed
renejuan opened this issue Jan 12, 2021 · 2 comments
Closed

does not work if dialr is not loaded #16

renejuan opened this issue Jan 12, 2021 · 2 comments
Assignees
Labels
bug Something isn't working
Milestone

Comments

@renejuan
Copy link

renejuan commented Jan 12, 2021

I am not sure if this is due to a namespace issue but I noticed that dialr have to be attached before running the phone function.

x <- "888-8989"
myfunc <- function(x) dialr::phone(x, region = "US")

# This does not work
myfunc(x)
# Error in .jcheck() : No running JVM detected. Maybe .jinit() would help.

# This neither
dialr::phone(x, "US")

# It seems by loading the library before performing any operations the problem
# is resolved.
library(dialr)
dialr::phone(x, "US")
myfunc(x)
@gorcha
Copy link
Collaborator

gorcha commented Jan 13, 2021

Hi!

This is because rJava is only initialised when the package is attached with library(), but not just when the namespace is loaded (i.e. with the :: reference).

See .onAttach() vs .onLoad() here:

dialr/R/zzz.R

Lines 2 to 35 in 398f2c6

.onLoad <- function(libname, pkgname) {
op <- options()
op.dialr <- list(
dialr.name = "DIALR",
dialr.home = "AU",
dialr.format = "E164",
dialr.locale = "en",
dialr.show_progress = TRUE
)
toset <- !(names(op.dialr) %in% names(op))
if (any(toset)) options(op.dialr[toset])
register_s3_method("pillar", "pillar_shaft", "phone")
register_s3_method("pillar", "type_sum", "phone")
register_s3_method("pillar", "is_vector_s3", "phone")
register_s3_method("pillar", "obj_sum", "phone")
invisible()
}
#' @import rJava
.onAttach <- function(libname, pkgname) {
rJava::.jpackage("dialrjars")
rJava::.jpackage(pkgname, lib.loc = libname) # needed to load RInterface.java
# what's your java version? Need > 1.5.0.
jversion <- .jcall('java.lang.System','S','getProperty','java.version')
if (jversion < "1.6.0")
stop(paste("Your java version is ", jversion,
". Need 1.6.0 or higher.", sep = ""))
# initialise PhoneNumberUtil singleton
invisible(.get_phoneNumberUtil())
}

When I originally set up dialr I took this approach based on a couple of other Java wrapper packages, but I'm not sure that it has to be away. I'll try moving everything to .onLoad() instead and see if it causes any issues, which should give the behaviour you expect.

@gorcha gorcha self-assigned this Jan 13, 2021
@gorcha gorcha added the bug Something isn't working label Jan 13, 2021
@gorcha gorcha closed this as completed in d6aceda Jan 13, 2021
@gorcha
Copy link
Collaborator

gorcha commented Jan 13, 2021

Hi @renejuan, I've made this change and it appears to be working fine.
Thanks for the feedback!

@gorcha gorcha added this to the dialr 0.3.3/0.4.0 milestone Feb 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants