Last data update: 2014.03.03
|
R: Character string with advanced substitutions
Character string with advanced substitutions
Description
Package: R.utils
Class GString
character
~~|
~~+-- GString
Directly known subclasses:
public static class GString
extends character
Usage
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.character | Gets the processed character string. |
| evaluate | Parses and evaluates a GString. |
| gcat | - |
| getBuiltinDate | Gets the current date. |
| getBuiltinDatetime | Gets the current date and time. |
| getBuiltinHostname | Gets the hostname of the system running R. |
| getBuiltinOs | Gets the operating system of the running machine. |
| getBuiltinPid | Gets the process id of the current R session. |
| getBuiltinRhome | Gets the path where R is installed. |
| getBuiltinRversion | Gets the current R version. |
| getBuiltinTime | Gets the current time. |
| getBuiltinUsername | Gets the username of the user running R. |
| getRaw | Gets the unprocessed GString. |
| getVariableValue | Gets a variable value given a name and attributes. |
| gstring | - |
| parse | Parses a GString. |
| print | Prints the processed GString. |
|
Methods inherited from character:
all.equal, as.Date, as.POSIXlt, as.data.frame, as.raster, downloadFile, formula, getDLLRegisteredRoutines, isOpen, toAsciiRegExprPattern, toFileListTree, uses
Author(s)
Henrik Bengtsson
See Also
For conveniency, see functions gstring () and gcat ().
Examples
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# First example
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
who <- "world"
# Compare this...
cat(as.character(GString("Hello ${who}\n")))
# ...to this.
cat(GString("Hello ${who}\n"))
# Escaping
cat(as.character(GString("Hello \${who}\n")))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Looping over vectors
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
x <- 1:5
y <- c("hello", "world")
cat(as.character(GString("(x,y)=(${x},${y})")), sep=", ")
cat("\n")
cat(as.character(GString("(x,y)=(${x},$[capitalize]{y})")), sep=", ")
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}.")))
# Other built-in variables/functions...
cat(as.character(GString("Current date: ${date}\n")))
cat(as.character(GString("Current date: $[format='%d/%m/%y']{date}\n")))
cat(as.character(GString("Current time: ${time}\n")))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Evaluating inline R code
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
cat(as.character(GString("Simple calculation: 1+1=${`1+1`}\n")))
cat(as.character(GString("Alternative current date: ${`date()`}\n")))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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")))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Global search-replace feature
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Replace all '-' with '.'
cat(as.character(GString("Current date: ${date/-/.}\n")))
# Another example
cat(as.character(GString("Escaped string: 12*12=${`12*12`/1/}\n")))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# 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")))
cat(as.character(GString("Another letter: ${aletter}\n")))
# Another example
setMethodS3("getBuiltinGstring", "GString", function(object, ...) {
# Return another GString.
GString("${date} ${time}")
})
cat(as.character(GString("Advanced example: ${gstring}\n")))
# 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")))
n <- 5
cat(as.character(GString("${n} random numbers: ")))
cat(as.character(GString("$[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: ")))
cat(as.character(GString("$[n=runif(n=1,min=1,max=5), format='f']{runif}")))
cat("\n")
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(R.utils)
Loading required package: R.oo
Loading required package: R.methodsS3
R.methodsS3 v1.7.1 (2016-02-15) successfully loaded. See ?R.methodsS3 for help.
R.oo v1.20.0 (2016-02-17) successfully loaded. See ?R.oo for help.
Attaching package: 'R.oo'
The following objects are masked from 'package:methods':
getClasses, getMethods
The following objects are masked from 'package:base':
attach, detach, gc, load, save
R.utils v2.3.0 (2016-04-13) successfully loaded. See ?R.utils for help.
Attaching package: 'R.utils'
The following object is masked from 'package:utils':
timestamp
The following objects are masked from 'package:base':
cat, commandArgs, getOption, inherits, isOpen, parse, warnings
> png(filename="/home/ddbj/snapshot/RGM3/R_CC/result/R.utils/GString-class.Rd_%03d_medium.png", width=480, height=480)
> ### Name: GString
> ### Title: Character string with advanced substitutions
> ### Aliases: GString
> ### Keywords: classes
>
> ### ** 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 ddbj on host rgm-mini running R v3.3.1 in process #5724 on unix. R is installed in /home/ddbj/local/lib64/R.>
>
> # Other built-in variables/functions...
> cat(as.character(GString("Current date: ${date}\n")))
Current date: 2016-07-04
> 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: 22:43:29
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> # 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: Mon Jul 4 22:43:29 2016
>
>
> # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
> # 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: 2016.07.04
> # 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: g
> cat(as.character(GString("Another letter: ${aletter}\n")))
Another letter: b
>
>
> # Another example
> setMethodS3("getBuiltinGstring", "GString", function(object, ...) {
+ # Return another GString.
+ GString("${date} ${time}")
+ })
>
> cat(as.character(GString("Advanced example: ${gstring}\n")))
Advanced example: 2016-07-04 22:43:29
>
>
> # 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")))
A random number: 0.1331
> n <- 5
> cat(as.character(GString("${n} random numbers: ")))
5 random numbers: Warning message:
closing unused connection 3 (/usr/bin/env uname -n)
> 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")
>
>
>
>
>
> dev.off()
null device
1
>
|