https://github.com/coolbutuseless/gluestick
Simple, single-function string interpolation in Base R
https://github.com/coolbutuseless/gluestick
Last synced: 25 days ago
JSON representation
Simple, single-function string interpolation in Base R
- Host: GitHub
- URL: https://github.com/coolbutuseless/gluestick
- Owner: coolbutuseless
- License: mit
- Created: 2021-06-04T12:10:13.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2021-06-05T12:33:02.000Z (almost 4 years ago)
- Last Synced: 2025-03-18T01:11:17.541Z (28 days ago)
- Language: R
- Size: 14.6 KB
- Stars: 41
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- jimsghstars - coolbutuseless/gluestick - Simple, single-function string interpolation in Base R (R)
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = FALSE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)library(gluestick)
```# gluestick

[](https://github.com/coolbutuseless/gluestick/actions)The goal of the `gluestick` package is to provide a home for a single, simple function
(also named `gluestick`).This `gluestick()` function is meant to be an almost drop-in replacement
for 99.9% of the reasons I use the amazing [`glue`](https://cran.r-project.org/package=glue) package.The idea is that you would just steal the function out of this package and use
it in your own package in order to avoid having [`glue`](https://cran.r-project.org/package=glue)
as a dependency.## Benefits
* Single function
* No dependencies
* No C code
* Vanilla base R code (easy to hack upon)## Limitations
* There is no special handling when delimiters are repeated. This differs from
`glue` where doubling the delimiter escpaes it.
* No support for glue's idea of data transformers
* Far fewer sanity checks than glue## Installation
You can install from [GitHub](https://github.com/coolbutuseless/gluestick) with:
``` r
# install.package('remotes')
remotes::install_github('coolbutuseless/gluestick')
```In reality, I can't see anyone actually wanting to install/use this package
in preference to the glue package.What I envisage is that if you want something "glue-like" to include in your
own work, you can just steal the `gluestick()` function out of this package
and include it in your own package.## Basic Usage
```{r example}
library(gluestick)#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# By default, `src` data is the calling environment
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
name <- 'Mike'
gluestick("Hello {name}")#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# R code will be evaluated
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gluestick("Hello {name}. Score = {1 + 2}")#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Can pass in a list, data.frame, environment etc as the data source to
# override the default
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gluestick("Hello {name}", src = list(name = '#RStats'))#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Delimiters are user-definable
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gluestick("Hello ~~!name]]?", open = "~~!", close = "]]?")
```## Do not evaulate expressions - treat them as referencing named variables only
In some circumstances it might be wise to assume that the expressions aren't actually
R code, but only refer to names of variables in the data source. This might
be useful when evaluating format strings input by an untrusted user.If `eval=FALSE` is set, then expressions will be treated as variable names,
`mget()` will be used to fetch them from the data source, and no code
evaluation will occur.```{r error = TRUE}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This will work, as 'name' is a variable in the data `src` (i.e. the calling environment)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gluestick("Hello {name}", eval = FALSE)#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# This won't work as the code is not evaluated, and there is no variable
# named `2 * 2` in the `src` data
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
gluestick("Hello {2 * 2}", src = list(name = 'mike'), eval = FALSE)
```## Related Software
* Functionality is heavily modelled on [`glue`](https://cran.r-project.org/package=glue)
## Acknowledgements
* R Core for developing and maintaining the language.
* CRAN maintainers, for patiently shepherding packages onto CRAN and maintaining
the repository