R/PyOptions.R

#  -----------------------------------------------------------------------------
#  pyOptions
#  =========
#' @title Options for the PythonInR package
#' @description a function for getting and setting options in the PythonInR package.
#' @param option a character giving the option to set or get. 
#' @param value the new value of the option.
#' @details The following options are available:
#' \itemize{
#'   \item \strong{numpyAlias} a character giving the numpy alias, the 
#'         default value is "numpy".
#'   \item \strong{useNumpy} a logical giving if numpy should be used
#'         if getting and setting matrices.
#'   \item \strong{pandasAlias}  a character giving the pandas alias, the 
#'         default value is "pandas".
#'   \item \strong{usePandas} a logical giving if pandas should be used
#'         if getting and setting data.frames.
#'   \item \strong{winPython364} a logical indicating if Python 3 64-bit
#'         under windows is used, this option is set automatically at startup
#'         and shouldn't be changed.
#' }
#' @examples
#' \dontrun{
#' pyOptions()
#' pyExec("import numpy as np")
#' pyOptions("numpyAlias", "np")
#' pyOptions("useNumpy", TRUE)
#' pyExec("import pandas as pd")
#' pyOptions("pandasAlias", "pd")
#' pyOptions("usePandas", TRUE)
#' }
#  -----------------------------------------------------------------------------   
pyOptions <-
local({
    options <- list(numpyAlias="numpy", useNumpy=FALSE, pandasAlias="pandas",
                    usePandas=FALSE, winPython364=FALSE, intToLong=TRUE)
    function(option, value) {
        if (missing(option)) return(options)
        if (missing(value)){
            if (option == "intToLong") return(as.logical(.Call( "get_int_long_flag", PACKAGE="PythonEmbedInR" )))
            return(options[[option]])
        }else{
            sol <- NULL
            if ( class(value) != class(options[[option]]) ){
                stop(sprintf('"%s" has to be "%s"', option, class(options[[option]])))
            }
            if (option == "intToLong") {
                sol <- .Call( "set_int_long_flag", as.integer(value), PACKAGE="PythonEmbedInR" )
            }
            options[[option]] <<- value
            return(invisible(sol))
        }
    }
})
Sage-Bionetworks/PythonEmbedInR documentation built on April 17, 2023, 4:23 p.m.