mikeblazanin / gcplyr

gcplyr is an R package that facilitates wrangling and analysis of microbial growth curve data
https://mikeblazanin.github.io/gcplyr/
Other
30 stars 2 forks source link

Run Regex-functions on the Vector #139

Open discoleo opened 1 year ago

discoleo commented 1 year ago

Regular Expressions

Run functions using regular expressions on the entire vector, not on individual values. The Regex code can be compiled at runtime and run much faster. It is also a better design.

Existing Code

# - comments are also ugly formatted; else { #block_names were not provided, infer from filename

infer the names from filenames, stripping off the extension from end

  # and the dot at the beginning (if any)
  outputs[[i]]$metadata["block_name"] <- 
    sub("^\\.?/?(.*)\\.[[:alnum:]]+$", "\\1", files[i])
}

Modified Design

# - move code outside the for-loop; # - can be also inside a new utility function; if(is.null(block_names)) { # Why not save the results directly in block_names? # Nice comments as well: # Block_names were not provided: # - infer the names from filenames; # - strip off the extension from end # and the dot at the beginning (if any); block_names = sub("^\.?/?(.*)\.[[:alnum:]]+$", "\1", files); }

You can use now block_names in the for-loop directly, without checking for NULL anymore.

Note: