data_decode: Decode and encode text and binary data files

Description Usage Arguments Details Value Functions Author(s) See Also Examples

View source: R/data_engine.R

Description

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).

Usage

1
2
3
data_decode(data, encoding, as_text = FALSE, options = list())

data_encode(file, encoding, options = list(), output = NULL)

Arguments

data

Encoded data as a character string

encoding

Either 'base64' or 'gpg'

as_text

A boolean indicating if decoded data should be treated as text (TRUE) or binary (FALSE). Defaults to FALSE, meaning binary.

options

A list containing extra arguments for the encoding/decoding functions. For base64 encoding, linewidth (defaults to 64) and and newline (defaults to platform.newline()) optional arguments are possible. For gpg encoding, see the description below for details regarding the required receiver option to define the key to use for encryption. For further details and potentially other additional arguments, see the help of the corresponding underlying encoding functions: base64_encode and gpg_encrypt.

file

Path to file containing data to be encoded

output

Path where encoded data is to be stored. Optional; if NULL then encoded data will not be written to a file.

Details

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.

Value

Returns either the decoded data (data_decode) or the encoded data (data_encode).

Functions

Author(s)

David M. Kaplan dmkaplan2000@gmail.com

See Also

See also base64_encode and gpg_encrypt, platform.newline.

Examples

 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)

knitrdata documentation built on Dec. 8, 2020, 5:08 p.m.