Skip to content

Commit

Permalink
Added dec_sep in collate_*
Browse files Browse the repository at this point in the history
  • Loading branch information
carlopacioni committed Jul 10, 2020
1 parent b363538 commit a44e096
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 12 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
@@ -1,3 +1,7 @@
# vortexR 1.1.8
collate_* functions gain dec_sep argument to control decimal separator charcter in order to solve reading numerical data on European machines
Minor edits to help files

# vortexR 1.1.7
Ensured copatibility with R 4.*
fit_regression gains a new argument: `links`, which allows for a better control
Expand Down
17 changes: 12 additions & 5 deletions R/data_handling.R
Expand Up @@ -11,6 +11,7 @@
#' @param filename The fully qualified filename of a Vortex .dat or .stdat file
#' @param runs The number of simulation runs
#' @param verbose Progress messages, default: FALSE
#' @param dec_sep Decimal separator (default ".")
#' @return A data.frame with data from one .dat or .stdat file and
#' population/scenario names as factors
#' @import vortexRdata
Expand All @@ -21,7 +22,7 @@
#' pac.dir <- system.file('extdata', 'pacioni', package='vortexRdata')
#' f <- file.path(pac.dir, 'Pacioni_et_al_ST_Classic(Base).stdat')
#' one.st.classic <- collate_one_dat(f, 3)
collate_one_dat <- function(filename, runs, verbose = FALSE) {
collate_one_dat <- function(filename, runs, dec_sep=".", verbose = FALSE) {

if (verbose) message(cat("INFO vortexR::collate_one_dat parsing", filename))
lines <- readLines(filename)
Expand Down Expand Up @@ -59,6 +60,7 @@ collate_one_dat <- function(filename, runs, verbose = FALSE) {
sep = ";",
nrows = readFor,
skip = readAfter,
dec = dec_sep,
colClasses = "numeric",
comment.char = "")
colnames(tmp) <- h
Expand Down Expand Up @@ -114,12 +116,12 @@ collate_one_dat <- function(filename, runs, verbose = FALSE) {
#'
#' @param project The Vortex project name to be imported
#' @param scenario The scenario name if ST, default: NULL
#' @param runs The number of Vortex simulation runs
#' @param dir_in The local folder containing Vortex files, default: NULL. If
#' not specified, will fall back to use current working directory.
#' @param save2disk Whether to save the data as rda and csv, default: TRUE
#' @param dir_out The local path to store the output. Default: ProcessedData
#' @param verbose Progress messages, default: TRUE
#' @inheritParams collate_one_dat
#' @return a data.frame with data from all matching Vortex files or NULL
#' @import vortexRdata
#' @export
Expand Down Expand Up @@ -152,6 +154,7 @@ collate_dat <- function(project,
runs,
scenario = NULL,
dir_in = NULL,
dec_sep = ".",
save2disk = TRUE,
dir_out = "ProcessedData",
verbose = TRUE) {
Expand All @@ -176,7 +179,7 @@ collate_dat <- function(project,
if (verbose) message("vortexR::collate_dat is parsing:")
for (filename in files) {
if (verbose) message(filename, "\r")
d <- rbind(d, collate_one_dat(filename, runs))
d <- rbind(d, collate_one_dat(filename, runs, dec_sep = dec_sep))
}
if (save2disk) df2disk(d, dir_out, fname, "_data")
return(d)
Expand Down Expand Up @@ -219,6 +222,7 @@ collate_dat <- function(project,
collate_run <- function(project,
scenario,
npops = 1,
dec_sep = ".",
dir_in = NULL,
save2disk = TRUE,
dir_out = "ProcessedData",
Expand Down Expand Up @@ -250,7 +254,8 @@ collate_run <- function(project,
sep = ";",
skip = 3,
colClasses = "numeric",
comment.char = "")
comment.char = "",
dec=dec_sep)
colnames(trun) <- h

Scenario <- read.table(filename,
Expand Down Expand Up @@ -343,6 +348,7 @@ collate_run <- function(project,
collate_yr <- function(project,
scenario,
npops_noMeta = 1,
dec_sep = ".",
dir_in = NULL,
save2disk = TRUE,
dir_out = "ProcessedData",
Expand Down Expand Up @@ -391,7 +397,8 @@ collate_yr <- function(project,
n_rows,
iter_ln,
lines,
header)
header,
dec_sep = dec_sep)

censusData[[i]] <- rbindlist(one_yr)
}
Expand Down
7 changes: 5 additions & 2 deletions R/helper_fun.R
Expand Up @@ -111,10 +111,13 @@ PrefixAndRepeat <- function(chars, times = 1, prefix = "") {
#' @param iter_ln The number of rows to skip from the file
#' @param lines An object returned from readLines()
#' @param header A character vector of column names
#' @param dec_sep Decimal separator (default ".")
#' @return A data.frame
CompileIter <- function(iter, filename, n_rows, iter_ln, lines, header) {
CompileIter <- function(iter, filename, n_rows, iter_ln, lines, header,
dec_sep = ".") {
temp.df <- read.table(filename, header = FALSE, sep = ";", nrows = n_rows,
skip = iter_ln[iter], colClasses = "numeric",
skip = iter_ln[iter],
dec = dec_sep, colClasses = "numeric",
comment.char = "")
colnames(temp.df) <- header
Iteration <- rep(iter, length = length(temp.df$Year))
Expand Down
4 changes: 3 additions & 1 deletion man/CompileIter.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion man/collate_dat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion man/collate_one_dat.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/collate_run.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions man/collate_yr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/rRec.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vignettes/vortexRguide.Rmd
Expand Up @@ -360,7 +360,7 @@ You can access the help file for all functions typing ```?function_name```, wher

## Data handling

As for the vortexR output files, the input files in all ```collate``` functions are searched in the working directory unless an alternative path is provided with ```dir_in```. In all ```collate```, the argument ```verbose=TRUE``` will result in R printing on the screen the number and progress of files found/read.
As for the vortexR output files, the input files in all ```collate``` functions are searched in the working directory unless an alternative path is provided with ```dir_in```. In all ```collate```, the argument ```verbose=TRUE``` will result in R printing on the screen the number and progress of files found/read. By default, the decimal separator character is ".". In cases where the operating system uses a different separator (e.g. most of European countries use ","), this can be changed with the argument ```dec_sep``` (e.g. ```dec_sep=","```). This is omitted in all examples below for brevity, but is present in all ```collate``` functions.

### *collate_one_dat & collate_dat*
These functions collate one or all the .dat or .stdat files within a folder. ```collate_one_dat``` takes only two arguments: the name of the file (```filename```) and the number of runs or iterations (```runs```) that were conducted, while ```collate_dat``` has three arguments (in addition to the standard ```dir_in```, ```dir_out```, ```verbose```, and ```save2disk```): the name of the project (```project```), the name of the scenario (```scenario```) and the number of runs or iterations (```runs```) that were conducted. There is no need to pass the number of populations or their names as vortexR will automatically detect these information from the files.
Expand Down

0 comments on commit a44e096

Please sign in to comment.