Skip to content

Commit 059fec5

Browse files
authored
Merge pull request #14 from inbo/develop
Version 0.4
2 parents f7632a4 + e9c4e23 commit 059fec5

31 files changed

+517
-570
lines changed

DESCRIPTION

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
Package: n2khelper
22
Title: Auxiliary Functions for the Analysis and Reporting of the Natura 2000
33
Monitoring
4-
Version: 0.2
5-
Date: 2016-05-30
4+
Version: 0.4
5+
Date: 2017-07-28
66
Authors@R: c(person("Thierry", "Onkelinx", email = "thierry.onkelinx@inbo.be", role = c("aut", "cre")))
7-
Description: Functions for importing the raw data, creating analysis dataset
8-
and running the analysis for the Common Breeding Bird Survey in Flanders.
7+
Description: Auxiliary functions for analysing Natura 2000 monitoring data.
98
Depends:
109
R (>= 3.2.0)
1110
Imports:
@@ -15,6 +14,9 @@ Imports:
1514
lubridate,
1615
methods,
1716
assertthat,
17+
DBI,
18+
tidyr,
19+
lazyeval,
1820
RODBC
1921
License: GPL-3
2022
LazyData: true
@@ -45,6 +47,7 @@ Collate:
4547
'git_connection.R'
4648
'git_recent.R'
4749
'git_sha.R'
50+
'is_chartor.R'
4851
'is_git_repo.R'
4952
'list_files_git.R'
5053
'match_nbn_key.R'

NAMESPACE

+18-1
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,15 @@ export(check_single_logical)
1313
export(check_single_numeric)
1414
export(check_single_posix)
1515
export(check_single_probability)
16+
export(connect_nbn)
1617
export(connect_result)
1718
export(cut_date)
1819
export(get_nbn_key)
1920
export(get_nbn_key_multi)
2021
export(get_nbn_name)
2122
export(git_connect)
2223
export(git_connection)
24+
export(is.chartor)
2325
export(is_git_repo)
2426
export(match_nbn_key)
2527
export(odbc_connect)
@@ -38,26 +40,39 @@ exportMethods(write_delim_git)
3840
importClassesFrom(git2r,cred_ssh_key)
3941
importClassesFrom(git2r,cred_user_pass)
4042
importClassesFrom(git2r,git_repository)
43+
importFrom(DBI,dbExistsTable)
44+
importFrom(DBI,dbGetInfo)
45+
importFrom(DBI,dbGetQuery)
46+
importFrom(DBI,dbListFields)
4147
importFrom(RODBC,odbcClose)
4248
importFrom(RODBC,odbcDriverConnect)
4349
importFrom(RODBC,sqlClear)
4450
importFrom(RODBC,sqlColumns)
4551
importFrom(RODBC,sqlQuery)
46-
importFrom(RODBC,sqlTables)
52+
importFrom(assertthat,'on_failure<-')
4753
importFrom(assertthat,assert_that)
4854
importFrom(assertthat,has_name)
4955
importFrom(assertthat,is.count)
5056
importFrom(assertthat,is.flag)
5157
importFrom(assertthat,is.string)
5258
importFrom(assertthat,noNA)
5359
importFrom(dplyr,"%>%")
60+
importFrom(dplyr,collect)
61+
importFrom(dplyr,count_)
5462
importFrom(dplyr,data_frame)
63+
importFrom(dplyr,filter_)
5564
importFrom(dplyr,funs)
5665
importFrom(dplyr,group_by_)
66+
importFrom(dplyr,inner_join)
5767
importFrom(dplyr,mutate_)
5868
importFrom(dplyr,mutate_each_)
5969
importFrom(dplyr,select_)
70+
importFrom(dplyr,semi_join)
71+
importFrom(dplyr,slice_)
72+
importFrom(dplyr,src_postgres)
6073
importFrom(dplyr,summarise_)
74+
importFrom(dplyr,tbl)
75+
importFrom(dplyr,ungroup)
6176
importFrom(git2r,add)
6277
importFrom(git2r,commit)
6378
importFrom(git2r,config)
@@ -67,6 +82,7 @@ importFrom(git2r,hashfile)
6782
importFrom(git2r,head)
6883
importFrom(git2r,push)
6984
importFrom(git2r,repository)
85+
importFrom(lazyeval,interp)
7086
importFrom(lubridate,is.Date)
7187
importFrom(lubridate,is.POSIXt)
7288
importFrom(lubridate,year)
@@ -80,6 +96,7 @@ importFrom(plyr,ddply)
8096
importFrom(stats,aggregate)
8197
importFrom(stats,as.formula)
8298
importFrom(stats,na.fail)
99+
importFrom(tidyr,spread_)
83100
importFrom(utils,file_test)
84101
importFrom(utils,read.delim)
85102
importFrom(utils,read.table)

R/check_dbtable.R

+29-14
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
#' Check if a table is available in a given ODBC connection
22
#' @param error Indicates the behaviour when a table is missing. Gives an error
33
#' when error = TRUE (default). Return FALSE otherwise.
4-
#' @param schema The schema. Defaults to 'dbo'
4+
#' @param schema The schema. Defaults to 'public'
55
#' @inheritParams odbc_get_id
66
#' @export
7-
#' @importFrom RODBC sqlTables
7+
#' @importFrom assertthat assert_that is.string
8+
#' @importFrom dplyr %>%
9+
#' @importFrom DBI dbExistsTable dbGetInfo
810
#' @importFrom stats na.fail
911
#' @return TRUE when all tables are present in the ODBC connection.
10-
check_dbtable <- function(table, schema = "dbo", channel, error = TRUE){
12+
check_dbtable <- function(table, schema = "public", channel, error = TRUE){
13+
# nocov start
1114
table <- check_character(x = table, name = "table", na.action = na.fail)
1215
if (length(table) == 0) {
1316
stop("'table' must contain at least one value")
1417
}
15-
schema <- check_single_character(x = schema, name = "schema")
16-
if (class(channel) != "RODBC") {
17-
stop("channel is not an ODBC connection")
18+
assert_that(is.string(schema))
19+
if (has_name(channel, "con")) {
20+
assert_that(inherits(channel$con, "DBIConnection"))
21+
this_channel <- channel$con
22+
} else {
23+
assert_that(inherits(channel, "DBIConnection"))
24+
this_channel <- channel
1825
}
19-
# nocov start
2026

21-
available <- sqlTables(channel = channel, schema = schema)$TABLE_NAME
22-
check <- table %in% available
23-
if (all(check)) {
27+
test <- sapply(
28+
table,
29+
function(x){
30+
c(schema, x) %>%
31+
dbExistsTable(conn = this_channel)
32+
}
33+
)
34+
if (all(test)) {
2435
return(TRUE)
2536
}
2637
if (error) {
27-
stop(
28-
"Table(s) missing: ",
29-
paste(table[!check], collapse = ", ")
30-
)
38+
names(test)[!test] %>%
39+
paste(collapse = ", ") %>%
40+
sprintf(
41+
fmt = "Table(s) %s not found in schema %s on database %s",
42+
schema,
43+
dbGetInfo(this_channel)$dbname
44+
) %>%
45+
stop(call. = FALSE)
3146
} else {
3247
return(FALSE)
3348
}

R/check_dbtable_variable.R

+4-8
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
#' @param error Indicates the behaviour when a variable is missing. Gives an error when error = TRUE (default). Return FALSE otherwise.
33
#' @inheritParams odbc_get_id
44
#' @export
5-
#' @importFrom RODBC sqlColumns
5+
#' @importFrom DBI dbListFields
66
#' @importFrom stats na.fail
77
#' @return TRUE when all variables are present in the table.
88
check_dbtable_variable <- function(
99
table,
1010
variable,
11-
schema = "dbo",
11+
schema = "public",
1212
channel,
1313
error = TRUE
1414
){
@@ -24,12 +24,8 @@ check_dbtable_variable <- function(
2424
check_dbtable(table = table, schema = schema, channel = channel, error = TRUE)
2525
# nocov start
2626

27-
available <- sqlColumns(
28-
channel = channel,
29-
sqtable = table,
30-
schema = schema
31-
)$COLUMN_NAME
32-
check <- variable %in% available
27+
check <- variable %in%
28+
dbListFields(conn = channel$con, name = c(schema, table))
3329
if (all(check)) {
3430
return(TRUE)
3531
}

R/check_id.R

+4-17
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,18 @@
22
#' @inheritParams odbc_get_id
33
#' @export
44
#' @importFrom assertthat assert_that is.count
5-
#' @importFrom RODBC sqlQuery
5+
#' @importFrom DBI dbGetQuery
66
check_id <- function(value, variable, table, channel){
77
assert_that(is.count(value))
88
variable <- check_single_character(variable, name = "variable")
99
check_dbtable_variable(table = table, variable = variable, channel = channel)
1010

1111
# nocov start
1212
sql <- paste("SELECT", variable, "FROM", table, "WHERE", variable, "=", value)
13-
selection <- sqlQuery(
14-
channel = channel,
15-
query = sql,
16-
stringsAsFactors = FALSE,
17-
as.is = TRUE
13+
selection <- dbGetQuery(
14+
conn = channel$con,
15+
statement = sql
1816
)
19-
if (!inherits(selection, "data.frame")) {
20-
if (length(grep("Invalid column name", selection)) > 0) {
21-
stop(
22-
"The variable '", variable, "' doesn't exists in table '", table, "'"
23-
)
24-
}
25-
if (length(grep("Conversion failed", selection))) {
26-
stop(paste(selection, collapse = "\n"))
27-
}
28-
}
29-
3017
if (nrow(selection) == 0) {
3118
return(FALSE)
3219
} else {

R/connect_result.R

+25-24
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,36 @@
11
#' Opens an ODBC connection to the 'results' database
22
#' @export
3-
#' @importFrom RODBC odbcDriverConnect
4-
#' @param develop Logical value. Indicates the location ton the results database
5-
#' @param username the username to connect to the database. Use a trusted connection when missing
6-
#' @param password the password for the username. Ignored when using a trusted connection.
3+
#' @importFrom dplyr src_postgres
4+
#' @param username the username to connect to the database.
5+
#' @param password the password for the username.
6+
#' @param develop Logical value. Indicates the location of the results database
77
#' @importFrom assertthat assert_that is.flag noNA is.string
8-
connect_result <- function(develop = TRUE, username, password){
8+
connect_result <- function(username, password, develop = TRUE){
99
assert_that(is.flag(develop))
1010
assert_that(noNA(develop))
11+
assert_that(is.string(username))
12+
assert_that(is.string(password))
13+
14+
dbname <- "n2kresult"
1115
if (develop) {
12-
server <- "INBODEV02\\development"
16+
# nocov start
17+
host <- "localhost"
18+
# nocov end
1319
} else {
1420
stop("Production database not yet defined")
1521
}
16-
driver <- ifelse(.Platform$OS.type == "windows", "SQL Server", "FreeTDS")
17-
if (missing(username)) {
18-
authentication <- "Trusted_Connection=Yes;"
19-
} else {
20-
assert_that(is.string(username))
21-
if (username == "") {
22-
authentication <- "Trusted_Connection=Yes;"
23-
} else {
24-
assert_that(is.string(password))
25-
authentication <- sprintf("Uid=%s;Pwd=%s", username, password)
26-
}
27-
}
28-
connection <- sprintf(
29-
"Driver=%s;Server=%s;Database=D0116on00_SoortenMeetnetAnalyse;%s",
30-
driver,
31-
server,
32-
authentication
22+
# nocov start
23+
src_postgres(
24+
host = host,
25+
dbname = dbname,
26+
user = username,
27+
password = password
3328
)
34-
odbcDriverConnect(connection)
29+
# nocov end
30+
}
31+
32+
#' Open a trusted connection to the NBN database
33+
#' @export
34+
connect_nbn <- function(){
35+
odbcDriverConnect(connection = nbn.dsn)
3536
}

0 commit comments

Comments
 (0)