-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add functionality to read sample data from github release
- Loading branch information
Tuomo Nieminen
committed
Sep 10, 2018
1 parent
1031cda
commit 95b6e98
Showing
1 changed file
with
39 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,53 @@ | ||
#' Path to read.gt3x package sample data | ||
#' | ||
#' @param index The index of a sample file to retrieve | ||
#' @param index Integer. The index of a sample file to retrieve. If NULL (default) the path to the directory | ||
#' including the sample files will be returned. | ||
#' | ||
#' @examples | ||
#' dir <- gt3x_datapath() | ||
#' gt3x_filename <- gt3x_datapath(1) | ||
#' | ||
#' @export | ||
gt3x_datapath <- function(index = NULL) { | ||
datadir <- file.path(path.package("read.gt3x"), "extdata") | ||
if(!dir.exists(datadir)) stop("Data directory not found") | ||
# TODO: download data the first time this is called. | ||
homedir <- path.expand('~') | ||
datadir <- file.path(homedir, ".read.gt3x-data") | ||
if(!dir.exists(datadir)) | ||
dir.create(datadir) | ||
filenames <- gt3x_filename(index) | ||
for(filename in filenames) { | ||
if(!file.exists(file.path(datadir, filename))) | ||
gt3x_download(url = gt3x_url(filename=filename), destfile = file.path(datadir, filename)) | ||
} | ||
if(!is.null(index)) { | ||
files <- list_gt3x(datadir) | ||
if(index > length(files)) stop("Index is larger than the number of sample files, which there are ", length(files)) | ||
return(files[index]) | ||
} | ||
datadir | ||
} | ||
|
||
#' Download a gt3xfile | ||
gt3x_download <- function(url, destfile) { | ||
download.file(url = url, destfile = destfile, method = "auto", mode = "wb") | ||
} | ||
|
||
#' Get url of gt3x sample files | ||
gt3x_url <- function(index = NULL, filename = NULL, version = "v0.1-alpha", baseurl = "https://github.com/THLfi/read.gt3x/releases/download") { | ||
dataurl <- gt3x_dataurl(version, baseurl) | ||
if(is.null(filename)) | ||
filename <- gt3x_filename(index) | ||
file.path(dataurl, filename) | ||
} | ||
|
||
#' Get url of github release | ||
gt3x_dataurl <- function(version = "v0.1-alpha", baseurl = "https://github.com/THLfi/read.gt3x/releases/download") { | ||
url <- file.path(baseurl, version) | ||
} | ||
|
||
#' Get sample gt3x filenames | ||
gt3x_filename <- function(index = NULL) { | ||
files <- c("EE_left_29.5.2017-05-30.gt3x", "SS_left_19.5.2017-05-22.gt3x") | ||
if(is.null(index)) return(files) | ||
if(index > length(files)) stop("Index is larger than the number of available files") | ||
if(index < 1) stop("Index can't be less than 1") | ||
files[index] | ||
} |