Skip to content

Instantly share code, notes, and snippets.

@coolbutuseless
Created June 26, 2024 22:47
Show Gist options
  • Save coolbutuseless/b2419880c6cf4199505de174c9b9d9c2 to your computer and use it in GitHub Desktop.
Save coolbutuseless/b2419880c6cf4199505de174c9b9d9c2 to your computer and use it in GitHub Desktop.
Deparse summarises non-altrep integer vectors to a range
# remotes::install_github("multimeric/altrepr")
library(altrepr)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Create a long vector of consecutive INTEGER values
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int_vec_str <- sprintf("c(%s)", paste(5:(2^18), 'L', sep = '', collapse = ", "))
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Simply creating the integer vector doesn't make it an altrep
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
int_vec <- eval(parse(text = int_vec_str))
int_vec
is.integer(int_vec)
altrepr::is_altrep(int_vec)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 'deparse1()' then detects this is a range and summarises it as "first:last"
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
deparse(int_vec)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment