#' Dose-finding simulations for a list of scenarios
#' @description Run dose-finding simulations based on a customized decision
#'   table for one or more scenarios read from a file.
#' @param decTable A customized decision table in the same format as the output
#'   of \code{\link{dec.table}}.
#' @param file The name of the file containing the scenario definitions. See
#'   \code{\link{read.table}} for details.
#' @param header Logical; if \code{TRUE}, the file contains variable names in
#'   the first line. See \code{\link{read.table}} for details.
#' @param sep The field separator character. Defaults to \code{","}. See
#'   \code{\link{read.table}} for details.
#' @param ... Additional arguments passed to \code{\link{read.table}}.
#' @details In each row of the input file, the parameters must be ordered as
#'   \code{start.level}, \code{nsim}, and \code{truep}. The
#'   dose-finding algorithm is described in \code{\link{dec.sim}}.
#' @return An object of class \code{"dec.sim"} (for one scenario) or
#'   \code{"sl.sim"} (for multiple scenarios). Use \code{\link{summary}} to
#'   obtain and print a summary of the results. The returned object is a list
#'   containing:
#'   \item{mtd}{A vector of dose levels giving the recommended maximum
#'   tolerated dose (MTD) at the end of each trial.}
#'   \item{n.patients}{The average number of patients treated at each dose
#'   level.}
#'   \item{truep}{The input true toxicity probabilities.}
#'   \item{start.level}{The input starting dose level.}
#'   \item{nsim}{The input number of simulated trials.}
#' @author Wenchuan Guo <wguo1017@gmail.com>
#' @import stats
#' @import utils
#' @export
#' @examples
#' dt <- dec.table(0.6,0.4,0.2,0.3,c(3,3,3))
#' test.file <- system.file("extdata", "testS.csv", package = "tsdf")
#' # use a customized decision table
#' table.file <- system.file("extdata", "decTable.csv", package = "tsdf")
#' dec <- read.csv(table.file, row.names = 1, check.names = FALSE)
#' out1 <- sl.sim(as.matrix(dt$table), test.file)
#' out2 <- sl.sim(dec, test.file)

sl.sim <- function(decTable, file, header = TRUE, sep = ",", ...) {
  sl <- read.table(file, header, sep, ...)
  out <- lapply(1:nrow(sl), function(i){
    info <- sl[i, ][!is.na(sl[i, ])]
    n.info <- length(info)
    dec.sim(info[3:n.info], decTable, info[1], info[2])
  })
  if(nrow(sl) > 1) {
    class(out) <- c("sl.sim", "dec.sim")
  } else {
    class(out) <- "dec.sim"
  }
  return(out)
}
