First attempt at script-run-workflow
This commit is contained in:
49
workflow/templates/qhtcp/REMc/CompileGTF.R
Normal file
49
workflow/templates/qhtcp/REMc/CompileGTF.R
Normal file
@@ -0,0 +1,49 @@
|
||||
# Load the openxlsx package
|
||||
library(openxlsx)
|
||||
|
||||
# Function to combine CSV and TXT files into a workbook with named sheets
|
||||
combineFilesToWorkbook <- function(file_list, output_file) {
|
||||
# Create a new workbook
|
||||
wb <- createWorkbook()
|
||||
|
||||
# Loop through the file list
|
||||
for (i in seq_along(file_list)) {
|
||||
file <- file_list[i]
|
||||
|
||||
# Extract the sheet name from the file name based on the extension
|
||||
ext <- tools::file_ext(file)
|
||||
sheet_name <- tools::file_path_sans_ext(basename(file))
|
||||
|
||||
# Read the data based on the file extension
|
||||
if (ext %in% c("csv", "txt")) {
|
||||
if (ext == "csv") {
|
||||
data <- read.csv(file)
|
||||
} else if (ext == "txt") {
|
||||
data <- readLines(file)
|
||||
}
|
||||
|
||||
# Create a new sheet in the workbook with the extracted sheet name
|
||||
addWorksheet(wb, sheetName = sheet_name)
|
||||
|
||||
# Write the data to the sheet
|
||||
writeData(wb, sheet = sheet_name, x = data)
|
||||
}
|
||||
}
|
||||
|
||||
# Save the workbook as an Excel file
|
||||
saveWorkbook(wb, output_file)
|
||||
}
|
||||
|
||||
currentDir <- getwd()
|
||||
Component <- file.path(currentDir, "GTF", "Component", "ComponentResults.txt")
|
||||
Function <- file.path(currentDir, "GTF", "Function", "FuctionResults.txt")
|
||||
Process <- file.path(currentDir, "GTF", "Process", "ProcessResults.txt")
|
||||
|
||||
# Specify the list of input files (both CSV and TXT)
|
||||
file_list <- c(Component,Process,Function)
|
||||
|
||||
# Specify the output file name
|
||||
output_file <- output_file <- file.path(currentDir, "GTF", "GTFCombined.xlsx")
|
||||
# Call the function to combine the files into a workbook with named sheets
|
||||
combineFilesToWorkbook(file_list, output_file)
|
||||
|
||||
Reference in New Issue
Block a user