GString               package:R.utils               R Documentation

_C_h_a_r_a_c_t_e_r _s_t_r_i_n_g _w_i_t_h _a_d_v_a_n_c_e_d _s_u_b_s_t_i_t_u_t_i_o_n_s

_D_e_s_c_r_i_p_t_i_o_n:

     Package:  R.utils 
      *Class GString*

     'character'
      '~~|'
      '~~+--''GString'

     *Directly known subclasses:*


     public static class *GString*
      extends character

_U_s_a_g_e:

     GString(..., sep="")

_A_r_g_u_m_e_n_t_s:

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

     sep: A 'character' string to separate the terms.

_F_i_e_l_d_s _a_n_d _M_e_t_h_o_d_s:

     *Methods:*

         'as.character'        Gets the processed character string.
         '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.
         'print'               Prints the processed GString.

     *Methods inherited from character*:
      all.equal, as.data.frame, as.Date, getDLLRegisteredRoutines

_A_u_t_h_o_r(_s):

     Henrik Bengtsson (<URL: http://www.braju.com/R/>)

_E_x_a_m_p_l_e_s:

     # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     # 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")

