Package SnakeCharmR. October 7, 2017

Similar documents
Package messaging. May 27, 2018

Package validara. October 19, 2017

Package ECctmc. May 1, 2018

Package io. January 15, 2018

Package clipr. June 23, 2018

Package reportr. October 6, 2016

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

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

Package spark. July 21, 2017

Package loggit. April 9, 2018

Package geoops. March 19, 2018

Package crossword.r. January 19, 2018

Package lvec. May 24, 2018

Package strat. November 23, 2016

Package githubinstall

Package pkgbuild. October 16, 2018

Package reticulate. November 24, 2017

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

Package fastdummies. January 8, 2018

Package RcppProgress

Package postgistools

Package tensorflow. January 17, 2018

Package condusco. November 8, 2017

Package aws.transcribe

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

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

Package zip. R topics documented: March 11, Title Cross-Platform 'zip' Compression Version 2.0.1

Package synchronicity

Package promote. November 15, 2017

Package exifr. October 15, 2017

Package datasets.load

Package available. November 17, 2017

Package opencage. January 16, 2018

Package librarian. R topics documented:

Package shinyhelper. June 21, 2018

Package robotstxt. November 12, 2017

Package calpassapi. August 25, 2018

Package goodpractice

Package V8. April 25, 2017

Package sankey. R topics documented: October 22, 2017

Package pwrab. R topics documented: June 6, Type Package Title Power Analysis for AB Testing Version 0.1.0

Package repec. August 31, 2018

Package RODBCext. July 31, 2017

Package testit. R topics documented: June 14, 2018

Package farver. November 20, 2018

Package gridgraphics

Package ramcmc. May 29, 2018

Package sessioninfo. June 21, 2017

Package gtrendsr. October 19, 2017

Package dotcall64. January 11, 2018

Package dbx. July 5, 2018

Package gtrendsr. August 4, 2018

Package geogrid. August 19, 2018

Package Rserve. R topics documented: February 19, Version Title Binary R server

Package rbraries. April 18, 2018

Package fst. June 7, 2018

Package kirby21.base

Package RcppParallel

Package JuniperKernel

Package getopt. February 16, 2018

Package semver. January 6, 2017

Package desc. May 1, 2018

Package ssh. June 4, 2018

Package rmumps. September 19, 2017

Package coga. May 8, 2018

Package rngtools. May 15, 2018

Package SimilaR. June 21, 2018

Package BiocManager. November 13, 2018

Package mdftracks. February 6, 2017

Package fst. December 18, 2017

Package purrrlyr. R topics documented: May 13, Title Tools at the Intersection of 'purrr' and 'dplyr' Version 0.0.2

Package nmslibr. April 14, 2018

Package QCAtools. January 3, 2017

Package ompr. November 18, 2017

Package cloudml. June 12, 2018

Package tidyselect. October 11, 2018

Package ezknitr. September 16, 2016

Package readobj. November 2, 2017

Package triebeard. August 29, 2016

Package snplist. December 11, 2017

Package remotes. December 21, 2017

Package RPresto. July 13, 2017

Package MicroStrategyR

Package rmatio. July 28, 2017

Package bigqueryr. October 23, 2017

Package redux. May 31, 2018

Package RPushbullet. February 17, 2017

Package rtext. January 23, 2019

Package PxWebApiData

Package tibble. August 22, 2017

Package swmmr. April 10, 2018

Package darksky. September 20, 2017

Package wrswor. R topics documented: February 2, Type Package

Package combiter. December 4, 2017

Package htmlwidgets. February 25, 2016

Package TVsMiss. April 5, 2018

Package deductive. June 2, 2017

Package rcmdcheck. R topics documented: November 10, 2018

R topics documented: 2 clone

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

Transcription:

Version 1.0.7 Date 2017-10-05 Title R and Python Integration Package SnakeCharmR October 7, 2017 Author Alexandre Sieira, forked off of rpython by Carlos J. Gil Bellosta Maintainer Alexandre Sieira <alexandre.sieira@gmail.com> Run 'Python' code, make function calls, assign and retrieve variables, etc. from R. A fork from 'rpython' which uses 'jsonlite', 'Rcpp' and has several fixes and improvements. Imports jsonlite (>= 1.3), stringr (>= 1.2.0), Rcpp (>= 0.12.0), utils License GPL-2 SystemRequirements Python (>= 2.7) and Python headers and libraries (See the README.md file) OS_type unix URL https://github.com/asieira/snakecharmr BugReports https://github.com/asieira/snakecharmr/issues Suggests testthat (>= 1.0.0) LinkingTo Rcpp RoxygenNote 6.0.1 NeedsCompilation yes Repository CRAN Date/Publication 2017-10-06 22:36:32 UTC R topics documented: py.assign........................................... 2 py.call............................................ 3 py.exec........................................... 4 py.get............................................ 5 py.load............................................ 6 py.rm............................................ 7 SnakeCharmR........................................ 7 1

2 py.assign Index 8 py.assign Assign values to Python variables from R py.assign assigns values to variables in Python. Objects are serialized as JSON strings on R with jsonlite::tojson, are transferred to Python and are converted back to a Python value using json.loads. py.assign(var.name, value, json.opt.args = getoption("snakecharmr.json.opt.args", list(auto_unbox = TRUE, null = "null"))) var.name value json.opt.args a character string containing a valid Python variable name an R object whose equivalent wants to be assigned to the variable in Python explicit arguments to pass to jsonlite::tojson when serializing the value py.assign("a", 1:4) py.get("a") # [1] 1 2 3 4 py.assign("b", list(one = 1, foo = "bar")) str(py.get("b")) # List of 2 # $ foo: chr "bar" # $ one: int 1 py.exec("import math") py.get("math.pi") # [1] 3.141593 # by default jsonlite::tojson is called on the value to be assigned # with auto_unbox = TRUE, so vectors of length 1 are simplified to # scalar values in Python py.assign("c", "foo bar") py.exec("crepr = repr(c)") py.get("crepr") # [1] "u'foo bar'" # if we explicitly set auto_unbox to FALSE, a vector of length 1 # becomes a Python list of length 1 py.assign("d", "foo bar", json.opt.args = list(auto_unbox = FALSE))

py.call 3 py.exec("drepr = repr(d)") py.get("drepr") # [1] "[u'foo bar']" py.call Calls Python functions and methods from R This function runs a Python function taking as arguments R objects and returning an R object. Some limitations exist as to the nature of the objects that can be passed between R and Python, mainly which data types can be converted to/from JSON in R (using jsonlite) and Python (using the json module). py.call(fname,..., json.opt.args = getoption("snakecharmr.json.opt.args", list(auto_unbox = TRUE, null = "null")), json.opt.ret = getoption("snakecharmr.json.opt.ret", list())) Value fname name of function/method to call... R objects to pass as arguments to the Python function or method json.opt.args json.opt.ret explicit arguments to pass to jsonlite::tojson when serializing the function argument values explicit arguments to pass to jsonlite::fromjson when deserializing the function return value an R object containing the function or method call return value after serialization to JSON in the Python environment and deserialization from JSON in the R environment py.call("len", 1:3) # [1] 3 py.call("repr", 1:3) # [1] "[1, 2, 3]" a <- 1:4 b <- 5:8 py.exec("def concat(a,b): return a+b") py.call("concat", a, b) # [1] 1 2 3 4 5 6 7 8

4 py.exec py.assign("a", "hola hola") py.call("a.split", " ") # [1] "hola" "hola" # by default jsonlite::tojson is called on the function call arguments # with auto_unbox = TRUE, so vectors of length 1 are simplified to # scalar values in Python py.call("repr", "foo bar") # [1] "u'foo bar'" # if we explicitly set auto_unbox to FALSE, a vector of length 1 # becomes a Python list of length 1 py.call("repr", "foo bar", json.opt.args = list(auto_unbox = FALSE)) # [1] "[u'foo bar']" py.exec Executes arbitrary Python code from R This function runs Python code that is provided in a character vector. py.exec(code, stoponexception = TRUE) Details code a character vector containing Python code, typically a single line with indentation and EOL characters as required by Python syntax stoponexception if TRUE then stop will be called if a Python exception occurs, otherwise only a warning will be flagged The character vector containing the code to execute may consist of a single string with EOL and indentation characters embedded. Alternatively, it can be a character vector, each entry containing one or more lines of Python code. a <- 1:4 b <- 5:8 py.exec(c("def concat(a,b):", "\treturn a+b")) py.call("concat", a, b) # [1] 1 2 3 4 5 6 7 8 ## Not run:

py.get 5 py.exec("raise Exception('Stop the presses!')") # Error in py.exec("raise Exception('Stop the presses!')") (from py.exec.r#48) : # Traceback (most recent call last): # File "<string>", line 2, in <module> # Exception: Stop the presses! ## End(Not run) py.exec("raise Exception('Houston, we have a problem!')", stoponexception = FALSE) # Warning message: # In py.exec("raise Exception('Houston, we have a problem!')", stoponexception = FALSE) : # Traceback (most recent call last): # File "<string>", line 2, in <module> # Exception: Houston, we have a problem! py.get Get values from Python variables to R py.get get the value of Python and returns it to the R environment. Objects are serialized as JSON strings on Python with json.dumps, are transferred to R and are converted back to an R value using jsonlite::fromjson. py.get(var.name, json.opt.ret = getoption("snakecharmr.json.opt.ret", list())) var.name json.opt.ret a character string containing a valid Python variable name explicit arguments to pass to jsonlite::fromjson when deserializing the value Value an R object containing the variable value after serialization to JSON in the Python environment and deserialization from JSON in the R environment py.assign("a", 1:4) py.get("a") # [1] 1 2 3 4 py.assign("b", list(one = 1, foo = "bar")) str(py.get("b")) # List of 2 # $ foo: chr "bar" # $ one: int 1

6 py.load py.exec("import math") py.get("math.pi") # [1] 3.141593 ## Not run: py.rm("notset") py.get("notset") # Error in py.get("notset") (from py.get.r#60) : Traceback (most recent call last): # File "<string>", line 2, in <module> # NameError: name 'notset' is not defined ## End(Not run) py.load Reads and executes Python code from a file This function runs Python code contained in a file. This is basically a convenience function that is the equivalent of py.exec(readlines(file)). py.load(file, stoponexception = TRUE) file a connection or file name that will be passed to readlines to read the Python code in the file stoponexception logical value indicating whether to check or not to call stop if a Python exception occurs Details For better maintainability, it might be worth investigating concentrating more complex Python code that needs to be called from R into proper packages that can be installed using pip and loaded with py.exec("import package_name"). Value if stoponexception is FALSE, invisibly returns a string representation of any raised Python exceptions or NULL if none occur.

py.rm 7 py.rm Remove a Python variable from R This function uses the del Python command to remove a variable and reclaim its memory. Any exceptions, such as the one that would happen if the variable did not exist, will be caught and ignored. py.rm(var.name, stoponexception = FALSE) var.name a character string containing a valid Python variable name stoponexception if TRUE then stop will be called if a Python exception occurs, typically because the variable doesn t exist, otherwise only a warning will be flagged py.assign("a", "foo bar") py.get("a") # [1] "foo bar" py.rm("a") ## Not run: py.rm("a") # Warning message: # In py.rm("a") : Traceback (most recent call last): # File "<string>", line 2, in <module> # NameError: name 'a' is not defined ## End(Not run) SnakeCharmR R and Python Integration A fork from rpython which uses jsonlite and has several fixes and improvements Author(s) Alexandre Sieira

Index Topic manip py.exec, 4 py.assign, 2 py.call, 3 py.exec, 4 py.get, 5 py.load, 6 py.rm, 7 SnakeCharmR, 7 SnakeCharmR-package (SnakeCharmR), 7 8