Description Usage Arguments Details Value Functions Author(s) See Also Examples
These helper functions allow one to encode as text a binary or text data file
using either base64
or gpg
encoding (data_encode
) and
decode text-encoded data back into its original binary or text format
(data_decode
).
1 2 3 | data_decode(data, encoding, as_text = FALSE, options = list())
data_encode(file, encoding, options = list(), output = NULL)
|
data |
Encoded data as a character string |
encoding |
Either |
as_text |
A boolean indicating if decoded data should be treated as text
( |
options |
A list containing extra arguments for the encoding/decoding
functions. For |
file |
Path to file containing data to be encoded |
output |
Path where encoded data is to be stored. Optional; if
|
Encoding and decoding in base64
format uses functionality from the
xfun
package, whereas encoding and decoding
using gpg
uses functionality from the
gpg
package. See those packages for more
details on the encoding and decoding process and setting up a gpg
keyring.
data_encode
takes the name of a file containing the binary or text
data to be encoded and returns the encoded data as a character string. The
encoded data is returned silently to avoid outputing to the screen large
amounts of encoded data. If you want to visualize the encoded data, use the
cat
function. For larger data files, set the output
argument to
a path where the encoded data will be stored.
data_encode
takes a character string of encoded data and returns
either a character string of decoded data (if as_text=TRUE
) or a raw
vector of decoded binary data (if as_text=FALSE
).
For both functions, the options
input argument takes a list of
additional input arguments that are passed directly to the encoding or
decoding functions in the respective packages that handle the actual data
translation. See base64_encode
and
gpg_encrypt
for details.
For gpg
encoding and decoding, in addition to installing the
gpg
package, a gpg
keyring must be
installed and properly configured. For encoding, the receiver
and
potentially the signer
arguments must be supplied as elements of the
options
input argument.
Returns either the decoded data (data_decode
) or the encoded
data (data_encode
).
data_decode
: Returns decoded data as either a character string
(as_text=TRUE
) or raw vector (as_text=FALSE
).
data_encode
: Returns data encoded as a character string using
base64
or gpg
encoding.
David M. Kaplan dmkaplan2000@gmail.com
See also base64_encode
and
gpg_encrypt
, platform.newline
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | # Use a temporary directory ----------------------------
owd = getwd()
td = tempdir()
setwd(td)
# Do some data encoding and decoding ------------------
library(knitrdata)
x = data.frame(a=1:5,b=letters[1:5])
write.csv(x,"test.csv")
saveRDS(x,"test.RDS")
enccsv = data_encode("test.csv","base64")
encrds = data_encode("test.RDS","base64")
csv = data_decode(enccsv,"base64",as_text=TRUE)
cat(csv)
rds = data_decode(encrds,"base64",as_text=FALSE)
writeBin(rds,"test_output.RDS")
y = readRDS("test_output.RDS")
y
params = list(run_gpg=FALSE)
if (requireNamespace("gpg") && params$run_gpg) {
k = gpg::gpg_keygen("test","test@test.org")
encgpg = data_encode("test.csv","gpg",options = list(receiver=k))
cat(data_decode(encgpg,"gpg",as_text=TRUE))
gpg::gpg_delete(k,secret=TRUE)
}
# Cleanup ------------------------------------
file.remove("test.csv","test.RDS","test_output.RDS")
setwd(owd)
|
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.