hacker

Tricks and Convenience functions for GNU R
Log | Files | Refs

sub_preload.R (1516B)


      1 #  Copyright (C) 2020 Jan "JayVii"
      2 #  Author 2020 Jan "JayVii" <jayvii@posteo.de>
      3 #  SPDX-License-Identifier: GPL-3.0
      4 #  About this header: <https://reuse.software>
      5 
      6 # Apply regex-functions prior to loading a data set (.txt, .csv, ...)
      7 # A direct substitute for sub() and gsub() if data has not been loaded into RAM
      8 # yet.
      9 
     10 sub_preload <- function(pattern, replacement, file, backup = TRUE,
     11                         global = FALSE, extended = TRUE) {
     12 
     13     # Check supported OS
     14     supported_plattforms <- c("Linux")
     15     if (!(Sys.info()[["sysname"]] %in% supported_plattforms)) {
     16         stop("Sorry, your OS is currently not supported...")
     17     }
     18 
     19     # Check whether sed is installed
     20     sed_path <- system2(command = "whereis", args = "sed | awk '{ print $2 }'",
     21                         stdout = TRUE)
     22     if (nchar(sed_path) < 1) {
     23         stop("This function requires GNU sed, please install it on the host",
     24              " system and try again...")
     25     }
     26 
     27     # initialize additional parameters (empty characters)
     28     gl <- ipost <- eext <- ""
     29 
     30     # Construct sed script command
     31     if (global) gl <- "g"
     32     script <- paste0("s/", pattern, "/", replacement, "/", gl)
     33 
     34     # Construct additional parameters
     35     if (backup) ipost <- "=.BAK"
     36     if (extended) eext <- "-E"
     37     args <- paste0("--in-place", ipost, " ", eext)
     38 
     39     # construct script sequence
     40     sseq <- paste0("-e ", "'", script, "'")
     41 
     42     # run sed
     43     system2(command = "sed", args = paste(args, sseq, file), stdout = "")
     44 }
     45 
     46 # EOF sub_preload.R