ProductPromotion
Logo

R Programming

made by https://0x3d.site

GitHub - tidyverse/glue: Glue strings to data in R. Small, fast, dependency free interpreted string literals.
Glue strings to data in R. Small, fast, dependency free interpreted string literals. - tidyverse/glue
Visit Site

GitHub - tidyverse/glue: Glue strings to data in R. Small, fast, dependency free interpreted string literals.

GitHub - tidyverse/glue: Glue strings to data in R. Small, fast, dependency free interpreted string literals.

glue

CRAN_Status_Badge R-CMD-check test-coverage

glue offers interpreted string literals that are small, fast, and dependency-free. glue does this by embedding R expressions in curly braces, which are then evaluated and inserted into the string.

Installation

# Install released version from CRAN
install.packages("glue")
# Install development version from GitHub
pak::pak("tidyverse/glue")

Usage

glue() makes it easy to interpolate data into strings:

library(glue)

name <- "Fred"
glue("My name is {name}.")
#> My name is Fred.

stringr::str_glue() is an alias for glue::glue(). So if you’ve already attached stringr (or perhaps the whole tidyverse), you can use str_glue() to access all of the functionality of glue():

library(stringr) # or library(tidyverse)

name <- "Wilma"
str_glue("My name is {name}.")
#> My name is Wilma.

You’re not limited to using a bare symbol inside {}; it can be any little bit of R code:

name <- "Pebbles"
glue("Here is my name in uppercase and doubled: {strrep(toupper(name), 2)}.")
#> Here is my name in uppercase and doubled: PEBBLESPEBBLES.

Data can come from various sources

glue can interpolate values from the local environment or from data passed in name = value form:

x <- "the local environment"
glue(
  "`glue()` can access values from {x} or from {y}. {z}",
  y = "named arguments",
  z = "Woo!"
)
#> `glue()` can access values from the local environment or from named arguments. Woo!

If the relevant data lives in a data frame (or list or environment), use glue_data() instead:

mini_mtcars <- head(cbind(model = rownames(mtcars), mtcars))
glue_data(mini_mtcars, "{model} has {hp} hp.")
#> Mazda RX4 has 110 hp.
#> Mazda RX4 Wag has 110 hp.
#> Datsun 710 has 93 hp.
#> Hornet 4 Drive has 110 hp.
#> Hornet Sportabout has 175 hp.
#> Valiant has 105 hp.

glue_data() is very natural to use with the pipe:

mini_mtcars |>
  glue_data("{model} gets {mpg} miles per gallon.")
#> Mazda RX4 gets 21 miles per gallon.
#> Mazda RX4 Wag gets 21 miles per gallon.
#> Datsun 710 gets 22.8 miles per gallon.
#> Hornet 4 Drive gets 21.4 miles per gallon.
#> Hornet Sportabout gets 18.7 miles per gallon.
#> Valiant gets 18.1 miles per gallon.

These glue_data() examples also demonstrate that glue() is vectorized over the data.

What you see is awfully close to what you get

glue() lets you write code that makes it easy to predict what the final string will look like. There is considerably less syntactical noise and mystery compared to paste() and sprintf().

Empty first and last lines are automatically trimmed, as is leading whitespace that is common across all lines. You don’t have to choose between indenting your code properly and getting the output you actually want. Consider what happens when glue() is used inside the body of a function:

foo <- function() {
  glue("
    A formatted string
    Can have multiple lines
      with additional indentation preserved")
}
foo()
#> A formatted string
#> Can have multiple lines
#>   with additional indentation preserved

The leading whitespace that is common to all 3 lines is absent from the result.

Learning more

glue is a relatively small and focused package, but there’s more to it than the basic usage of glue() and glue_data() shown here. More recommended functions and resources:

  • The “Get started” article (vignette("glue")) demonstrates more interesting features of glue() and glue_data().
  • glue_sql() and glue_data_sql() are specialized functions for producing SQL statements.
  • glue provides a couple of custom knitr engines that allow you to use glue syntax in chunks; learn more in vignette("engines", package = "glue").

Code of Conduct

Please note that this project is released with a Contributor Code of Conduct. By participating in this project, you agree to abide by its terms.

More Resources
to explore the angular.

mail [email protected] to add your project or resources here 🔥.

Related Articles
to learn about angular.

FAQ's
to learn more about Angular JS.

mail [email protected] to add more queries here 🔍.

More Sites
to check out once you're finished browsing here.

0x3d
https://www.0x3d.site/
0x3d is designed for aggregating information.
NodeJS
https://nodejs.0x3d.site/
NodeJS Online Directory
Cross Platform
https://cross-platform.0x3d.site/
Cross Platform Online Directory
Open Source
https://open-source.0x3d.site/
Open Source Online Directory
Analytics
https://analytics.0x3d.site/
Analytics Online Directory
JavaScript
https://javascript.0x3d.site/
JavaScript Online Directory
GoLang
https://golang.0x3d.site/
GoLang Online Directory
Python
https://python.0x3d.site/
Python Online Directory
Swift
https://swift.0x3d.site/
Swift Online Directory
Rust
https://rust.0x3d.site/
Rust Online Directory
Scala
https://scala.0x3d.site/
Scala Online Directory
Ruby
https://ruby.0x3d.site/
Ruby Online Directory
Clojure
https://clojure.0x3d.site/
Clojure Online Directory
Elixir
https://elixir.0x3d.site/
Elixir Online Directory
Elm
https://elm.0x3d.site/
Elm Online Directory
Lua
https://lua.0x3d.site/
Lua Online Directory
C Programming
https://c-programming.0x3d.site/
C Programming Online Directory
C++ Programming
https://cpp-programming.0x3d.site/
C++ Programming Online Directory
R Programming
https://r-programming.0x3d.site/
R Programming Online Directory
Perl
https://perl.0x3d.site/
Perl Online Directory
Java
https://java.0x3d.site/
Java Online Directory
Kotlin
https://kotlin.0x3d.site/
Kotlin Online Directory
PHP
https://php.0x3d.site/
PHP Online Directory
React JS
https://react.0x3d.site/
React JS Online Directory
Angular
https://angular.0x3d.site/
Angular JS Online Directory