GithubHelp home page GithubHelp logo

rlugojr / glue-1 Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tidyverse/glue

0.0 1.0 0.0 51 KB

Glue strings to data in R. Small, fast, dependency free interpreted string literals.

License: Other

R 79.91% C 20.09%

glue-1's Introduction

glue

Travis-CI Build Status Coverage Status

Glue strings to data in R. Small, fast, dependency free interpreted string literals.

Installation

# install.packages("devtools")
devtools::install_github("tidyverse/glue")

Usage

Long strings can be broken by line and will be concatenated together

name <- "Fred"
age <- 50
anniversary <- as.Date("1991-10-12")
glue('My name is {name},',
  ' my age next year is {age + 1},',
  ' my anniversary is {format(anniversary, "%A, %B %d, %Y")}.')
#> My name is Fred, my age next year is 51, my anniversary is Saturday, October 12, 1991.

You can use named arguments to assign temporary variables

glue('My name is {name},',
  ' my age next year is {age + 1},',
  ' my anniversary is {format(anniversary, "%A, %B %d, %Y")}.',
  name = "Joe",
  age = 40,
  anniversary = as.Date("2001-10-12"))
#> My name is Joe, my age next year is 41, my anniversary is Friday, October 12, 2001.

glue_data() is useful in magrittr pipes.

library(magrittr)
mtcars %>% glue_data("{rownames(.)} 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
#> Duster 360 has 245 hp
#> Merc 240D has 62 hp
#> Merc 230 has 95 hp
#> Merc 280 has 123 hp
#> Merc 280C has 123 hp
#> Merc 450SE has 180 hp
#> Merc 450SL has 180 hp
#> Merc 450SLC has 180 hp
#> Cadillac Fleetwood has 205 hp
#> Lincoln Continental has 215 hp
#> Chrysler Imperial has 230 hp
#> Fiat 128 has 66 hp
#> Honda Civic has 52 hp
#> Toyota Corolla has 65 hp
#> Toyota Corona has 97 hp
#> Dodge Challenger has 150 hp
#> AMC Javelin has 150 hp
#> Camaro Z28 has 245 hp
#> Pontiac Firebird has 175 hp
#> Fiat X1-9 has 66 hp
#> Porsche 914-2 has 91 hp
#> Lotus Europa has 113 hp
#> Ford Pantera L has 264 hp
#> Ferrari Dino has 175 hp
#> Maserati Bora has 335 hp
#> Volvo 142E has 109 hp

Leading whitespace and blank lines are automatically trimmed, which lets you indent the strings naturally.

glue("
    A Formatted string
    Can have multiple lines
      with additional indention preserved
    ")
#> A Formatted string
#> Can have multiple lines
#>   with additional indention preserved

A literal brace can be inserted by using doubled braces.

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

All valid R code works in expressions, including braces and escaping. Backslashes do need to be doubled just like in all R strings.

  `foo}\`` <- "foo"
glue("{
      {
        '}\\'' # { and } in comments, single quotes
        \"}\\\"\" # or double quotes are ignored
        `foo}\\`` # as are { in backticks
      }
  }")
#> foo

Other implementations

Some other implementations of string interpolation in R (although not using identical syntax).

glue-1's People

Contributors

jimhester avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.