Getting an R raw vector from byte string in `reticulate`


#1

When using reticulate and Python objects/functions return something like:

b'12345abcd!@?'

how would one go about getting that as a raw vector (not a Unicode string) back in R?

pyhex_to_raw <- function(pyhex) {
  as.raw(
    strtoi(
      vapply(
        X = seq(1, nchar(pyhex), by = 2), 
        FUN = function(.x) substr(pyhex, .x, .x+1),
        FUN.VALUE = "character",
        USE.NAMES = FALSE
      ),
      16L
    )
  )
}

pyhex_to_raw(python_object$hex())

seems like a pretty hack-ish way to accomplish it (but it works).