as.character.python.builtin.str | R Documentation |
Convert a Python string to an R Character Vector
## S3 method for class 'python.builtin.str'
as.character(x, nul = stop("Embedded NUL in string."), ...)
x |
A Python string |
nul |
Action to take if the Python string contains an embedded NUL (
|
... |
Unused |
An R character vector. The returned vector will always of length 1,
unless nul = NULL
was supplied.
# Given a Python function that errors when it attempts to return
# a string with an embedded NUL
py_run_string('
def get_string_w_nul():
return "a b" + chr(0) + "c d"
')
get_string_w_nul <- py$get_string_w_nul
try(get_string_w_nul()) # Error : Embedded NUL in string.
# To get the string into R, use `r_to_py()` on the function to stop it from
# eagerly converting the Python string to R, and then call `as.character()` with
# a `nul` argument supplied to convert the string to R.
get_string_w_nul <- r_to_py(get_string_w_nul)
get_string_w_nul() # unconverted python string: inherits(x, 'python.builtin.str')
as.character(get_string_w_nul(), nul = "<NUL>") # Replace: "a b<NUL>c d"
as.character(get_string_w_nul(), nul = "") # Remove: "a bc d"
as.character(get_string_w_nul(), nul = NULL) # Split: "a b" "c d"
# cleanup example
rm(get_string_w_nul); py$get_string_w_nul <- NULL
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.