Skip to content

Commit f4d0c1f

Browse files
committed
resturctered R code into backend, server and ui
1 parent 605c0c7 commit f4d0c1f

40 files changed

Lines changed: 675 additions & 657 deletions
File renamed without changes.
Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
identify_seperator <- function(path) {
1+
env_import_V1_2 <- new.env(parent = getNamespace("OpenStats"))
2+
3+
env_import_V1_2$identify_seperator <- function(path) {
24
line <- readLines(path, n = 1)
35
if(grepl(";", line)) return(";")
46
if(grepl("\t", line)) return("\t")
57
if(grepl(",", line)) return(",")
68
stop("Could not identify the separator. Please upload a file with a known separator.")
79
}
810

9-
trim_outer_quotes <- function(v) {
11+
env_import_V1_2$trim_outer_quotes <- function(v) {
1012
sapply(v, function(x) {
1113
if (x == "") return("")
1214
sub('^"(.*)"$', '\\1', x)
1315
})
1416
}
15-
cast_types_cols <- function(df, excel = FALSE) {
17+
env_import_V1_2$cast_types_cols <- function(df, excel = FALSE) {
1618
f <- function(x) {
1719
options(warn = -1)
1820
x <- as.numeric(x)
@@ -25,18 +27,18 @@ cast_types_cols <- function(df, excel = FALSE) {
2527
if (a) {
2628
return(as.numeric(b))
2729
}
28-
if (!excel) b <- trim_outer_quotes(b)
30+
if (!excel) b <- env_import_V1_2$trim_outer_quotes(b)
2931
return(as.factor(b))
3032
}
3133
df <- Map(conv, check, df)
3234
data.frame(df)
3335
}
3436

35-
is_separator <- function(c) {
37+
env_import_V1_2$is_separator <- function(c) {
3638
all(is.na(c))
3739
}
3840

39-
scan_rows_or_cols <- function(df, rows = TRUE) {
41+
env_import_V1_2$scan_rows_or_cols <- function(df, rows = TRUE) {
4042
dim_fct <- nrow
4143
if (!rows) dim_fct <- ncol
4244

@@ -50,7 +52,7 @@ scan_rows_or_cols <- function(df, rows = TRUE) {
5052
find_start <- function(df, offset) {
5153
indices <- offset:dim_fct(df)
5254
for (i in seq_along(indices)) {
53-
if (!is_separator(getter(df, indices[i]))) return(indices[i])
55+
if (!env_import_V1_2$is_separator(getter(df, indices[i]))) return(indices[i])
5456
}
5557
stop("Did not found any start col")
5658
}
@@ -59,7 +61,7 @@ scan_rows_or_cols <- function(df, rows = TRUE) {
5961
end <- start
6062
indices <- start:dim_fct(df)
6163
for (i in seq_along(indices)) {
62-
if (!is_separator(getter(df, indices[i]))) {
64+
if (!env_import_V1_2$is_separator(getter(df, indices[i]))) {
6365
end <- end + 1
6466
} else {
6567
break
@@ -83,51 +85,51 @@ scan_rows_or_cols <- function(df, rows = TRUE) {
8385
find_indices(df)
8486
}
8587

86-
find_sub_tables <- function(rows, cols, df) {
88+
env_import_V1_2$find_sub_tables <- function(rows, cols, df) {
8789
if (length(cols) > 1) return(TRUE)
8890
if (length(rows) > 1) return(TRUE)
8991
dims <- c(rows[[1]]$end, cols[[1]]$end)
9092
!all(dims == dim(df))
9193
}
9294

93-
extract_tables <- function(env_tables, df, excel = FALSE) {
94-
cols <- scan_rows_or_cols(df, FALSE)
95+
env_import_V1_2$extract_tables <- function(env_tables, df, excel = FALSE) {
96+
cols <- env_import_V1_2$scan_rows_or_cols(df, FALSE)
9597
tables <- list()
9698
for (cs in seq_along(cols)) {
9799
temp <- df[, cols[[cs]]$start:cols[[cs]]$end]
98-
rows <- scan_rows_or_cols(temp, TRUE)
100+
rows <- env_import_V1_2$scan_rows_or_cols(temp, TRUE)
99101
tables <- c(tables, lapply(rows, function(rs) {
100102
temp[rs$start:rs$end, ]
101103
}))
102104
}
103-
if (!find_sub_tables(rows, cols, df)) {
105+
if (!env_import_V1_2$find_sub_tables(rows, cols, df)) {
104106
env_tables$tables <- c(env_tables$tables, tables)
105107
return()
106108
}
107109
tables <- lapply(tables, \(x) {
108-
extract_tables(env_tables, x, excel)
110+
env_import_V1_2$extract_tables(env_tables, x, excel)
109111
})
110112
}
111113

112-
convert_to_dfs <- function(tables, excel = FALSE) {
114+
env_import_V1_2$convert_to_dfs <- function(tables, excel = FALSE) {
113115
tables <- lapply(tables, function(x) {
114116
if (nrow(x) > 2) {
115117
temp <- x[2:nrow(x), ]
116118
if (excel) {
117119
names(temp) <- sapply(x[1, ], make.names)
118120
} else {
119-
names(temp) <- sapply(trim_outer_quotes(x[1, ]), make.names)
121+
names(temp) <- sapply(env_import_V1_2$trim_outer_quotes(x[1, ]), make.names)
120122
}
121123
x <- temp
122124
}
123125
x
124126
})
125127
lapply(tables, function(x) {
126-
cast_types_cols(x, excel)
128+
env_import_V1_2$cast_types_cols(x, excel)
127129
})
128130
}
129131

130-
read_data_excel <- function(path) {
132+
env_import_V1_2$read_data_excel <- function(path) {
131133
sheets <- readxl::excel_sheets(path)
132134
res <- list()
133135
for (s in sheets) {
@@ -139,15 +141,15 @@ read_data_excel <- function(path) {
139141
tables <- as.data.frame(tables)
140142
env_tables <- new.env(parent = emptyenv())
141143
env_tables$tables <- list()
142-
extract_tables(env_tables, tables, TRUE)
143-
tables <- convert_to_dfs(env_tables$tables, TRUE)
144+
env_import_V1_2$extract_tables(env_tables, tables, TRUE)
145+
tables <- env_import_V1_2$convert_to_dfs(env_tables$tables, TRUE)
144146
res <- c(res, tables)
145147
}
146148
res
147149
}
148150

149-
read_raw <- function(path) {
150-
sep <- identify_seperator(path)
151+
env_import_V1_2$read_raw <- function(path) {
152+
sep <- env_import_V1_2$identify_seperator(path)
151153
raw_content <- readLines(path)
152154
raw_content <- lapply(raw_content, function(r){
153155
r <- strsplit(r, split = sep, fixed = TRUE)[[1]]
@@ -163,25 +165,25 @@ read_raw <- function(path) {
163165
as.data.frame(raw_df, stringsAsFactors = FALSE)
164166
}
165167

166-
read_data_csv <- function(path) {
167-
tables <- read_raw(path)
168+
env_import_V1_2$read_data_csv <- function(path) {
169+
tables <- env_import_V1_2$read_raw(path)
168170
env_tables <- new.env(parent = emptyenv())
169171
env_tables$tables <- list()
170-
extract_tables(env_tables, tables, FALSE)
171-
convert_to_dfs(env_tables$tables, FALSE)
172+
env_import_V1_2$extract_tables(env_tables, tables, FALSE)
173+
env_import_V1_2$convert_to_dfs(env_tables$tables, FALSE)
172174
}
173175

174-
readData <- function(path, DataModelState, ResultsState) {
176+
env_import_V1_2$read_data <- function(path, DataModelState, ResultsState) {
175177
stopifnot(is.character(path))
176178
if (!file.exists(path)) stop("File does not exists")
177179
max_file_size <- 50 * 1024^2 # 50 MB in bytes
178180
file_size <- file.info(path)$size
179181
if (is.na(file_size) || file_size > max_file_size) {
180182
stop("File size exceeds the 50 MB limit. Please upload a smaller file.")
181183
}
182-
tables <- try(read_data_excel(path), silent = TRUE)
184+
tables <- try(env_import_V1_2$read_data_excel(path), silent = TRUE)
183185
if (inherits(tables, "try-error")) {
184-
tables <- try(read_data_csv(path))
186+
tables <- try(env_import_V1_2$read_data_csv(path))
185187
if (inherits(tables, "try-error")) {
186188
stop(tables)
187189
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)