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, charToUnicode=TRUE,
                    vecToList=FALSE, autoTypecast=TRUE, simplify=TRUE)
    function(option, value) {
        if (missing(option)) return(options)
        if (missing(value)){
            if (option == "intToLong") return(as.logical(.Call( "get_int_long_flag" )))
            if (option == "charToUnicode") return(as.logical(.Call( "get_character_to_py_unicode_flag" )))
            if (option == "vecToList") return(as.logical(.Call( "get_vec_to_list_flag" )))
            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) )
            }
            if (option == "charToUnicode") {
                sol <- .Call( "set_character_to_py_unicode_flag", as.integer(value) )
            }
            options[[option]] <<- value
            return(invisible(sol))
        }
    }
})

Try the PythonInR package in your browser

Any scripts or data that you put into this service are public.

PythonInR documentation built on May 2, 2019, 5:17 p.m.