Package reticulate. November 24, 2017

Similar documents
Package reticulate. August 5, 2018

Package tensorflow. January 17, 2018

Package nmslibr. April 14, 2018

Package cloudml. June 12, 2018

Package RcppParallel

Package htmlwidgets. July 10, 2017

Package knitrprogressbar

Package redux. May 31, 2018

Package tfruns. February 18, 2018

Package cattonum. R topics documented: May 2, Type Package Version Title Encode Categorical Features

Package htmlwidgets. February 25, 2016

Package kdtools. April 26, 2018

Package readxl. April 18, 2017

Package fastdummies. January 8, 2018

Package jdx. R topics documented: January 9, Type Package Title 'Java' Data Exchange for 'R' and 'rjava'

Package radix. September 17, 2018

Package fst. December 18, 2017

Package geojsonsf. R topics documented: January 11, Type Package Title GeoJSON to Simple Feature Converter Version 1.3.

Package validara. October 19, 2017

Package ssh. June 4, 2018

Package fst. June 7, 2018

Package narray. January 28, 2018

Package utf8. May 24, 2018

Package gtrendsr. October 19, 2017

Package tidyselect. October 11, 2018

Package pkgbuild. October 16, 2018

Package ECctmc. May 1, 2018

Package gtrendsr. August 4, 2018

Package clipr. June 23, 2018

Package githubinstall

Package semver. January 6, 2017

Package librarian. R topics documented:

Package triebeard. August 29, 2016

Package bigreadr. R topics documented: August 13, Version Date Title Read Large Text Files

Package fastrtext. December 10, 2017

Package datasets.load

Package tiler. June 9, 2018

Package tibble. August 22, 2017

Package embed. November 19, 2018

Package pdfsearch. July 10, 2018

Package promises. April 13, 2018

Package readobj. November 2, 2017

Package robotstxt. November 12, 2017

Package calpassapi. August 25, 2018

Package docxtools. July 6, 2018

Package bigqueryr. October 23, 2017

Package catchr. R topics documented: December 30, Type Package. Title Catch, Collect, and Raise Conditions. Version 0.1.0

Package canvasxpress

Package oec. R topics documented: May 11, Type Package

Package tabulizer. June 7, 2018

Package messaging. May 27, 2018

Package testit. R topics documented: June 14, 2018

Package SEMrushR. November 3, 2018

Package ezknitr. September 16, 2016

Package opencage. January 16, 2018

Package AhoCorasickTrie

Package kirby21.base

Package rprojroot. January 3, Title Finding Files in Project Subdirectories Version 1.3-2

Package data.world. April 5, 2018

Package internetarchive

Package rtext. January 23, 2019

Package IRkernel. January 7, 2019

Package jstree. October 24, 2017

Package lumberjack. R topics documented: July 20, 2018

Package revealjs. R topics documented: March 13, Type Package

Package spark. July 21, 2017

Package rzeit2. January 7, 2019

Package styler. December 11, Title Non-Invasive Pretty Printing of R Code Version 1.0.0

Package nngeo. September 29, 2018

Package available. November 17, 2017

Package profvis. R topics documented:

Package patentsview. July 12, 2017

Cross-platform daemonization tools.

Package rcmdcheck. R topics documented: November 10, 2018

Package RODBCext. July 31, 2017

Package geogrid. August 19, 2018

Package cli. November 5, 2017

Package hyphenatr. August 29, 2016

Package ggimage. R topics documented: November 1, Title Use Image in 'ggplot2' Version 0.0.7

Package callr. August 29, 2016

Package sigmanet. April 23, 2018

Package desc. May 1, 2018

Package BiocManager. November 13, 2018

Package gifti. February 1, 2018

Package Numero. November 24, 2018

Package ompr. November 18, 2017

Package SASmarkdown. R topics documented: November 30, Version Date Title 'SAS' Markdown

Package msgtools. March 2, 2017

Package pkgload. July 8, 2018

Package condusco. November 8, 2017

Package recosystem. September 2, 2017

Package httpcache. October 17, 2017

Package shinytest. May 7, 2018

Package liftr. R topics documented: May 14, Type Package

Package goodpractice

Package crossrun. October 8, 2018

Package raker. October 10, 2017

Package gcforest. October 19, 2018

Package routr. October 26, 2017

Package sgmcmc. September 26, Type Package

Transcription:

Type Package Title R Interface to Python Version 1.3.1 Package reticulate November 24, 2017 R interface to Python modules, classes, and functions. When calling into Python R data types are automatically converted to their equivalent Python types. When values are returned from Python to R they are converted back to R types. Compatible with all versions of Python >= 2.7. License Apache License 2.0 URL https://github.com/rstudio/reticulate BugReports https://github.com/rstudio/reticulate/issues SystemRequirements Python (>= 2.7.0) Encoding UTF-8 LazyData true Depends R (>= 3.0) Imports utils, graphics, jsonlite, Rcpp (>= 0.12.7) Suggests testthat, knitr LinkingTo Rcpp RoxygenNote 6.0.1 VignetteBuilder knitr NeedsCompilation yes Author JJ Allaire [aut, cre], Kevin Ushey [ctb], RStudio [cph, fnd], Yuan Tang [aut, cph], Dirk Eddelbuettel [ctb, cph], Bryan Lewis [ctb, cph], Marcus Geelnard [aut, cph] (TinyThread library, http://tinythreadpp.bitsnbites.eu/) Maintainer JJ Allaire <jj@rstudio.com> Repository CRAN Date/Publication 2017-11-24 18:12:29 UTC 1

2 R topics documented: R topics documented: array_reshape........................................ 3 conda-tools......................................... 4 dict............................................. 5 eng_python......................................... 5 import............................................ 6 iterate............................................ 7 np_array........................................... 8 py.............................................. 8 py_available......................................... 9 py_capture_output..................................... 9 py_config.......................................... 10 py_discover_config..................................... 10 py_function_wrapper.................................... 11 py_get_attr......................................... 11 py_has_attr......................................... 12 py_help........................................... 12 py_id............................................ 13 py_is_null_xptr....................................... 13 py_iterator.......................................... 14 py_last_error........................................ 15 py_len............................................ 16 py_list_attributes...................................... 16 py_module_available.................................... 17 py_run............................................ 17 py_save_object....................................... 18 py_set_attr.......................................... 18 py_set_seed......................................... 19 py_str............................................ 19 py_suppress_warnings................................... 20 py_to_r_wrapper...................................... 20 py_unicode......................................... 21 r-py-conversion....................................... 21 reticulate.......................................... 22 tuple............................................. 22 use_python......................................... 23 with.python.builtin.object.................................. 23 Index 25

array_reshape 3 array_reshape Reshape an Array Reshape (reindex) a multi-dimensional array, using row-major (C-style) reshaping semantics by default. array_reshape(x, dim, order = c("c", "F")) x dim order An array The new dimensions to be set on the array. The order in which elements of x should be read during the rearrangement. "C" means elements should be read in row-major order, with the last index changing fastest; "F" means elements should be read in column-major order, with the first index changing fastest. Details This function differs from e.g. dim(x) <- dim in a very important way: by default, array_reshape() will fill the new dimensions in row-major (C-style) ordering, while dim<-() will fill new dimensions in column-major (Fortran-style) ordering. This is done to be consistent with libraries like NumPy, Keras, and TensorFlow, which default to this sort of ordering when reshaping arrays. See the examples for why this difference may be important. Examples ## Not run: # let's construct a 2x2 array from a vector of 4 elements x <- 1:4 # rearrange will fill the array row-wise array_reshape(x, c(2, 2)) # [,1] [,2] # [1,] 1 2 # [2,] 3 4 # setting the dimensions 'fills' the array col-wise dim(x) <- c(2, 2) x # [,1] [,2] # [1,] 1 3 # [2,] 2 4 ## End(Not run)

4 conda-tools conda-tools Interface to conda utility commands Interface to conda utility commands conda_list(conda = "auto") conda_create(envname, packages = "python", conda = "auto") conda_remove(envname, packages = NULL, conda = "auto") conda_install(envname, packages, pip = FALSE, pip_ignore_installed = TRUE, conda = "auto") conda_binary(conda = "auto") conda_version(conda = "auto") conda envname packages pip Path to conda executable (or "auto" to find conda using the PATH and other conventional install locations). Name of conda environment Character vector with package names to install. TRUE to use pip (defaults to FALSE) pip_ignore_installed Ignore installed versions when using pip. This is TRUE by default so that specific package versions can be installed even if they are downgrades. The FALSE option is useful for situations where you don t want a pip install to attempt an overwrite of a conda binary package (e.g. SciPy on Windows which is very difficult to install via pip due to compilation requirements). conda_list() returns a data frame with the names and paths to the respective python binaries of available environments. conda_create() returns the Path to the python binary of the created environment. conda_binary() returns the location of the main conda binary or NULL if none can be found.

dict 5 dict Create Python dictionary Create a Python dictionary object, including a dictionary whose keys are other Python objects rather than character vectors. dict(..., convert = FALSE) Note... Name/value pairs for dictionary (or a single named list to be converted to a dictionary). convert A Python dictionary TRUE to automatically convert Python objects to their R equivalent. If you pass FALSE you can do manual conversion using the py_to_r() function. The returned dictionary will not automatically convert it s elements from Python to R. You can do manual converstion with the py_to_r() function or pass convert = TRUE to request automatic conversion. eng_python A reticulate Engine for Knitr This provides a reticulate engine for knitr, suitable for usage when attempting to render Python chunks. Using this engine allows for shared state between Python chunks in a document that is, variables defined by one Python chunk can be used by later Python chunks. eng_python(options) options Chunk options, as provided by knitr during chunk execution.

6 import Details The engine can be activated by setting (for example) knitr::knit_engines$set(python = reticulate::eng_python) Typically, this will be set within a document s setup chunk, or by the environment requesting that Python chunks be processed by this engine. import Import a Python module Import the specified Python module for calling from R. import(module, as = NULL, convert = TRUE, delay_load = FALSE) import_main(convert = TRUE) import_builtins(convert = TRUE) import_from_path(module, path, convert = TRUE, delay_load = FALSE) module as convert delay_load path Module name Alias for module name (affects names of R classes) TRUE to automatically convert Python objects to their R equivalent. If you pass FALSE you can do manual conversion using the py_to_r() function. TRUE to delay loading the module until it is first used. FALSE to load the module immediately. If a function is provided then it will be called once the module is loaded. If a list containing on_load() and on_error(e) elements is provided then on_load() will be called on successful load and on_error(e) if an error occurs. Path to import from Details The import_from_path function imports a Python module from an arbitrary filesystem path (the directory of the specified python script is automatically added to the sys.path). A Python module

iterate 7 Examples ## Not run: main <- import_main() sys <- import("sys") ## End(Not run) iterate Traverse a Python iterator or generator Traverse a Python iterator or generator iterate(it, f = base::identity, simplify = TRUE) iter_next(it, completed = NULL) it f simplify completed Python iterator or generator Function to apply to each item. By default applies the identity function which just reflects back the value of the item. Should the result be simplified to a vector if possible? Sentinel value to return from iter_next() if the iteration completes (defaults to NULL but can be any R value you specify). Details Simplification is only attempted all elements are length 1 vectors of type "character", "complex", "double", "integer", or "logical". For iterate(), A list or vector containing the results of calling f on each item in x (invisibly); For iter_next(), the next value in the iteration (or the sentinel completed value if the iteration is complete).

8 py np_array NumPy array Create NumPy arrays and convert the data type and in-memory ordering of existing NumPy arrays. np_array(data, dtype = NULL, order = "C") data dtype order Vector or existing NumPy array providing data for the array Numpy data type (e.g. "float32", "float64", etc.) Memory ordering for array. "C" means C order, "F" means Fortran order. A NumPy array object. py Interact with the Python Main Module The py object provides a means for interacting with the Python main session directly from R. py Format An R object acting as an interface to the Python main module.

py_available 9 py_available Check if Python is available on this system Check if Python is available on this system py_available(initialize = FALSE) py_numpy_available(initialize = FALSE) initialize TRUE to attempt to initialize Python bindings if they aren t yet available (defaults to FALSE). Note Logical indicating whether Python is initialized. The py_numpy_available function is a superset of the py_available function (it calls py_available first before checking for NumPy). py_capture_output Capture and return Python output Capture and return Python output py_capture_output(expr, type = c("stdout", "stderr")) expr type Expression to capture stdout for Streams to capture (defaults to both stdout and stderr) Character vector with output

10 py_discover_config py_config Python configuration Information on Python and Numpy versions detected py_config() Python configuration object; Logical indicating whether Python bindings are available py_discover_config Discover the version of Python to use with reticulate. This function enables callers to check which versions of Python will be discovered on a system as well as which one will be chosen for use with reticulate. py_discover_config(required_module = NULL, use_environment = NULL) required_module A optional module name that must be available in order for a version of Python to be used. use_environment An optional virtual/conda environment name to prefer in the search Python configuration object.

py_function_wrapper 11 py_function_wrapper Scaffold R wrappers for Python functions Scaffold R wrappers for Python functions py_function_wrapper(python_function, r_prefix = NULL, r_function = NULL) py_function_docs(python_function) Note python_function Fully qualfied name of Python function or class constructor (e.g. tf$layers$average_pooling1d) r_prefix r_function Prefix to add to generated R function name Name of R function to generate (defaults to name of Python function if not specified) The generated wrapper will often require additional editing (e.g. to convert Python list literals in the docs to R lists, to massage R numeric values to Python integers via as.integer where required, etc.) so is really intended as an starting point for an R wrapper rather than a wrapper that can be used without modification. py_get_attr Get an attribute of a Python object Get an attribute of a Python object py_get_attr(x, name, silent = FALSE) x name silent Python object Attribute name TRUE to return NULL if the attribute doesn t exist (default is FALSE which will raise an error)

12 py_help Attribute of Python object py_has_attr Check if a Python object has an attribute Check whether a Python object x has an attribute name. py_has_attr(x, name) x name A python object. The attribute to be accessed. TRUE if the object has the attribute name, and FALSE otherwise. py_help Documentation for Python Objects Documentation for Python Objects py_help(object) object Object to print documentation for

py_id 13 py_id Unique identifer for Python object Get a globally unique identifer for a Python object. py_id(object) object Python object Unique identifer (as integer) or NULL Note In the current implementation of CPython this is the memory address of the object. py_is_null_xptr Check if a Python object is a null externalptr Check if a Python object is a null externalptr py_is_null_xptr(x) py_validate_xptr(x) x Python object Details When Python objects are serialized within a persisted R environment (e.g..rdata file) they are deserialized into null externalptr objects (since the Python session they were originally connected to no longer exists). This function allows you to safely check whether whether a Python object is a null externalptr. The py_validate function is a convenience function which calls py_is_null_xptr and throws an error in the case that the xptr is NULL.

14 py_iterator Logical indicating whether the object is a null externalptr py_iterator Create a Python iterator from an R function Create a Python iterator from an R function py_iterator(fn, completed = NULL) fn completed R function with no arguments. Special sentinel return value which indicates that iteration is complete (defaults to NULL) Details Python generators are functions that implement the Python iterator protocol. In Python, values are returned using the yield keyword. In R, values are simply returned from the function. In Python, the yield keyword enables successive iterations to use the state of previous iterations. In R, this can be done by returning a function that mutates it s enclosing environment via the <<operator. For example: sequence_generator <-function(start) { value <- start function() { value <<- value + 1 value } } g <- generator(sequence_generator(10)) Python iterator which calls the R function for each iteration.

py_last_error 15 Ending Iteration In Python, returning from a function without calling yield indicates the end of the iteration. In R however, return is used to yield values, so the end of iteration is indicated by a special return value (NULL by default, however this can be changed using the completed parameter). For example: sequence_generator <-function(start) { value <- start function() { value <<- value + 1 if (value < 100) value else NULL } } Threading Some Python APIs use generators to parallelize operations by calling the generator on a background thread and then consuming it s results on the foreground thread. The py_iterator() function creates threadsafe iterators by ensuring that the R function is always called on the main thread (to be compatible with R s single-threaded runtime) even if the generator is run on a background thread. py_last_error Get or clear the last Python error encountered Get or clear the last Python error encountered py_last_error() py_clear_last_error() For py_last_error(), a list with the type, value, and traceback for the last Python error encountered (can be NULL if no error has yet been encountered).

16 py_list_attributes py_len Length of Python object Get the length of a Python object (equivalent to the Python len() built in function). py_len(x) x Python object Length as integer py_list_attributes List all attributes of a Python object List all attributes of a Python object py_list_attributes(x) x Python object Character vector of attributes

py_module_available 17 py_module_available Check if a Python module is available on this system. Check if a Python module is available on this system. py_module_available(module) module Name of module Logical indicating whether module is available py_run Run Python code Execute code within the the main Python module. py_run_string(code, local = FALSE, convert = TRUE) py_run_file(file, local = FALSE, convert = TRUE) py_eval(code, convert = TRUE) code local convert file Code to execute Whether to create objects in a local/private namespace (if FALSE, objects are created within the main module). TRUE to automatically convert Python objects to their R equivalent. If you pass FALSE you can do manual conversion using the py_to_r() function. File to execute For py_eval(), the result of evaluating the expression; For py_run_string() and py_run_file(), the dictionary associated with the code execution.

18 py_set_attr py_save_object Save and load Python objects with pickle Save and load Python objects with pickle py_save_object(object, filename, pickle = "pickle") py_load_object(filename, pickle = "pickle") object filename pickle Object to save File name The implementation of pickle to use (defaults to "pickle" but could e.g. also be "cpickle") py_set_attr Set an attribute of a Python object Set an attribute of a Python object py_set_attr(x, name, value) x name value Python object Attribute name Attribute value

py_set_seed 19 py_set_seed Set Python and NumPy random seeds Set various random seeds required to ensure reproducible results. The provided seed value will establish a new random seed for Python and NumPy, and will also (by default) disable hash randomization. py_set_seed(seed, disable_hash_randomization = TRUE) Details seed A single value, interpreted as an integer disable_hash_randomization Disable hash randomization, which is another common source of variable results. See https://docs.python.org/3.4/using/cmdline.html#envvar-pythonhashseed This function does not set the R random seed, for that you should call set.seed(). py_str An S3 method for getting the string representation of a Python object An S3 method for getting the string representation of a Python object py_str(object,...) object Python object... Unused Details The default implementation will call PyObject_Str on the object. Character vector

20 py_to_r_wrapper py_suppress_warnings Suppress Python warnings for an expression Suppress Python warnings for an expression py_suppress_warnings(expr) expr Expression to suppress warnings for Result of evaluating expression py_to_r_wrapper R wrapper for Python objects S3 method to create a custom R wrapper for a Python object. The default wrapper is either an R environment or an R function (for callable python objects). py_to_r_wrapper(x) x Python object

py_unicode 21 py_unicode Convert to Python Unicode Object Convert to Python Unicode Object py_unicode(str) str Single element character vector to convert Details By default R character vectors are converted to Python strings. In Python 3 these values are unicode objects however in Python 2 they are 8-bit string objects. This function enables you to obtain a Python unicode object from an R character vector when running under Python 2 (under Python 3 a standard Python string object is returend). r-py-conversion Convert between Pyton and R objects Convert between Pyton and R objects py_to_r(x) r_to_py(x, convert = FALSE) x convert Object to convert TRUE to automatically convert Python objects to their R equivalent. If you pass FALSE you can do manual conversion using the py_to_r() function. Converted object

22 tuple reticulate R Interface to Python R interface to Python modules, classes, and functions. When calling into Python R data types are automatically converted to their equivalent Python types. When values are returned from Python to R they are converted back to R types. The reticulate package is compatible with all versions of Python >= 2.7. Integration with NumPy requires NumPy version 1.6 or higher. tuple Create Python tuple Create a Python tuple object tuple(..., convert = FALSE)... s for tuple (or a single list to be converted to a tuple). convert TRUE to automatically convert Python objects to their R equivalent. If you pass FALSE you can do manual conversion using the py_to_r() function. A Python tuple Note The returned tuple will not automatically convert it s elements from Python to R. You can do manual converstion with the py_to_r() function or pass convert = TRUE to request automatic conversion.

use_python 23 use_python Configure which version of Python to use Configure which version of Python to use use_python(python, required = FALSE) use_virtualenv(virtualenv, required = FALSE) use_condaenv(condaenv, conda = "auto", required = FALSE) python required virtualenv condaenv conda Path to Python binary Is this version of Python required? If TRUE then an error occurs if it s not located. Otherwise, the version is taken as a hint only and scanning for other versions will still proceed. Directory of Python virtualenv Name of Conda environment Conda executable. Default is "auto", which checks the PATH as well as other standard locations for Anaconda installations. with.python.builtin.object Evaluate an expression within a context. The with method for objects of type python.builtin.object implements the context manager protocol used by the Python with statement. The passed object must implement the context manager ( enter and exit methods. ## S3 method for class 'python.builtin.object' with(data, expr, as = NULL,...)

24 with.python.builtin.object data expr as... Unused Context to enter and exit Expression to evaluate within the context Name of variable to assign context to for the duration of the expression s evaluation (optional).

Index Topic datasets py, 8 Topic intername conda-tools, 4 array_reshape, 3 conda-tools, 4 conda_binary (conda-tools), 4 conda_create (conda-tools), 4 conda_install (conda-tools), 4 conda_list (conda-tools), 4 conda_remove (conda-tools), 4 conda_version (conda-tools), 4 dict, 5 eng_python, 5 import, 6 import_builtins (import), 6 import_from_path (import), 6 import_main (import), 6 iter_next (iterate), 7 iterate, 7 np_array, 8 py, 8 py_available, 9 py_capture_output, 9 py_clear_last_error (py_last_error), 15 py_config, 10 py_discover_config, 10 py_eval (py_run), 17 py_function_docs (py_function_wrapper), 11 py_function_wrapper, 11 py_get_attr, 11 py_has_attr, 12 py_help, 12 py_id, 13 py_is_null_xptr, 13 py_iterator, 14 py_last_error, 15 py_len, 16 py_list_attributes, 16 py_load_object (py_save_object), 18 py_module_available, 17 py_numpy_available (py_available), 9 py_run, 17 py_run_file (py_run), 17 py_run_string (py_run), 17 py_save_object, 18 py_set_attr, 18 py_set_seed, 19 py_str, 19 py_suppress_warnings, 20 py_to_r (r-py-conversion), 21 py_to_r(), 5, 6, 17, 21, 22 py_to_r_wrapper, 20 py_unicode, 21 py_validate_xptr (py_is_null_xptr), 13 r-py-conversion, 21 r_to_py (r-py-conversion), 21 reticulate, 22 reticulate-package (reticulate), 22 set.seed(), 19 tuple, 22 use_condaenv (use_python), 23 use_python, 23 use_virtualenv (use_python), 23 with.python.builtin.object, 23 25