writebin in R: How to Save Data as byte File in R

binary file is a type of computer file that is not a text file. The binary file is not readable by humans.  The term “binary file” is usually used as a term meaning “non-text file“. A binary file’s code should be evaluated by a program or a hardware processor that knows exactly how it is formatted.

writebin in R

The writebin() is a built-in R function that saves geographic data as a byte file in gzip compressed format (‘.gz’). The writebin() function only works 2D (one layer) spatial object.

Syntax

writebin(spadata, filename, folder, param)

Arguments

spatdata: It is a one-layer-raster-object or matrix holding spatial data.

param: It is a character string showing the parameter name for the dataset treatment. See the parameter_definitions for available parameters.

filename: It is a character string naming the ‘.gz’-file to be created.

folder: It is a character string indicating the target directory.

Return Value

It returns NULL or a raw vector (if con is a raw vector).

Example

tf <- tempfile()
x <- as.integer(c(-2, 1) * 2 ^ (0:21))
writeBin(con = tf, x)
readBin(tf, integer(), n = 20)
unlink(tf)

Output

[1]  -2   2   -8   8  -32  32  -128  128  -512
[10] 512 -2048 2048 -8192 8192 -32768 32768 -131072 131072
[19] -524288 524288

You can see from that output is not human readable. It can only read by hardware. A binary file is a file that contains information stored only in the form of bits and bytes like 0’s and 1’s.

Sometimes, the data generated by other programs must be processed by R as a binary file. Also, R is required to create binary files which can be shared with other programs.

That is it for writebin() Function in R.

Leave a Comment