Skip to content

Commit 5d9ff5d

Browse files
committed
Merge pull request #1 from INBO-Natura2000/develop
Version 0.0
2 parents 751b4f1 + f9df205 commit 5d9ff5d

File tree

109 files changed

+5480
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

109 files changed

+5480
-0
lines changed

.Rbuildignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
^.*\.Rproj$
2+
^\.Rproj\.user$
3+
^data-raw$
4+
^\.git$
5+
^wercker\.yml$
6+
^\.lintr$

.lintr

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
linters: with_defaults(camel_case_linter = NULL, multiple_dots_linter = NULL)

DESCRIPTION

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
Package: n2khelper
2+
Title: Auxiliary Functions for the Analysis and Reporting of the Natura 2000
3+
Monitoring
4+
Version: 0.0
5+
Authors@R: c(person("Thierry", "Onkelinx", email = "thierry.onkelinx@inbo.be", role = c("aut", "cre")))
6+
Description: Functions for importing the raw data, creating analysis dataset
7+
and running the analysis for the Common Breeding Bird Survey in Flanders.
8+
Depends:
9+
R (>= 3.2.0)
10+
Imports:
11+
git2r,
12+
digest,
13+
dplyr,
14+
plyr,
15+
lubridate,
16+
methods,
17+
assertthat,
18+
RODBC
19+
License: GPL-3
20+
LazyData: true
21+
Suggests:
22+
testthat
23+
Collate:
24+
'gitConnection_class.R'
25+
'auto_commit.R'
26+
'check_character.R'
27+
'check_dataframe_covariate.R'
28+
'check_dataframe_variable.R'
29+
'check_dbtable.R'
30+
'check_dbtable_variable.R'
31+
'check_git_repo.R'
32+
'check_id.R'
33+
'check_path.R'
34+
'check_single_character.R'
35+
'check_single_logical.R'
36+
'check_single_numeric.R'
37+
'check_single_posix.R'
38+
'check_single_probability.R'
39+
'check_single_strictly_positive_integer.R'
40+
'connect_result.R'
41+
'cut_date.R'
42+
'get_nbn_key.R'
43+
'get_nbn_key_multi.R'
44+
'get_nbn_name.R'
45+
'get_sha1.R'
46+
'git_connect.R'
47+
'git_connection.R'
48+
'git_recent.R'
49+
'git_sha.R'
50+
'is_git_repo.R'
51+
'list_files_git.R'
52+
'match_nbn_key.R'
53+
'num_32_64.R'
54+
'odbc_connect.R'
55+
'odbc_get_id.R'
56+
'odbc_get_multi_id.R'
57+
'odbc_insert.R'
58+
'read_delim_git.R'
59+
'read_object_environment.R'
60+
'remove_files_git.R'
61+
'write_delim_git.R'

NAMESPACE

+84
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# Generated by roxygen2 (4.1.1): do not edit by hand
2+
3+
export(check_character)
4+
export(check_dataframe_covariate)
5+
export(check_dataframe_variable)
6+
export(check_dbtable)
7+
export(check_dbtable_variable)
8+
export(check_git_repo)
9+
export(check_id)
10+
export(check_path)
11+
export(check_single_character)
12+
export(check_single_logical)
13+
export(check_single_numeric)
14+
export(check_single_posix)
15+
export(check_single_probability)
16+
export(check_single_strictly_positive_integer)
17+
export(connect_result)
18+
export(cut_date)
19+
export(get_nbn_key)
20+
export(get_nbn_key_multi)
21+
export(get_nbn_name)
22+
export(git_connect)
23+
export(git_connection)
24+
export(is_git_repo)
25+
export(match_nbn_key)
26+
export(num_32_64)
27+
export(odbc_connect)
28+
export(odbc_get_id)
29+
export(odbc_get_multi_id)
30+
export(odbc_insert)
31+
export(read_object_environment)
32+
export(sha1_digits)
33+
exportClasses(gitConnection)
34+
exportMethods(auto_commit)
35+
exportMethods(get_sha1)
36+
exportMethods(git_recent)
37+
exportMethods(git_sha)
38+
exportMethods(list_files_git)
39+
exportMethods(read_delim_git)
40+
exportMethods(remove_files_git)
41+
exportMethods(write_delim_git)
42+
importClassesFrom(git2r,cred_ssh_key)
43+
importClassesFrom(git2r,cred_user_pass)
44+
importClassesFrom(git2r,git_repository)
45+
importFrom(RODBC,odbcClose)
46+
importFrom(RODBC,odbcDriverConnect)
47+
importFrom(RODBC,sqlClear)
48+
importFrom(RODBC,sqlColumns)
49+
importFrom(RODBC,sqlQuery)
50+
importFrom(RODBC,sqlTables)
51+
importFrom(assertthat,assert_that)
52+
importFrom(assertthat,has_name)
53+
importFrom(assertthat,is.count)
54+
importFrom(assertthat,is.flag)
55+
importFrom(assertthat,is.string)
56+
importFrom(assertthat,noNA)
57+
importFrom(digest,digest)
58+
importFrom(dplyr,"%>%")
59+
importFrom(dplyr,data_frame)
60+
importFrom(dplyr,funs)
61+
importFrom(dplyr,group_by_)
62+
importFrom(dplyr,mutate_)
63+
importFrom(dplyr,mutate_each_)
64+
importFrom(dplyr,select_)
65+
importFrom(dplyr,summarise_)
66+
importFrom(git2r,add)
67+
importFrom(git2r,commit)
68+
importFrom(git2r,config)
69+
importFrom(git2r,cred_ssh_key)
70+
importFrom(git2r,cred_user_pass)
71+
importFrom(git2r,hashfile)
72+
importFrom(git2r,head)
73+
importFrom(git2r,push)
74+
importFrom(git2r,repository)
75+
importFrom(lubridate,is.Date)
76+
importFrom(lubridate,is.POSIXt)
77+
importFrom(lubridate,year)
78+
importFrom(methods,new)
79+
importFrom(methods,setClass)
80+
importFrom(methods,setClassUnion)
81+
importFrom(methods,setGeneric)
82+
importFrom(methods,setMethod)
83+
importFrom(methods,setValidity)
84+
importFrom(plyr,ddply)

R/auto_commit.R

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
#' Commit staged changes in a git repository with automated message
2+
#'
3+
#' The mesagge is based on the information returned by \code{\link[utils]{sessionInfo}}
4+
#' @param package The name of the package from which we autocommit
5+
#' @param connection The path of the repository. Default to \code{rawdata.path}
6+
#' @param ... parameters passed to \code{git_connection} when relevant
7+
#' @name auto_commit
8+
#' @rdname auto_commit
9+
#' @exportMethod auto_commit
10+
#' @docType methods
11+
#' @importFrom methods setGeneric
12+
#' @include gitConnection_class.R
13+
setGeneric(
14+
name = "auto_commit",
15+
def = function(
16+
package, connection, ...
17+
){
18+
standard.generic("autocommit")
19+
}
20+
)
21+
22+
#' @rdname auto_commit
23+
#' @aliases auto_commit,gitConnection-methods
24+
#' @importFrom methods setMethod
25+
#' @importFrom git2r repository commit cred_user_pass head push
26+
setMethod(
27+
f = "auto_commit",
28+
signature = signature(connection = "ANY"),
29+
definition = function(
30+
package,
31+
connection,
32+
...
33+
){
34+
auto_commit(
35+
package = package,
36+
connection = git_connection(repo.path = connection, ...)
37+
)
38+
}
39+
)
40+
41+
42+
#' @rdname auto_commit
43+
#' @aliases auto_commit,git_connection-methods
44+
#' @importFrom methods setMethod
45+
#' @importFrom git2r commit cred_user_pass head push
46+
setMethod(
47+
f = "auto_commit",
48+
signature = signature(connection = "gitConnection"),
49+
definition = function(package, connection, ...){
50+
package <- check_single_character(package)
51+
52+
#format commit message based on sessionInfo()
53+
info <- sessionInfo()
54+
format.other <- function(x){
55+
paste0(x$Package, " ", x$Version, " built ", x$Built, "\n")
56+
}
57+
message <- paste0(
58+
"Automatic commit from ", package, "\n\n",
59+
info$R.version$version.string, " revision ", info$R.version$"svn rev",
60+
" on ", info$R.version$platform, "\n",
61+
"\nBase packages: ",
62+
paste0(info$basePkgs, collapse = ", "), "\n",
63+
"\nOther package(s):\n",
64+
paste(sapply(info$otherPkgs, format.other), collapse = ""),
65+
"\nLoaded via a namespace:\n",
66+
paste(sapply(info$loadedOnly, format.other), collapse = "")
67+
)
68+
69+
committed <- tryCatch(
70+
commit(repo = connection@Repository, message = message),
71+
error = function(e){
72+
if (e$message == "Error in 'git2r_commit': Nothing added to commit\n") {
73+
FALSE
74+
} else {
75+
e
76+
}
77+
}
78+
)
79+
if ("error" %in% class(committed)) {
80+
stop(committed)
81+
}
82+
if (class(committed) != "git_commit") {
83+
return(invisible(TRUE))
84+
}
85+
if (is.null(connection@Credentials)) {
86+
warning("changes committed but not pushed")
87+
} else {
88+
message("Pushing changes to remote repository")
89+
tryCatch(
90+
push(head(connection@Repository), credentials = connection@Credentials),
91+
error = function(e){
92+
warning(e)
93+
}
94+
)
95+
}
96+
return(invisible(TRUE))
97+
}
98+
)

R/check_character.R

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#' Check if the object is a character
2+
#'
3+
#' Factors are converted to character.
4+
#' @param x the object to check
5+
#' @param name the name of the object to use in the error message
6+
#' @param na.action \code{link[stats]{na.fail}} throws an error in case of \code{NA} (default). \code{link[stats]{na.omit}} will return \code{x} without the \code{NA} values. \code{link[stats]{na.pass}} will return \code{x} with the \code{NA} values.
7+
#' @return The function gives the character back. it throws an error when the input is not a character.
8+
#' @export
9+
#' @examples
10+
#' check_character(c("20", "b"))
11+
check_character <- function(x, name = "x", na.action = na.fail){
12+
if(!class(x) %in% c("character", "factor")){
13+
stop(name, " must be character")
14+
}
15+
x <- na.action(x)
16+
if(is.factor(x)){
17+
return(levels(x)[x])
18+
} else {
19+
return(x)
20+
}
21+
}

R/check_dataframe_covariate.R

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#' Check if the covariates are available in a dataframe
2+
#' @inheritParams check_dataframe_variable
3+
#' @param covariate The right hand side of the model as a character
4+
#' @param response The left hand side of the model as a character
5+
#' @export
6+
check_dataframe_covariate <- function(
7+
df,
8+
covariate,
9+
response = "Count",
10+
error = TRUE
11+
){
12+
covariate <- check_single_character(covariate, name = "covariate")
13+
response <- check_single_character(response, name = "response")
14+
error <- check_single_logical(error)
15+
16+
formula <- as.formula(paste(response, "~ ", covariate))
17+
output <- check_dataframe_variable(
18+
df = df[1, ],
19+
variable = all.vars(formula),
20+
error = error
21+
)
22+
return(output)
23+
}

R/check_dataframe_variable.R

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#' Check if a data.frame contains variables
2+
#'
3+
#' @param df the \code{data.frame} to check
4+
#' @param variable either a character vector with the names of the variable to
5+
#' check or a named list. The names of the list must match the names of the
6+
#' required variables in the data.frame. The elements of the list contain the
7+
#' accepted classes for each varaible.
8+
#' @param name the name of the \code{data.frame} to use in the error message
9+
#' @param error When TRUE (default), the function returns an error when a
10+
#' variable is missing. Otherwise it returns a warning.
11+
#' @return The function returns TRUE when all variables are present. If returns
12+
#' FALSE when a variable is missing and \code{error = FALSE}.
13+
#' @export
14+
#' @examples
15+
#' check_dataframe_variable(
16+
#' df = data.frame(a = integer(0)),
17+
#' variable = "a"
18+
#' )
19+
#' check_dataframe_variable(
20+
#' df = data.frame(a = integer(0)),
21+
#' variable = list(a = c("integer", "numeric"))
22+
#' )
23+
#' @importFrom assertthat assert_that is.string is.flag noNA
24+
check_dataframe_variable <- function(df, variable, name = "df", error = TRUE){
25+
assert_that(is.string(name))
26+
assert_that(inherits(df, "data.frame") | inherits(df, "matrix"))
27+
assert_that(is.flag(error))
28+
assert_that(noNA(error))
29+
assert_that(is.list(variable) | is.character(variable))
30+
31+
if (inherits(variable, "list")) {
32+
required.class <- variable
33+
variable <- names(required.class)
34+
} else {
35+
required.class <- NULL
36+
}
37+
38+
assert_that(length(variable) > 0)
39+
assert_that(noNA(variable))
40+
41+
available <- variable %in% colnames(df)
42+
if (!all(available)) {
43+
missing.var <- paste(variable[!available], collapse = ", ")
44+
if (error) {
45+
stop("Variables missing in ", name, ": ", missing.var)
46+
} else {
47+
warning("Variables missing in ", name, ": ", missing.var)
48+
return(FALSE)
49+
}
50+
}
51+
52+
if (is.null(required.class)) {
53+
return(TRUE)
54+
}
55+
56+
all.NA <- sapply(
57+
df[, variable],
58+
function(x){
59+
all(is.na(x))
60+
}
61+
)
62+
current.class <- sapply(df[, variable[!all.NA]], class)
63+
correct.class <- sapply(seq_along(current.class), function(i){
64+
any(current.class[[i]] %in% required.class[!all.NA][[i]])
65+
})
66+
if (!all(correct.class)) {
67+
wrong.class <- current.class[!correct.class]
68+
wrong.class <- sapply(wrong.class, paste, collapse = "', '")
69+
expected.class <- required.class[!all.NA][names(wrong.class)]
70+
expected.class <- sapply(expected.class, paste, collapse = "', '")
71+
stop(
72+
"Wrong class for following variable(s)\n",
73+
paste0(
74+
names(wrong.class), ": got '", wrong.class,
75+
"', expected '", expected.class, "'\n",
76+
collapse = ", "
77+
)
78+
)
79+
}
80+
return(TRUE)
81+
}

0 commit comments

Comments
 (0)