R Under development (unstable) (2023-11-16 r85542 ucrt) -- "Unsuffered Consequences" Copyright (C) 2023 The R Foundation for Statistical Computing Platform: x86_64-w64-mingw32/x64 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.8.2 (2022-06-13 22:00:14 UTC) successfully loaded. See ?R.methodsS3 for help. R.oo v1.25.0 (2022-06-12 02:20:02 UTC) successfully loaded. See ?R.oo for help. Attaching package: 'R.oo' The following object is masked from 'package:R.methodsS3': throw The following objects are masked from 'package:methods': getClasses, getMethods The following objects are masked from 'package:base': attach, detach, load, save R.utils v2.12.3 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, isOpen, nullfile, parse, warnings > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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} > > # Printing > print(GString("Hello ${who}\n")) [1] "Hello world\n" > > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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") > > cat(as.character(GString("(x,y)=(${x},$[toupper]{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 CRAN on host CRANWIN3 running R v4.4.0 in process #16680 on windows. R is installed in D:/RCompile/recent/R.> > > # Other built-in variables/functions... > cat(as.character(GString("Current date: ${date}\n"))) Current date: 2023-11-17 > 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: 08:48:02 > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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: Fri Nov 17 08:48:02 2023 > > > # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - > # 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: 2023.11.17 > # 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: m > cat(as.character(GString("Another letter: ${aletter}\n"))) Another letter: b > > > # Another example > setMethodS3("getBuiltinGstring", "GString", function(object, ...) { + # Return another GString. + GString("${date} ${time}") + }) > > if (FALSE) { + 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") + + } # if (FALSE) > > proc.time() user system elapsed 0.32 0.12 0.37