suppressPackageStartupMessages({ library(minitypes) }) knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.path = "figures/README-" )
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:
uint8raw byte format. chr logical - R's built-in logical type e.g. c(T, T, F)bit - compact bit representation (from the bit package)bitstring - character representaction of bits e.g. 110uint8 - R's built-in integer type. Checked to be within range [0 - 255]int8 - R's built-in integer type. Checked to be within range [-128, 127]hex8 - 2 character hexadecimal representation of a uint8uint16 - R's built-in integer type. Checked to be within range [0 - 2^16-1]int16 - R's built-in integer type. Checked to be within range [-2^15, 2^15-1]hex16 - 4 character hexadecimal representation of a uint16uint24 - R's built-in integer type. Checked to be within range [0 - 2^24-1]hex24 - 6 character hexadecimal representation of a uint24int32 - R's built-in integer typeYou can install minitypes from github with:
# install.packages("devtools") devtools::install_github("coolbutuseless/minitypes")
raw int32pad and the value (T/F) to pad with can be specifieduint8 values to int32raw values to uint16rawr <- 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 typesAll 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')
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')
raw_reverse_bits() - reverse the bits in a raw byteraw_swap_endian() - swap the order of a group of bytes (nbytes at a time) within a raw vectorNA_integer_ & NA_character_ bit representationNA_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')
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # A scarlet tanager! #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ gif_filename <- "figures/bird.gif" gif <- readBin(gif_filename, 'raw', n = file.size(gif_filename)) head(gif, 50)
r glue::glue("")
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # 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)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.