Glue strings to data with

Jim Hester




@jimhester_
jimhester
jim.hester@rstudio.com

Gluing strings is everywhere

Excel

=CONCATENATE(A1, " ", B1)

Bash

python

Gluing strings in R is painful

## Inventory
##   Apples: 3
##   Bananas: 2
## Total: 5
## Inventory
##   Apples: 3
##   Bananas: 2
## Total: 5

glue makes gluing strings easy!

## Inventory
##   Apples: 3
##   Bananas: 2
## Total: 5

glue is convienent

## You can indent naturally and break up long lines if needed.

glue is safe

## 1 will recycle with a length of 1 a
## 2 will recycle with a length of 1 a
## 3 will recycle with a length of 1 a
## 1 will recycle with the same length a
## 2 will recycle with the same length b
## 3 will recycle with the same length c
## Error: Variables must be length 1 or 3

glue handles missings

## Tim Filiga Cahill
## Harry NA Kewell
## Mark NA Schwarzer
## Tim Filiga Cahill
## NA
## NA
## Tim Filiga Cahill
## Harry  Kewell
## Mark  Schwarzer

glue is for packages

  • Zero dependencies, tested to R 3.1
  • Customizable
  • Fast

glue is customizable

.open and .close change delimiters

## $fruits
## $fruits$apples
## [1] 1
## 
## $fruits$bananas
## [1] 2

.transformer are robust

## cat 'test'

.transformer are useful

## π: 3.1415927
## 46.88% of mtcars have 3 gears

.transformer are fun 🎉

## If life gives you 🍋 make 🍹
## Many 🤙👋👏👐🙌 make 🎃 🏢

glue is fast

Types of speed

  • Speed of writing
  • Speed of recall
  • Speed of execution

glue is fast on single strings

benchmark length 1 string

glue is fast on lots of strings

benchmark length 100k string

glue is fast

glue contains more than glue()

glue_data() binds strings to any list / environment

## Mazda RX4 has 6 cylinders and 110hp
## Mazda RX4 Wag has 6 cylinders and 110hp
## Datsun 710 has 4 cylinders and 93hp
## Hornet 4 Drive has 6 cylinders and 110hp
## Hornet Sportabout has 8 cylinders and 175hp
## Valiant has 6 cylinders and 105hp

glue_collapse() collapses vectors

## 1 2 3 4 5 6 7 8 9 10
## 1 2 3 4...
## `1`, `2`, `3`, `4`, `5`, `6`, `7`, `8`, `9` and `10`

glue_sql() quotes SQL strings

## <SQL> SELECT * FROM `xyz`.`iris` WHERE species IN ('setosa', 'versicolor')

str_glue() is in

## glue version: 1.2.0.9000
## tidyverse version: 1.2.1
  • 60 reverse dependencies

  • Monthly downloads blah
  • dplyr, stringr, tidyr dependency, maybe already installed

install.packages("glue")

glue.tidyverse.org

## Have a super day!




@jimhester_   jimhester   jim.hester@rstudio.com