R/replace-token-function.R

Defines functions replace.token

Documented in replace.token

#' @title  To replace tokens in documents (e.g. calibration certificate templates) with a given string.
#' @description 
#' To replace tokens in in documents (e.g. calibration certificate templates) with valid input - typically generated by the software. 
#' The modified text is for Latex typesetting.
#' Sample call:
#' 
#' txt <- "jsjdksfkd  TOKEN.CERTDATE jsfhjhfjkhjfadsk safjsfkf"
#' 
#' txt <- replace.token(txt,"TOKEN.CERTDATE","December 24, 1999")
#' 
#' @usage  txt <- replace.token(txt,"TOKEN.CERTDATE","December 24, 1999")
#' @name replace.token 
#' @author Claus E. Andersen
#' @return text (typically an entire document with many lines).
#' @param TOKEN.ID (e.g. "TOKEN.XYZ").
#' @param Replace.auto (e.g. "").
#' @param Replace.manual as an alternative (set to NULL).
#' @export replace.token
replace.token <- function(txt,TOKEN.ID="TOKEN.XYZ",Replace.auto="",Replace.manual=NULL){
  # Created: June 1, 2017
  # Revised: June 1, 2017
  # Name   : Claus E. Andersen
  # Purpose: To replace tokens in calibration certificate templates
  #          with valid input - mainly generated by the software. The
  #          modified text is for Latex typesetting.
  # Sample call:
  # txt <- "jsjdksfkd  TOKEN.CERTDATE jsfhjhfjkhjfadsk safjsfkf"
  # txt <- replace.token(txt,"TOKEN.CERTDATE","December 24, 1999")
  #
  print(paste("1. replace.token: TOKEN.ID =",TOKEN.ID))
  print(paste("2. replace.token: Replace.auto =",Replace.auto))
  print(paste("3. replace.token: Replace.manual =",Replace.manual))
  
  i <- grep(TOKEN.ID,txt,value=FALSE)
  N <- length(i)
  if(is.null(Replace.manual)){
    txt <- gsub(TOKEN.ID,Replace.auto,txt)
  } else {
    txt <- gsub(TOKEN.ID,Replace.manual,txt)
  }
  print(paste(N," ",TOKEN.ID," was replaced in this number of lines."))
  txt
} # replace.token
claus-e-andersen/clanTools documentation built on Nov. 30, 2024, 11:06 p.m.