| dsb {fixest} | R Documentation |
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, "!").
dsb(x, collapse = NULL)
x |
A character string, must be of length 1. Every expression inside |
collapse |
If the variables inserted into the string are of length greater than 1, you can merge into a single string with Every expression inside |
A character vector. It is of length > 1 only if the variables inserted are of length > 1.
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, " "].
Laurent Berge
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?")