Last active
March 31, 2023 05:25
-
-
Save bonifazi/07ee12c771550565868f04be2c94a904 to your computer and use it in GitHub Desktop.
Rscript for command line execution and --flagged arguments checks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env Rscript | |
# | |
# Purpose: what this script does. | |
# Author: Renzo Bonifazi - dd-mm-yyyy | |
# Usage: | |
# # Rscript --vanilla myscript.R --file my_path/myfile.csv --output myoutput.csv | |
# | |
# Example: | |
# # Rscript --vanilla Make_pedigree.R --file Renzo/Documents/file.csv --output Output.csv | |
# | |
# Details: | |
# | |
# Versions: | |
# * 0.0 - initial script. | |
# | |
# Notes: | |
# | |
################################################################################### | |
Sys.time() # print starting time | |
# 0. Load packages -------- | |
if (!require("pacman")) {install.packages("pacman", quiet = T)} | |
pacman::p_load(optparse, dplyr, tidyr, lubridate, data.table, dtplyr) | |
# [test usage only] pacman::p_load(tidylog) | |
# 1. Read cmd line arguments --------------------------------------------------------------- | |
# args parser # https://www.r-bloggers.com/2015/09/passing-arguments-to-an-r-script-from-command-lines/ | |
option_list <- list( | |
make_option(c("-f", "--file"), | |
type = "character", default = NULL, | |
help = "Input file name", metavar = "character" | |
), | |
make_option(c("-o", "--output"), | |
type = "character", default = NULL, | |
help = "Output file name", metavar = "character" | |
) | |
) | |
opt_parser <- OptionParser(option_list = option_list) # process list of args above | |
opt <- parse_args(opt_parser) # make args accessible in an object | |
if (is.null(opt$file) | is.null(opt$output)) { | |
print_help(opt_parser) | |
stop(cat("Input and output file name must be supplied! \n Current path is:", getwd(), "\n"), call. = FALSE) | |
} | |
# 2. Vars and set working directory -------- | |
# ___ 2.1 Set variables and wd | |
infile <- opt$file | |
outfile <- opt$output | |
setwd(dirname(infile)) # set wd to input filename | |
# [test usage only] infile <- "myfile.csv" | |
# [test usage only] outfile <- "outfile.csv" | |
# [test usage only] setwd("C:/Users/Documents/mycodes/") | |
# ___ 2.2 Fixed vars | |
col_names <- c("col1", "col2", "col3", "col4") | |
# 3. Read files -------- | |
file_proc <- fread(infile, | |
sep = ";", header = F, stringsAsFactors = F, | |
strip.white = T, col.names = col_names, | |
colClasses = list(character = c(1:2), integer64 = c(3:4)) # first 2 cols as character, last 2 as integer64 (=large integer) | |
) | |
# 4. Edit files or any other operations -------- | |
file_edited <- lazy_dt(file_proc) %>% # trigger R to use dtplyr for faster execution | |
# [test usage only] file_proc %>% as.data.frame %>% | |
your_lines_of_dplyr %>% | |
as.data.frame() # convert back to readable data.frame | |
# 5. Checks ---------------------------------------- | |
# 6. Save output ------ | |
fwrite(x = file_edited, file = outfile, quote = F, row.names = F, append = F, sep = ",") | |
Sys.time() # print ending time |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment