Jim Hester
=CONCATENATE(A1, " ", B1)
apples <- 3
bananas <- 2
paste0("Inventory", "\n",
" Apples: ", apples, "\n",
" Bananas: ", bananas, "\n",
"Total: ", apples + bananas)
## Inventory
## Apples: 3
## Bananas: 2
## Total: 5
## Inventory
## Apples: 3
## Bananas: 2
## Total: 5
apples <- 3
bananas <- 2
glue("
Inventory
Apples: {apples}
Bananas: {bananas}
Total: {apples + bananas}")
## Inventory
## Apples: 3
## Bananas: 2
## Total: 5
## You can indent naturally and break up long lines if needed.
## 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
## Tim Filiga Cahill
## Harry NA Kewell
## Mark NA Schwarzer
## Tim Filiga Cahill
## NA
## NA
## Tim Filiga Cahill
## Harry Kewell
## Mark Schwarzer
.open
and .close
change delimitersj_glue <- function(..., .envir = parent.frame()) {
glue(..., .open = "<", .close = ">", .envir = .envir)
}
apples <- 1; bananas <- 2
json <- j_glue('
{
"fruits": {
"apples": <apples>,
"bananas": <bananas>
}
}')
jsonlite::fromJSON(json)
## $fruits
## $fruits$apples
## [1] 1
##
## $fruits$bananas
## [1] 2
.transformer
are robustshell_transformer <- function(code, envir) {
shQuote(eval(parse(text = code), envir))
}
glue_sh <- function(..., .envir = parent.frame()) {
glue(..., .envir = .envir, .transformer = shell_transformer)
}
filename <- "test"
writeLines("hello!", filename)
cmd <- glue_sh("cat {filename}")
cmd
## cat 'test'
.transformer
are useful## π: 3.1415927
## 46.88% of mtcars have 3 gears
.transformer
are fun 🎉## If life gives you 🍋 make 🍹
## Many 🤙👋👏👐🙌 make 🎃 🏢
{
}
parsing in Cparse()
eval()
and paste0()
.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 stringscon <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
tbl <- DBI::Id(schema = "xyz", table = "iris")
glue_sql(
"SELECT * FROM {`tbl`} WHERE species IN ({vals*})",
vals = c("setosa", "versicolor"), .con = con)
## <SQL> SELECT * FROM `xyz`.`iris` WHERE species IN ('setosa', 'versicolor')
str_glue()
is in library(tidyverse)
str_glue("
glue version: {packageVersion('glue')}
tidyverse version: {packageVersion('tidyverse')}
")
## glue version: 1.2.0.9000
## tidyverse version: 1.2.1
60 reverse dependencies
dplyr, stringr, tidyr dependency, maybe already installed
install.packages("glue")
adjective <- c("luminous", "stylish", "super", "striking",
"impressive", "fantastic")
glue("Have a {sample(adjective, 1)} day!")
## Have a super day!