Package: R.utils
Class GString

character
~~|
~~+--GString

Directly known subclasses:

public static class GString
extends character

GString(..., sep="")

Arguments

...

one or more objects, to be coerced to character vectors.

sep

A character string to separate the terms.

Fields and Methods

Methods:

as.characterGets the processed character string.
evaluateParses and evaluates a GString.
gcat-
getBuiltinDateGets the current date.
getBuiltinDatetimeGets the current date and time.
getBuiltinHostnameGets the hostname of the system running R.
getBuiltinOsGets the operating system of the running machine.
getBuiltinPidGets the process id of the current R session.
getBuiltinRhomeGets the path where R is installed.
getBuiltinRversionGets the current R version.
getBuiltinTimeGets the current time.
getBuiltinUsernameGets the username of the user running R.
getRawGets the unprocessed GString.
getVariableValueGets a variable value given a name and attributes.
gstring-
parseParses a GString.
printPrints the processed GString.

Methods inherited from character:
Ops,nonStructure,vector-method, Ops,structure,vector-method, Ops,vector,nonStructure-method, Ops,vector,structure-method, all.equal, as.Date, as.POSIXlt, as.data.frame, as.raster, coerce,ANY,character-method, coerce,character,SuperClassMethod-method, coerce,character,signature-method, coerce<-,ObjectsWithPackage,character-method, coerce<-,signature,character-method, downloadFile, formula, getDLLRegisteredRoutines, glyphJust, isOpen, toAsciiRegExprPattern, toFileListTree, uses

Author

Henrik Bengtsson

See also

For convenience, see functions gstring() and gcat().

Examples

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# First example
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
who <- "world"

# Compare this...
cat(as.character(GString("Hello ${who}\n")))
#> Hello world

# ...to this.
cat(GString("Hello ${who}\n"))
#> Hello ${who}

# Escaping
cat(as.character(GString("Hello \\${who}\n")))
#> Hello ${who}


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Looping over vectors
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
x <- 1:5
y <- c("hello", "world")
cat(as.character(GString("(x,y)=(${x},${y})")), sep=", ")
#> (x,y)=(1,hello), (x,y)=(2,world), (x,y)=(3,hello), (x,y)=(4,world), (x,y)=(5,hello)
cat("\n")
#> 

cat(as.character(GString("(x,y)=(${x},$[capitalize]{y})")), sep=", ")
#> (x,y)=(1,Hello), (x,y)=(2,World), (x,y)=(3,Hello), (x,y)=(4,World), (x,y)=(5,Hello)
cat("\n")
#> 


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Predefined ("builtin") variables
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cat(as.character(GString("Hello ${username} on host ${hostname} running ",
"R v${rversion} in process #${pid} on ${os}. R is installed in ${rhome}.")))
#> Hello hb on host hb-x1-2023 running R v4.6.2 in process #1114437 on unix. R is installed in /home/hb/shared/software/CBI/_ubuntu24_04/R-4.6.2-gcc13/lib/R.


# Other built-in variables/functions...
cat(as.character(GString("Current date: ${date}\n")))
#> Current date: 2026-11-11
cat(as.character(GString("Current date: $[format='%d/%m/%y']{date}\n")))
#> Current date: $[format='%d/%m/%y']{date}
cat(as.character(GString("Current time: ${time}\n")))
#> Current time: 17:10:33


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Evaluating inline R code
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cat(as.character(GString("Simple calculation: 1+1=${`1+1`}\n")))
#> Simple calculation: 1+1=2
cat(as.character(GString("Alternative current date: ${`date()`}\n")))
#> Alternative current date: Wed Nov 11 17:10:33 2026


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Function values
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Call function rnorm with arguments n=1, i.e. rnorm(n=1)
cat(as.character(GString("Random normal number: $[n=1]{rnorm}\n")))
#> Random normal number: $[n=1]{rnorm}


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Global search-replace feature
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Replace all '-' with '.'
cat(as.character(GString("Current date: ${date/-/.}\n")))
#> Current date: 2026.11.11
# Another example
cat(as.character(GString("Escaped string: 12*12=${`12*12`/1/}\n")))
#> Escaped string: 12*12=44


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Defining new "builtin" function values
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Define your own builtin variables (functions)
setMethodS3("getBuiltinAletter", "GString", function(object, ...) {
  base::letters[runif(1, min=1, max=length(base::letters))]
})

cat(as.character(GString("A letter: ${aletter}\n")))
#> A letter: NA
cat(as.character(GString("Another letter: ${aletter}\n")))
#> Another letter: NA


# Another example
setMethodS3("getBuiltinGstring", "GString", function(object, ...) {
  # Return another GString.
  GString("${date} ${time}")
})

cat(as.character(GString("Advanced example: ${gstring}\n")))
#> Error in if (any(nchar(value) > 0L)) break: missing value where TRUE/FALSE needed


# Advanced example
setMethodS3("getBuiltinRunif", "GString", function(object, n=1, min=0, max=1, ...) {
  formatC(runif(n=n, min=min, max=max), ...)
})

cat(as.character(GString("A random number: ${runif}\n")))
#> Error in if (any(nchar(value) > 0L)) break: missing value where TRUE/FALSE needed
n <- 5
cat(as.character(GString("${n} random numbers: ")))
#> 5 random numbers: 
cat(as.character(GString("$[n=n, format='f']{runif}")))
#> $[n=n, format='f']{runif}
cat("\n")
#> 


# Advanced options.
# Options are parsed as if they are elements in a list, e.g.
#   list(n=runif(n=1,min=1,max=5), format='f')
cat(as.character(GString("$Random number of numbers: ")))
#> $Random number of numbers: 
cat(as.character(GString("$[n=runif(n=1,min=1,max=5), format='f']{runif}")))
#> $[n=runif(n=1,min=1,max=5), format='f']{runif}
cat("\n")
#>