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
#' @return ...

<<<<<<< HEAD
parameter_read <- function(
                           f,
                           path = "parameter_script/") {
  # File name should be a list
  parameter_df <- rep(list(NA, length(f)))

=======
parameter_read <- function (
  f,
  path = "parameter_script/"
) { 
  #f <- f2
  # File name should be a list
  parameter_df <- rep(list(NA), length(f))
  
>>>>>>> 381875eedd7472bfbaf2cfd62d780ca03ff9b601
  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, 2) == "# ") {
      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() %>%
<<<<<<< HEAD
      as.data.frame() %>%
      mutate(
        treatment = gsub("[/.a-z|.]+", "", f[i]) %>% substr(1, 3) %>% as.numeric(),
        replicate = gsub("[/.a-z]+|(\\d+-)|-", "", f[i]) %>% as.numeric()
      )
=======
      as.data.frame %>%
      mutate(treatment = unlist(strsplit(f[i], "_"))[1])
    #treatment = gsub("[/.a-z|.]+", "", f[i]) %>% substr(1,3) %>% as.numeric())
    #             replicate = gsub("[/.a-z]+|(\\d+-)|-", "", f[i]) %>% as.numeric())
>>>>>>> 381875eedd7472bfbaf2cfd62d780ca03ff9b601
  }

  # Merge the df
  parameter_df <- parameter_df %>% rbindlist()

  # Return a dataframe
  return(parameter_df)
}
Chang-Yu-Chang/MigrationCommunity documentation built on Aug. 13, 2019, 9:41 p.m.