-
Notifications
You must be signed in to change notification settings - Fork 6
/
A879E0CC
20 lines (20 loc) · 4.48 KB
/
A879E0CC
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
"collab_server" : "",
"contents" : "#' Collect data from different csv files.\n#' \n#' @param folderName A string showing the path of the folder holding different csv files.\n#' @param fileType A string showing the file type, e.g. \"txt\", \"csv\", \"excel\".\n#' @param range A vector containing startRow, endRow, startColumn, endColumn, e.g., \n#' c(2,15,2,3)\n#' @param sheetIndex A number showing the sheetIndex in the excel file, if fileType is excel,\n#' sheetIndex has to be provided, default is 1.\n#' @return The collected data from different files in the folder.\n#' @examples \n#' \n#' #use internal data as an example.\n#' folder <- file.path(path.package(\"hyfo\"), 'extdata')\n#' # file may vary with different environment, it if doesn't work, use local way to get\n#' # folder path.\n#' \n#' a <- collectData(folder, fileType = 'csv', range = c(10, 20, 1,2))\n#' \n#' # More examples can be found in the user manual on http:https://yuanchao-xu.github.io/hyfo/\n#' \n#' @export\n#' @importFrom data.table rbindlist\ncollectData <- function(folderName, fileType = NULL, range = NULL, sheetIndex = 1){\n \n message('All the files in the folder should have the same format')\n \n if (is.null(fileType)) stop('Please enter fileType, \"txt\", \"csv\" or \"excel\".')\n \n if (length(range) > 4) {\n stop('\"range\" should be c(startRow, endRow, startCol, endCol)')\n }else if (is.null(range)) {\n stop('\"range\" can not be blank, e.g., range <- c(startRow, endRow, startCol, endCol).')\n }\n \n if (fileType == 'csv') {\n fileNames <- list.files(folderName, pattern = '*.csv', full.names = TRUE)\n if (length(fileNames) == 0) stop('No csv file in the folder.')\n \n data <- lapply(fileNames, readCsv, range = range)\n data <- rbindlist(data)\n \n } else if (fileType == 'txt') {\n fileNames <- list.files(folderName, pattern = '*.txt', full.names = TRUE)\n if (length(fileNames) == 0) {\n fileNames <- list.files(folderName, pattern = '*.TXT', full.names = TRUE)\n }\n if (length(fileNames) == 0) stop('No text file in the folder.')\n message('For txt file, only startRow and endRow will be considered.')\n data <- lapply(fileNames, readTxt, range = range)\n data <- unlist(data)\n \n# In order not to introduce too much trouble to user, this part has been hiden\n# Because it needs java environment installed.\n#\n } else if (fileType == 'excel') {\n \n message('This part needs java installed in your computer, so it is commentted in\n the original file, check the original R file or https://github.com/Yuanchao-Xu/hyfo/blob/master/R/collectData.R\n for ideas.')\n# fileNames <- list.files(folderName, pattern = '*.xlsx', full.names = TRUE)\n# if (length(fileNames) == 0){\n# fileNames <- list.files(folderName, pattern = '*.xls', full.names = TRUE)\n# }\n# \n# if (length(fileNames) == 0) stop('No excel in the folder.')\n# data <- lapply(fileNames, readExcel, range = range, sheetIndex = sheetIndex)\n# checkBind(data, 'rbind')\n# data <- do.call('rbind', data)\n }else{\n stop('fileType should be \"txt\", \"csv\" or \"excel\".')\n }\n \n \n return(data)\n \n}\n\n# #importFrom xlsx read.xls\n# readExcel <- function(fileName, range, sheetIndex){\n# data <- read.xls(fileName, sheetIndex = sheetIndex, rowIndex = seq(range[1], range[2]),\n# colIndex = seq(range[3], range[4])) \n# colnames(data) <- seq(1, dim(data)[2])\n# \n# message(fileName) \n# return(data)\n# }\n\nreadTxt <- function(fileName, range){\n data <- readLines(fileName)\n data <- data[range[1]:range[2]]\n return(data)\n}\n\n\n\n#' @importFrom utils read.csv\n#' @references \n#' R Core Team (2015). R: A language and environment for statistical computing. R Foundation for\n#' Statistical Computing, Vienna, Austria. URL http:https://www.R-project.org/.\nreadCsv <- function(fileName, range){\n \n data <- read.csv(fileName, skip = range[1] - 1, header = FALSE)\n data <- data[1:(range[2] - range[1] + 1), range[3]:range[4]]\n \n return(data)\n}\n\n\n\n\n\n",
"created" : 1487522346501.000,
"dirty" : false,
"encoding" : "ASCII",
"folds" : "",
"hash" : "1659278673",
"id" : "A879E0CC",
"lastKnownWriteTime" : 1487522383,
"last_content_update" : 1487522383743,
"path" : "~/GitHub/hyfo/R/collectData.R",
"project_path" : "R/collectData.R",
"properties" : {
},
"relative_order" : 14,
"source_on_save" : false,
"source_window" : "",
"type" : "r_source"
}