glue::glue()
get_help()
docs
The glue()
function is part of the identically-named {glue}
package, which is part of the {tidyverse}
.
We use glue()
conveniently combine multiple strings into one. glue()
is similar to the base R function paste()
, except it makes it much easier to include variables in your string. By default, glue()
will not insert any space or chatacter between strings being combined. You can change this behavior with the argument sep
.
To use this function, you need to either first load the {glue}
library, or always use the function with glue::glue()
notation.
# Load the library
library(glue)
# Or, load the full tidyverse:
library(tidyverse)
# Or, use :: notation
::glue() glue
glue("combine this string {and this variable} into a full single string")
Some examples below use the carnivores
dataset. Learn more about this dataset with get_help("carnivores")
.
# Show the carnivores dataset
carnivores
## # A tibble: 9 × 4
## name genus awake brainwt
## <chr> <fct> <dbl> <dbl>
## 1 Arctic fox Vulpes 11.5 0.0445
## 2 Cheetah Acinonyx 11.9 NA
## 3 Dog Canis 13.9 0.07
## 4 Gray seal Haliochoerus 17.8 0.325
## 5 Jaguar Panthera 13.6 0.157
## 6 Lion Panthera 10.5 NA
## 7 Northern fur seal Callorhinus 15.3 NA
## 8 Red fox Vulpes 14.2 0.0504
## 9 Tiger Panthera 8.2 NA
# Quickly create strings that contain variables using {} around variable names, without ending quotes!
<- "bears"
best_animals <- "mosquitos"
worst_animals glue("We love {best_animals}, but we hate {worst_animals}.")
## We love bears, but we hate mosquitos.
# Another example of embedding variables into a string. Again, quotes enclose the entire argument to `glue()`
<- "introverse"
favorite_package glue("My favorite R package is the {favorite_package}.")
## My favorite R package is the introverse.
# Create a new column called `full_info` in carnivores that is comprised of: "name (genus)"
# Again, directly refer to column names but surround them in curly braces {}
# You'll notice the column comes out a <chr> type. That's ok! It behaves like <chr>
%>%
carnivores ::mutate(full_info = glue("{name} ({genus})")) dplyr
## # A tibble: 9 × 5
## name genus awake brainwt full_info
## <chr> <fct> <dbl> <dbl> <glue>
## 1 Arctic fox Vulpes 11.5 0.0445 Arctic fox (Vulpes)
## 2 Cheetah Acinonyx 11.9 NA Cheetah (Acinonyx)
## 3 Dog Canis 13.9 0.07 Dog (Canis)
## 4 Gray seal Haliochoerus 17.8 0.325 Gray seal (Haliochoerus)
## 5 Jaguar Panthera 13.6 0.157 Jaguar (Panthera)
## 6 Lion Panthera 10.5 NA Lion (Panthera)
## 7 Northern fur seal Callorhinus 15.3 NA Northern fur seal (Callorhinus)
## 8 Red fox Vulpes 14.2 0.0504 Red fox (Vulpes)
## 9 Tiger Panthera 8.2 NA Tiger (Panthera)
# You can also use as.character() to ensure a <chr> column type that was created with glue()
# Use `get_help("as.character") to learn more
%>%
carnivores # Create column and coerce it into a character type
::mutate(full_info = glue("{name} ({genus})"),
dplyrfull_info = as.character(full_info))
## # A tibble: 9 × 5
## name genus awake brainwt full_info
## <chr> <fct> <dbl> <dbl> <chr>
## 1 Arctic fox Vulpes 11.5 0.0445 Arctic fox (Vulpes)
## 2 Cheetah Acinonyx 11.9 NA Cheetah (Acinonyx)
## 3 Dog Canis 13.9 0.07 Dog (Canis)
## 4 Gray seal Haliochoerus 17.8 0.325 Gray seal (Haliochoerus)
## 5 Jaguar Panthera 13.6 0.157 Jaguar (Panthera)
## 6 Lion Panthera 10.5 NA Lion (Panthera)
## 7 Northern fur seal Callorhinus 15.3 NA Northern fur seal (Callorhinus)
## 8 Red fox Vulpes 14.2 0.0504 Red fox (Vulpes)
## 9 Tiger Panthera 8.2 NA Tiger (Panthera)
# Create a new column called `description` in carnivores that is comprised of the sentence:
# "The species {name} is awake, on average, {awake} hours per day."
%>%
carnivores ::mutate(description = glue("The species {name} is awake {awake} hours per day.")) dplyr
## # A tibble: 9 × 5
## name genus awake brainwt description
## <chr> <fct> <dbl> <dbl> <glue>
## 1 Arctic fox Vulpes 11.5 0.0445 The species Arctic fox is awake 11.5 hours per …
## 2 Cheetah Acinonyx 11.9 NA The species Cheetah is awake 11.9 hours per day.
## 3 Dog Canis 13.9 0.07 The species Dog is awake 13.9 hours per day.
## 4 Gray seal Haliochoerus 17.8 0.325 The species Gray seal is awake 17.8 hours per d…
## 5 Jaguar Panthera 13.6 0.157 The species Jaguar is awake 13.6 hours per day.
## 6 Lion Panthera 10.5 NA The species Lion is awake 10.5 hours per day.
## 7 Northern fur seal Callorhinus 15.3 NA The species Northern fur seal is awake 15.3 hou…
## 8 Red fox Vulpes 14.2 0.0504 The species Red fox is awake 14.2 hours per day.
## 9 Tiger Panthera 8.2 NA The species Tiger is awake 8.2 hours per day.