R/parameter_read.R

Defines functions parameter_read

Documented in parameter_read

#' Read the parameters saved in the result text file
#' @description Read the parameters from the files that save the simulated result and turn them into a dataframe
#' @name parameter_write
#' @param f the vector of file names to be read
#' @param path directory of the files
#' @examples
#' 
#' @export


parameter_read <- function (
  f,
  path = "data/"
) { 
  # File name should be a list
  parameter_df <- rep(list(NA, length(f)))
  
  for (i in 1:length(f)) {
    
    # Read the first few lines of comment
    l <- readLines(paste0(path, f[i]), 1, 1)
    
    counter <- 1
    while (tail(l, 1) %>% substr(1, 1) == "#") {
      counter <- counter + 1
      l <- readLines(paste0(path, "/", f[i]), counter)
    }
    l <- l[-counter]
    
    # Parameter list
    parameter_df[[i]] <- 
      setNames(sapply(strsplit(l, " "), function(x) `[`(x, 4)),
               sapply(strsplit(l, " "), function(x) `[`(x, 2))) %>%
      t() %>%
      as.data.frame %>%
      mutate(treatment = gsub("[/.a-z|.]+", "", f[i]) %>% substr(1,3) %>% as.numeric(),
             replicate = gsub("[/.a-z]+|(\\d+-)|-", "", f[i]) %>% as.numeric())
  }
  
  # Merge the df
  parameter_df <- parameter_df %>% rbindlist()
  
  # Return a dataframe
  return(parameter_df)
}
Chang-Yu-Chang/MigrationCommunity documentation built on April 22, 2019, 7:36 p.m.