R/quickreadfiles.R

Defines functions quickreadfiles

Documented in quickreadfiles

#' Reads several files
#'
#' \code{quickreadfiles} builts a data frame from several txt files. It
#' assumes that in each file, the first row has the names of the variables.
#' @param path Path of the file (default is the working directory).
#' @param extension Specify whether the file extension is 'txt' or 'csv'.
#' @param ... arguments of the form name_var = c('value1', 'value2',..).
#' A new column with variable name name_var is addes to the data frame.
#' @examples
#' # download the 3 files in
#' # https://github.com/danilinares/quickpsy/tree/master/inst/extdata/example1
#' # and add them to your working directory
#' # dat <- quickreadfiles(subject = c('aa', 'bb', 'cc'), session = c('1', '2'))
#' # fit <- quickpsy(dat, phase, resp, grouping=.(subject), lapses = T, guess = T)
#' # plotcurves(fit)
#' @importFrom utils read.csv
#' @import dplyr
#' @export

quickreadfiles <- function(path = getwd(), extension = 'txt', ...) {

  arguments <- c(as.list(environment()), list(...))
  arguments[1] <- NULL
  arguments[1] <- NULL
  exist<-NULL # Joan added 1-4-2015

  if (extension == 'txt') {
    extensiondot <- '.txt'
    funread <- read.table
  }
  else if (extension == 'csv') {
    extensiondot <- '.csv'
    funread <- read.csv
  }
  else stop('The extension of the files should be txt of csv')

  namesfun <- function(d) {
    namefile <- paste0(path,'/', paste(unlist(d), collapse = ''), extensiondot)
    data.frame(namefile, exist = file.exists(namefile), stringsAsFactors=F)
  }


  namefiles <- expand.grid(arguments) %>%
    group_by_(.dots = names(arguments)) %>%
    do(namesfun(.))

  namefiles %>% filter(exist) %>%
    group_by_(.dots = names(arguments)) %>%
    do(funread(.$namefile, header = T)) %>%
    ungroup()
}

Try the quickpsy package in your browser

Any scripts or data that you put into this service are public.

quickpsy documentation built on Oct. 2, 2019, 5:03 p.m.