diff --git a/NEWS.md b/NEWS.md index 642eccc..c094d66 100644 --- a/NEWS.md +++ b/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 diff --git a/R/data_handling.R b/R/data_handling.R index 1c45885..9fcd701 100644 --- a/R/data_handling.R +++ b/R/data_handling.R @@ -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 @@ -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) @@ -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 @@ -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 @@ -152,6 +154,7 @@ collate_dat <- function(project, runs, scenario = NULL, dir_in = NULL, + dec_sep = ".", save2disk = TRUE, dir_out = "ProcessedData", verbose = TRUE) { @@ -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) @@ -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", @@ -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, @@ -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", @@ -391,7 +397,8 @@ collate_yr <- function(project, n_rows, iter_ln, lines, - header) + header, + dec_sep = dec_sep) censusData[[i]] <- rbindlist(one_yr) } diff --git a/R/helper_fun.R b/R/helper_fun.R index 40dec01..64aa7f6 100644 --- a/R/helper_fun.R +++ b/R/helper_fun.R @@ -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)) diff --git a/man/CompileIter.Rd b/man/CompileIter.Rd index 9386496..e685e39 100644 --- a/man/CompileIter.Rd +++ b/man/CompileIter.Rd @@ -4,7 +4,7 @@ \alias{CompileIter} \title{Compile iterations from one .yr file} \usage{ -CompileIter(iter, filename, n_rows, iter_ln, lines, header) +CompileIter(iter, filename, n_rows, iter_ln, lines, header, dec_sep = ".") } \arguments{ \item{iter}{The iteration (run) number} @@ -18,6 +18,8 @@ CompileIter(iter, filename, n_rows, iter_ln, lines, header) \item{lines}{An object returned from readLines()} \item{header}{A character vector of column names} + +\item{dec_sep}{Decimal separator (default ".")} } \value{ A data.frame diff --git a/man/collate_dat.Rd b/man/collate_dat.Rd index d04e15e..f939f09 100644 --- a/man/collate_dat.Rd +++ b/man/collate_dat.Rd @@ -9,6 +9,7 @@ collate_dat( runs, scenario = NULL, dir_in = NULL, + dec_sep = ".", save2disk = TRUE, dir_out = "ProcessedData", verbose = TRUE @@ -17,13 +18,15 @@ collate_dat( \arguments{ \item{project}{The Vortex project name to be imported} -\item{runs}{The number of Vortex simulation runs} +\item{runs}{The number of simulation runs} \item{scenario}{The scenario name if ST, default: NULL} \item{dir_in}{The local folder containing Vortex files, default: NULL. If not specified, will fall back to use current working directory.} +\item{dec_sep}{Decimal separator (default ".")} + \item{save2disk}{Whether to save the data as rda and csv, default: TRUE} \item{dir_out}{The local path to store the output. Default: ProcessedData} diff --git a/man/collate_one_dat.Rd b/man/collate_one_dat.Rd index ab347ed..a23c1ac 100644 --- a/man/collate_one_dat.Rd +++ b/man/collate_one_dat.Rd @@ -4,13 +4,15 @@ \alias{collate_one_dat} \title{Collate one local Vortex output file into a data.frame} \usage{ -collate_one_dat(filename, runs, verbose = FALSE) +collate_one_dat(filename, runs, dec_sep = ".", verbose = FALSE) } \arguments{ \item{filename}{The fully qualified filename of a Vortex .dat or .stdat file} \item{runs}{The number of simulation runs} +\item{dec_sep}{Decimal separator (default ".")} + \item{verbose}{Progress messages, default: FALSE} } \value{ diff --git a/man/collate_run.Rd b/man/collate_run.Rd index 70e0ee3..7926ef8 100644 --- a/man/collate_run.Rd +++ b/man/collate_run.Rd @@ -8,6 +8,7 @@ collate_run( project, scenario, npops = 1, + dec_sep = ".", dir_in = NULL, save2disk = TRUE, dir_out = "ProcessedData", @@ -22,6 +23,8 @@ collate_run( \item{npops}{The total number of simulated populations including the metapopulation} +\item{dec_sep}{Decimal separator (default ".")} + \item{dir_in}{The local folder containing Vortex files, default: NULL. If not specified, will fall back to use current working directory.} diff --git a/man/collate_yr.Rd b/man/collate_yr.Rd index eac114c..6bafefd 100644 --- a/man/collate_yr.Rd +++ b/man/collate_yr.Rd @@ -8,6 +8,7 @@ collate_yr( project, scenario, npops_noMeta = 1, + dec_sep = ".", dir_in = NULL, save2disk = TRUE, dir_out = "ProcessedData", @@ -22,6 +23,8 @@ collate_yr( \item{npops_noMeta}{The total number of populations excluding the metapopulation, default: 1} +\item{dec_sep}{Decimal separator (default ".")} + \item{dir_in}{The local folder containing Vortex files, default: NULL. If not specified, will fall back to use current working directory.} diff --git a/man/rRec.Rd b/man/rRec.Rd index f0f437b..23fff3e 100644 --- a/man/rRec.Rd +++ b/man/rRec.Rd @@ -28,7 +28,7 @@ used as baseline if simulations were not conducted with the ST module} \item{ST}{Whether files are from sensitivity analysis (TRUE), or not (FALSE, default)} -\item{runs}{The number of Vortex simulation runs} +\item{runs}{The number of simulation runs} \item{yr0}{The time window to be considered (first and last year respectively)} diff --git a/vignettes/vortexRguide.Rmd b/vignettes/vortexRguide.Rmd index 5e69ad3..1be9e28 100644 --- a/vignettes/vortexRguide.Rmd +++ b/vignettes/vortexRguide.Rmd @@ -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.