You signed in with another tab or window.Reload to refresh your session.You signed out in another tab or window.Reload to refresh your session.You switched accounts on another tab or window.Reload to refresh your session.Dismiss alert
b64 is a dependency free, fast, lightweight, and vectorized base64encoder and decoder.
Installation
You can install b64 from CRAN with
install.packages("b64")
Example
Encode to base64 usingencode().
library(b64)hello<- encode("Hello, from extendr")hello#> [1] "SGVsbG8sIGZyb20gZXh0ZW5kcg=="
Decode usingdecode(). Note that the returned object will always havethe"blob" class. To achieve 0 dependencies, theblob package isonly listed as a suggested dependency but if you attach it, its printmethod will be used.
b64 shines when encoding and decoding files.encode_file() anddecode_file() both work by reading a file as a stream making it farfaster than the alternative.
While the encoding is very impressive, better yet is the decodingperformance.
# create a temp filetmp2<- tempfile()# encode it and write to tmep fileencode_file(tmp)|> charToRaw()|> writeBin(tmp2)bench::mark(b64= decode_file(tmp2),base64enc=base64enc::base64decode(file(tmp2)))#> # A tibble: 2 × 6#> expression min median `itr/sec` mem_alloc `gc/sec`#> <bch:expr> <bch:tm> <bch:tm> <dbl> <bch:byt> <dbl>#> 1 b64 107.09µs 122.6µs 7414. 164KB 8.77#> 2 base64enc 1.58ms 1.6ms 602. 172KB 3.70
Alternative engines
Out of the box,b64 provides a number of pre-configured engines thatcan be used. The functionengine() allows you to choose one of thesedifferent engines For example,engine("url_safe") provides a standardengine that uses a url-safe alphabet with padding.
We can create custom engines withnew_engine(). This allows us toprovide our on alphabet and configuration.
We can use one of the many predefined alphabets or create one our selveswithnew_alphabet(). We can also specify our engine config usingnew_config() which lets us choose whether or not to pad and how tohandle decoding.