R: Extends strings with variables using the Dot Square Bracket...

dsb {fixest} | R Documentation

## Extends strings with variables using the Dot Square Bracket operator

### Description

Simple utility to insert variables into character strings using the "dot square bracket" operator. Typically `dsb("Hello I'm .[x]!")` is equivalent to `paste0("hello I'm ", x, "!")`.

### Usage

    dsb(x, collapse = NULL)

### Arguments

`x` | A character string, must be of length 1. Every expression inside `.[]` is evaluated in the current frame and then coerced to character and inserted into the character string. For example: `dsb("hello .[name]")` is equivalent to `paste0("hello ", name)`. You can add a string literal as first or last element in the `.[]`. Doing so will collapse the expression. If first, as in `.['text', expr]`, the expression is first collapsed: `paste0(expr, collapse = "text")` is applied. If last, as in `"before.[expr, 'text']"`, the expression is collapsed to the previous adjacent text: `paste0("before", expr, collapse = "text")` is applied. The collapsing is always done with the previous text only. To collapse the whole string, use the argument 'collapse'.
`collapse` | If the variables inserted into the string are of length greater than 1, you can merge into a single string with `collapse`. Every expression inside `.[]` is evaluated in the current frame and then coerced to character and inserted into the character string.

### Value

A character vector. It is of length > 1 only if the variables inserted are of length > 1.

### Collapsing

If the expression inside `.[]` is a vector, a vector will be returned, except when we explicitly request the character string to be "collapsed". There are three main ways to do the collapsing that we detail below. Throughout, consider that the variable `name` is equal to `name = c("Romeo", "Juliet")` and the example `dsb("hello .[name], what's up?")`.

- full collapse:The argument `collapse` is used. All the string elements are attached. For example `dsb("hello .[name], what's up?", collapse = " And... ")` leads to `"hello Romeo, what's up? And... hello Juliet, what's up?"`.

- expression-collapse:There is a string literal in the first position of `.[]`. In that case the expression in brackets is first collapsed before being merged. For example in `dsb("hello .[' and ', name], what's up?")` leads to `"hello Romeo and Juliet, what's up?"`. If you add a comma but omit the string literal, the default is to collapse with a space: `.[,expr]` is equivalent to `.[" ", expr]`.

- text-expression-collapse:There is a string literal in the second position of `.[]`. In that case the expression in brackets is collapsed to the adjacent string on the left. For example in `dsb("hello .[name, ' and '], what's up?")` leads to `"hello Romeo and hello Juliet, what's up?"`. If you add a comma but omit the string literal, the default is to collapse with a space: `.[expr,]` is equivalent to `.[expr, " "]`.

### Author(s)

Laurent Berge

### Examples

    guy_all = c("Jenny", "Bryan")
    loc_all = c("kitchen", "bathroom")

    guy = sample(guy_all, 1)
    loc = sample(loc_all, 1)

    dsb("Where is .[guy]? .[guy] is in the .[loc].")

    # Since the stuff in brackets is evaluated in the current frame,
    # you can do things like:

    guy_gen = function() sample(guy_all, 1)
    loc_gen = function() sample(loc_all, 1)

    dsb("Where is .[g <- guy_gen()]? .[g] is in the .[loc_gen()].")

    #
    # Collapsing options
    #

    name = c("Romeo", "Juliet")

    # full collapse with argument collapse
    dsb("hello .[name], what's up?", collapse = " and ")

    # collapse the expression by adding a string literal
    # in the *first* position of .[]
    dsb("hello .[' and ', name], what's up?")

    # collapsing the epression with the previous string
    # using a string literal in the *second* position
    dsb("hello .[name, ' and '], what's up?")

    # The elements can also be empty, leading to collapse with a " "
    dsb("hello .[, name], what's up?")
    dsb("hello .[name, ], what's up?")

[Package fixest version 0.10.1 Index]