Go to TogaWare.com Home Page. Data Science Desktop Survival Guide
by Graham Williams
Duck Duck Go



CLICK HERE TO VISIT THE UPDATED SURVIVAL GUIDE

Glue Strings Together

20180729 The glue package provides a mechanism for building output strings from a collection of strings and variables. The basic use of glue::glue() will concatenate its string arguments with variable substitution identified using curly braces. In this exmaple we use the rattle::weatherAUS and format large numbers using scales::comma().
dsname <- "weatherAUS"
nobs   <- nrow(weatherAUS)
starts <- min(weatherAUS$Date)
glue("The {dsname} dataset",
     " has just less than {comma(nobs + 1)} observations,",
     " starting from {format(starts, '%-d %B %Y')}.")
## The weatherAUS dataset has just less than 176,748 observations, starting...

We can manually wrap the sentence.

glue("
     The {dsname} dataset has just
     less than {comma(nobs + 1)} observations
     starting from {format(starts, '%-d %B %Y')}.
     "
)
## The weatherAUS dataset has just
## less than 176,748 observations
## starting from 1 November 2007.

Notice how the initial and last empty lines are handled “as expected”, and the line split is maintained. Named arguments within the function call can be used to assign values to variables that only exist in the scope of the function call.

glue("
     The {dsname} dataset has just
         less than {comma(nobs + 1)} observations
     starting from {format(starts, '%-d %B %Y')}.
     "
,
     dsname = "weather",
     nobs   = nrow(weather),
     starts = min(weather$Date))
## The weather dataset has just
##     less than 367 observations
## starting from 1 November 2007.

We can also see the effect of indenting lines in this example, where the indentation is retained.


Support further development by purchasing the PDF version of the book.
Other online resources include the GNU/Linux Desktop Survival Guide.
Books available on Amazon include Data Mining with Rattle and Essentials of Data Science.
Popular open source software includes rattle and wajig.
Hosted by Togaware, a pioneer of free and open source software since 1984.
Copyright © 2000-2020 Togaware Pty Ltd. . Creative Commons ShareAlike V4.