Skip to content

Commit

Permalink
Fix 🐛 in read_balance()
Browse files Browse the repository at this point in the history
  • Loading branch information
mrustl committed Jun 28, 2024
1 parent 610400e commit 7f5fb24
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ importFrom(readr,read_delim)
importFrom(readr,read_fwf)
importFrom(readr,read_table)
importFrom(rlang,.data)
importFrom(stats,median)
importFrom(stats,setNames)
importFrom(stringr,str_detect)
importFrom(stringr,str_extract)
Expand Down
22 changes: 16 additions & 6 deletions R/read_balance.R
Original file line number Diff line number Diff line change
Expand Up @@ -48,17 +48,27 @@ bal_id_start <- grep("Length", block_sel_txt)
bal_id_end <- length(block_sel_txt)


balance <- lapply(block_sel_txt[bal_id_start:bal_id_end], function(x) {
stringr::str_extract_all(x, "\\d+?\\.\\d+")[[1]] %>% as.double() %>% t() %>%
tibble::as_tibble()
}) %>%
dplyr::bind_rows()
# Funktion zur Extraktion der Fließkommazahlen
extract_floats <- function(text) {
as.numeric(unlist(regmatches(text, gregexpr("-?\\d*\\.\\d+(?:E[+-]?\\d+)?", text))))
}


# Anwenden der Funktion auf den gesamten Text
float_numbers_list <- lapply(block_sel_txt[bal_id_start:bal_id_end], extract_floats)

# Erstellen eines DataFrames mit drei Spalten
balance <- do.call(rbind, lapply(float_numbers_list, function(x) {
length(x) <- 3 # Setze die Länge auf 3, um sicherzustellen, dass alle Einträge drei Spalten haben
return(x)
})) %>%
tibble::as_tibble()

names(balance) <- sprintf("id_%d", subregion_ids)


parvals <- block_sel_txt[bal_id_start:bal_id_end] %>%
stringr::str_remove_all("-?\\d+?\\.\\d+E\\+?-?\\d\\d") %>%
stringr::str_remove_all("-?\\d*\\.\\d+(?:E[+-]?\\d+)?") %>%
stringr::str_trim()

parvals_df <- parvals %>%
Expand Down
3 changes: 2 additions & 1 deletion R/read_profile.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#' @return tibble with PROFILE.out data
#' @export
#' @importFrom stringr str_replace
#' @importFrom stats median
read_profile <- function(path) {

lines <- readLines(path)
Expand All @@ -26,7 +27,7 @@ read_profile <- function(path) {
stringr::str_split(" ", simplify = TRUE) %>%
as.vector() %>% tolower())

header_clean <- if(median(ncols) > length(header_names_file)) {
header_clean <- if(stats::median(ncols) > length(header_names_file)) {
string_conc <- sprintf("conc%d", seq_len(median(ncols) - length(header_names_file))+1)

c(stringr::str_replace(header_names_file, "conc", "conc1"),
Expand Down

0 comments on commit 7f5fb24

Please sign in to comment.