suppressPackageStartupMessages({
  library(minitypes)
})

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "figures/README-"
)

minitypes

AppVeyor build status Travis build status Coverage status

minitypes is a collection of type conversion tools for R atomic vectors.

This package is built upon R's type conversion tools and aims to:

Supported Types:

Installation

You can install minitypes from github with:

# install.packages("devtools")
devtools::install_github("coolbutuseless/minitypes")

Promotion Rules

Examples - Functions to convert supported types to/from raw

r <- as.raw(c(1, 2, 0, 255))

raw_to_uint8(r)

raw_to_uint16(r, endian = 'little')

int32_to_raw(1L, endian = 'big')

raw_to_bitstring(r)

logical_to_raw(c(T, F, F), pad = 'left')

convert_type() - conversion between any two supported types

All supported types can be converted to/from the raw type. Thus any two supported types can be converted between each other by using raw as the intermediary.

However, for complex endian/bit-order conversions (or cases where raw intermediary would lose information about the conversion), it is probably safer to do it manually as a two-step process.

convert_type("1111111100010011", src_type='bitstring', dst_type='uint8')

convert_type(c(1L, 2L, 3L), src_type = 'int32', dst_type = 'uint8', endian = 'big')

Examples - Specialised conversion functions

minitypes includes some functions for direct conversion between two supported types.

This may be for efficiency reasons, but is often because going through an intermediate raw step would lose information needed for the complete conversion.

bitstring_to_logical("100 011")

int32_to_bitstring(1L, endian = 'big')

int32_to_bitstring(1L, endian = 'little')

int32_to_bitstring(1L, endian = 'little', first_bit = 'lsb')

Helper functions

Useless tricks - NA_integer_ & NA_character_ bit representation

NA_integer_ and NA_character_ are just specific bit patterns within each of those types.

convert_type(NA_integer_, 'int32', 'bitstring', endian = 'big', first_bit = 'msb')

convert_type(NA_character_, 'chr', 'bitstring', endian = 'big', first_bit = 'msb')

Worked Example - Parsing a colour table from a 'gif'

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# A scarlet tanager!
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gif_filename <- "figures/bird.gif"
gif          <- readBin(gif_filename, 'raw', n = file.size(gif_filename))

head(gif, 50)

r glue::glue("![]({gif_filename})")

#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Subset out the header components and convert to the correct type
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
raw_to_chr(gif[1:6])  # "GIF89a" (text)

raw_to_uint16(gif[7:8], endian = 'little')  # width (integer)

raw_to_uint16(gif[9:10], endian = 'little')  # height (integer)

# Is a global colour table present?
colour_table_info <- raw_to_uint8(gif[11])  
if (bitwAnd(colour_table_info, 128L)) { print("GCT present") }

# read 255 RGB triplets has 6-char-hex (i.e. hex24) starting at position 14
colour_table <- raw_to_hex24(gif[seq_len(255*3) + 13L], endian = 'big')
colour_table <- sort(paste0('#', colour_table))

head(colour_table)

image(x = 1, y = 1:255, z = matrix(1:255, nrow = 1), col = colour_table, axes = FALSE, ann = FALSE)


coolbutuseless/minitypes documentation built on May 27, 2019, 9:55 a.m.