forked from olejandro/times-ts-tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read.R
103 lines (94 loc) · 3.08 KB
/
read.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# Copyright 2019 Energy Modelling Lab ApS
# Copyright 2020 Olexandr Balyk
#
# This file is part of TIMES-TS-Tool.
#
# TIMES-TS-Tool is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# TIMES-TS-Tool is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with TIMES-TS-Tool. If not, see <https://www.gnu.org/licenses/>.
library(openxlsx)
library(tidyr)
library(dplyr)
filepath <- function(path, file_name_pattern){
exclude_pattern <- "~$"
n = nchar(path)
if (substr(path, n, n) == "/"){
sep <- ""
} else {
sep <- "/"
}
matching_files <- list.files(path=path,pattern=file_name_pattern)
file_name <- matching_files[!grepl(exclude_pattern,matching_files,fixed=TRUE)]
return(paste(path, file_name, sep = sep))
}
checkModelPath <- function(modelPath){
defaultOutputDir <- "output"
if (dir.exists(filepath(modelPath,"SysSettings"))){
return(modelPath)
} else {
if (!dir.exists(defaultOutputDir)){
dir.create(defaultOutputDir)
}
return(paste0(defaultOutputDir,"/"))
}
}
read_DMI <- function(path, file_name_pattern, column=NULL){
data <- read.csv(filepath(path,file_name_pattern), sep = ";")
if (is.null(column)) {
return(data[,])
} else {
return(as.data.frame(data[,column]))
}
}
read_V_FT <- function(path, file_name_pattern, sheet, from_row, colNames = TRUE, fill_columns) {
xlsxFile <- filepath(path,file_name_pattern)
df <- read.xlsx(xlsxFile, sheet = sheet, startRow = from_row, colNames = colNames)
if (missing(fill_columns)){
return(df)
} else {
return(fill(df,all_of(fill_columns)))
}
}
readXlRange <- function(xlFilePath, sheet=NULL, colNames=TRUE, rowNames=TRUE,
rows, cols, detectDates = TRUE) {
# If the sheet name is not NULL read that sheet
if (!is.null(sheet)) {
df <- readWorkbook(xlFilePath,
sheet = sheet,
colNames = colNames,
rowNames = rowNames,
rows = rows,
cols = cols,
detectDates = detectDates)
# Assign column name sheet name if there is only one column
if (length(colnames(df)) == 1) {
colnames(df) <- sheet
}
return(df)
} else {
sheets <- getSheetNames(xlFilePath)
for (aSheetName in sheets)
{
temp_df <- readXlRange(xlFilePath = xlFilePath,
sheet = aSheetName,
rows = rows,
cols = cols)
# Check the position of the sheet in the sheets list
if (match(aSheetName,sheets) == 1) {
df <- temp_df
} else {
df <- cbind(df, temp_df)
}
}
return(df)
}
}