Skip to content

Package development depending on RBioFormats: How can I reliably set java.parameters when importing functions from RBioFormats into my package? #38

@Gewerd-Strauss

Description

@Gewerd-Strauss

Hello,

I am a bit at a loss.
I am developing a package that requires RBioFormats::read.image() to lead subsections of images. THus, I need to spin up a java VM prior to loading these functions, and given that the images are 4000x6000 pixels large the default heap size fits not suffice.

In normal scripts, you could ensure

G <- 8
options(java.parameters = paste0("-Xmx",G,"g"))

My thoughts were that using .onLoad() I could successfully apply -Xmx8g.

zzz.R:

..onLoad <- function(lib, pkg) {
    curr_memory <- getOption("java.parameters")
    options(dfl.java_parameters = curr_memory)
    GB <- 8
    if (is.null(curr_memory)) {
        options(java.parameters = paste0("-Xmx",GB,"g"))
        packageStartupMessage(paste0("1Set option <java.parameters> to '",getOption("java.parameters"),"'"))
    } else {
        matches <- regexec("(\\d+)(.*)", curr_memory)
        value <- as.integer(regmatches(curr_memory, matches)[[1]][[2]])
        unit <- regmatches(curr_memory, matches)[[1]][[3]]
        if (isFALSE(unit=="g")) { # we loaded less than a gigabyte - so we must stock up
            options(java.parameters = paste0("-Xmx",GB,"g"))
            packageStartupMessage(paste0("2Set option <java.parameters> to '",getOption("java.parameters"),"'"))
        } else {
            if (value<GB) {
                options(java.parameters = paste0("-Xmx",GB,"g"))
                packageStartupMessage(paste0("3Set option <java.parameters> to '",getOption("java.parameters"),"'"))
            } else {
                packageStartupMessage(paste0("4<java.parameters> is already set to '",getOption("java.parameters"),"'"))
            }
        }
    }
    invisible()
}
.onUnLoad <- function(lib, pkg) {
    ## reset Java parameters
    options(java.parameters = getOption("dfl.java_parameters"))
    options(dfl.java_parameters = NULL)
}

The NAMESPACE:

# Generated by roxygen2: do not edit by hand

export(load_image)
export(prep_loading)
importFrom(RBioFormats,coreMetadata)
importFrom(RBioFormats,read.image)
importFrom(RBioFormats,read.metadata)
importFrom(imager,RGBtoHSV)
importFrom(imager,as.cimg)
importFrom(imager,load.image)
importFrom(imager,sRGBtoRGB)
importFrom(stringr,str_c)

But this approach very reliably does nothing. Executing

RBioFormats::checkJavaMemory()
getOption("java.parameters")

after either calling library(my_package) or executing devtools::load_all(".") however still returns:

> RBioFormats::checkJavaMemory()
[1] 455.5
> getOption("java.parameters")
[1] "-Xmx8g

Functions which require RBioFormats access its functions via

[...]
#'
#' @return hsv-formatted pixel.array, unless HSV==false.
#' @export
#' [...]
#' @importFrom RBioFormats coreMetadata
#' @importFrom RBioFormats read.metadata
#' @importFrom RBioFormats read.image
#'
#' @example man/examples/load_image.R
load_image <- function(image_path, subset_only = FALSE, HSV = TRUE, crop_left=0, crop_right=0, crop_top=0, crop_bottom=0) {
[...]

I want my package to ensure that RBioFormats gets loaded with 8 GB.
How can I do so automatically? What am I not understanding here?


Thank you,
Sincerely,
~Gw

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