Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making the package build #42

Merged
merged 6 commits into from
Apr 28, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@

.DS_Store
.Rproj.user
fish-tracking.Rproj
7 changes: 5 additions & 2 deletions scripts/packages/fishtracking/DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,8 @@ Description: This package will help you manage fish tracking telemetry data. It
Depends: R (>= 3.1.0)
License: GPL
LazyData: true
Imports: testthat
Suggests: testthat
Imports:
lubridate,
DBI
Suggests:
testthat
11 changes: 10 additions & 1 deletion scripts/packages/fishtracking/NAMESPACE
Original file line number Diff line number Diff line change
@@ -1 +1,10 @@
exportPattern(".")
# Generated by roxygen2 (4.1.1): do not edit by hand

export(input2store)
export(merge_files)
export(parse_date)
export(parse_station)
export(read_input)
export(validate_data)
importFrom(DBI,dbWriteTable)
importFrom(lubridate,parse_date_time)
67 changes: 39 additions & 28 deletions scripts/packages/fishtracking/R/input2store.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,24 @@
#' following columns: Date.Time, Receiver.id, Receiver.Name, Transmitter.id
#' @examples
#' read_input("VR2W_122340_20141010_1.csv")
#' @export
read_input <- function (filename) {
data <- read.csv(filename, sep=",", stringsAsFactors=FALSE)
if (length(colnames(data)) == 14) {
if (ncol(data) == 14) {
# inbo format. Date header should be "Date.Time"
if (colnames(data)[1] == "Date.Time") {
outdata <- data[, c("Date.Time", "Receiver.Name", "Station.Name")]
colnames(outdata) <- c("Date.Time", "Receiver.id", "Receiver.Name")
outdata$Transmitter.id <- paste(data$Code.Space, data$ID, sep="-")
}
} else if (length(colnames(data)) == 10) {
} else if (ncol(data) == 10) {
# vliz format. Field 2 should be "Receiver"
if (colnames(data)[2] == "Receiver") {
colnames(data)[1] <- "Date.and.Time"
outdata <- data[, c("Date.and.Time", "Receiver","Station.Name", "Transmitter")]
colnames(outdata) <- c("Date.Time", "Receiver.id", "Receiver.Name", "Transmitter.id")
}
} else if (length(colnames(data)) == 11) {
} else if (ncol(data) == 11) {
# VUE export format. Field 1 should be "DateTimeUTC"
if (colnames(data)[1] == "DateTimeUTC") {
outdata <- data[, c("DateTimeUTC", "Receiver", "ReceiverCode", "Transmitter")]
Expand All @@ -56,13 +57,12 @@ read_input <- function (filename) {
#' with the columns Date.Time, Receiver.id, Receiver.Name, Transmitter.id
#' @examples
#' merge_files("/path/to/directory/")
#' @export
merge_files <- function (directory) {
if (substr(directory, length(directory), length(directory)) != "/") {
directory = paste(directory, "/", sep="")
}
csvfiles = Sys.glob(paste(directory, "*.csv", sep=""))
contents = lapply(csvfiles, read_input)
ldply(contents)
directory <- normalizePath(directory, winslash = "/", mustWork = TRUE)
csvfiles = Sys.glob(paste(directory, "/*.csv", sep=""))
contents <- lapply(csvfiles, read_input)
do.call(rbind, contents)
}


Expand All @@ -81,12 +81,14 @@ merge_files <- function (directory) {
#' @examples
#' # should return c("2000-03-21 13:21:42", "1952-04-15 09:00:31", NA, NA)
#' parse_date(c("2000-03-21 13:21:42", "15-04-1952 09:00:31", "03-31-2004 03:49:23", "31-02-2004 04:29:42"))
#' @export
#' @importFrom lubridate parse_date_time
parse_date <- function (dateStr) {
# check format "yyyy-mm-dd hh:mm:ss"
result1 = strptime(dateStr, "%Y-%m-%d %H:%M:%S")
# check format "dd-mm-yyyy hh:mm:ss"
result2 = strptime(dateStr, "%d-%m-%Y %H:%M:%S")
result1[is.na(result1)] <- result2[is.na(result1)]
result1 <- parse_date_time(
dateStr,
orders = c("Y-m-d H:M:S", "d-m-Y H:M:S"),
tz = ""
)
return(as.character(result1))
}

Expand All @@ -98,6 +100,7 @@ parse_date <- function (dateStr) {
#'
#' @param stations Vector containing station names
#' @return Vector containing valid station names or NA's
#' @export
parse_station <- function(stations) {
result = grepl(".*-[0-9]*-[0-9]*", stations)
stations[!result] <- NA
Expand All @@ -112,19 +115,29 @@ parse_station <- function(stations) {
#' @return TRUE if data is ok
#' @examples
#' validate_data(data)
#' @export
validate_data <- function(indata) {
indata$Date.Time <- parse_date(indata$Date.Time)
indata$Receiver.Name <- parse_station(indata$Receiver.Name)
ok = TRUE
if (sum(is.na(indata$Date.Time)) > 0) {
print(paste("invalid dates found at rows: ", paste(which(is.na(indata$Date.Time)), collapse=",")))
ok = FALSE
if (anyNA(indata$Date.Time)) {
stop(
"invalid dates found at rows: ",
paste(
which(is.na(indata$Date.Time)),
collapse=","
)
)
}
if (sum(is.na(indata$Receiver.Name)) > 0) {
print(paste("invalid receiver names found at rows: ", paste(which(is.na(indata$Receiver.Name)), collapse=",")))
ok = FALSE
if (anyNA(indata$Receiver.Name)) {
stop(
"invalid receiver names found at rows: ",
paste(
which(is.na(indata$Receiver.Name)),
collapse=","
)
)
}
return(ok)
return(invisible(TRUE))
}

#================================
Expand All @@ -139,12 +152,10 @@ validate_data <- function(indata) {
#' @return Nothing
#' @examples
#' input2store("/path/to/input/directory/")
#' @export
#' @importFrom DBI dbWriteTable
input2store <- function(dbConnection, directory) {
data <- merge_files(directory)
validatedData <- validate_data(data)
if (validatedData) {
dbWriteTable(dbConnection, "detections", validatedData, overwrite=FALSE, append=TRUE)
} else {
print("errors found during validation. Nothing written to database.")
}
}
dbWriteTable(dbConnection, "detections", validatedData, overwrite=FALSE, append=TRUE)
}
2 changes: 0 additions & 2 deletions scripts/packages/fishtracking/R/run_tests.R

This file was deleted.

4 changes: 2 additions & 2 deletions scripts/packages/fishtracking/man/input2store.Rd
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/input2store.R
\name{input2store}
\alias{input2store}
\title{Migrate input data to data store}
\usage{
input2store(directory)
input2store(dbConnection, directory)
}
\arguments{
\item{directory}{The directory containing the tracking data}
Expand Down
2 changes: 1 addition & 1 deletion scripts/packages/fishtracking/man/merge_files.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/input2store.R
\name{merge_files}
\alias{merge_files}
Expand Down
2 changes: 1 addition & 1 deletion scripts/packages/fishtracking/man/parse_date.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/input2store.R
\name{parse_date}
\alias{parse_date}
Expand Down
2 changes: 1 addition & 1 deletion scripts/packages/fishtracking/man/parse_station.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/input2store.R
\name{parse_station}
\alias{parse_station}
Expand Down
2 changes: 1 addition & 1 deletion scripts/packages/fishtracking/man/read_input.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/input2store.R
\name{read_input}
\alias{read_input}
Expand Down
2 changes: 1 addition & 1 deletion scripts/packages/fishtracking/man/validate_data.Rd
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
% Generated by roxygen2 (4.1.0): do not edit by hand
% Generated by roxygen2 (4.1.1): do not edit by hand
% Please edit documentation in R/input2store.R
\name{validate_data}
\alias{validate_data}
Expand Down