Title: | Auxiliary Functions for the Analysis and Reporting of the Natura 2000 Monitoring |
---|---|
Description: | Auxiliary functions for analysing Natura 2000 monitoring data. |
Authors: | Thierry Onkelinx [aut, cre] , Research Institute for Nature and Forest [cph, fnd] |
Maintainer: | Thierry Onkelinx <[email protected]> |
License: | GPL-3 |
Version: | 0.5.0 |
Built: | 2024-10-12 03:10:07 UTC |
Source: | https://github.com/inbo/n2khelper |
Factors are converted to character.
check_character(x, name = "x", na_action = na.fail)
check_character(x, name = "x", na_action = na.fail)
x |
the object to check |
name |
the name of the object to use in the error message |
na_action |
|
The function gives the character back. It throws an error when the input is not a character.
check_character(c("20", "b"))
check_character(c("20", "b"))
Check if the covariates are available in a dataframe
check_dataframe_covariate(df, covariate, response = "Count", error = TRUE)
check_dataframe_covariate(df, covariate, response = "Count", error = TRUE)
df |
the |
covariate |
The right hand side of the model as a character |
response |
The left hand side of the model as a character |
error |
When TRUE (default), the function returns an error when a variable is missing. Otherwise it returns a warning. |
Check if a data.frame contains variables
check_dataframe_variable( df, variable, name = "df", force_na = FALSE, error = TRUE )
check_dataframe_variable( df, variable, name = "df", force_na = FALSE, error = TRUE )
df |
the |
variable |
either a character vector with the names of the variable to check or a named list. The names of the list must match the names of the required variables in the data.frame. The elements of the list contain the accepted classes for each varaible. |
name |
the name of the |
force_na |
check the class of variables with all NA |
error |
When TRUE (default), the function returns an error when a variable is missing. Otherwise it returns a warning. |
The function returns TRUE when all variables are present. If returns
FALSE when a variable is missing and error = FALSE
.
check_dataframe_variable( df = data.frame(a = integer(0)), variable = "a" ) check_dataframe_variable( df = data.frame(a = integer(0)), variable = list(a = c("integer", "numeric")) )
check_dataframe_variable( df = data.frame(a = integer(0)), variable = "a" ) check_dataframe_variable( df = data.frame(a = integer(0)), variable = list(a = c("integer", "numeric")) )
Check if a table is available in a given ODBC connection
check_dbtable(table, schema = "public", channel, error = TRUE)
check_dbtable(table, schema = "public", channel, error = TRUE)
table |
The name of the table |
schema |
The schema. Defaults to 'public' |
channel |
the open dplyr connection to the database. |
error |
Indicates the behaviour when a table is missing. Gives an error when error = TRUE (default). Return FALSE otherwise. |
TRUE when all tables are present in the ODBC connection.
Check if a variable is available in a given table
check_dbtable_variable( table, variable, schema = "public", channel, error = TRUE )
check_dbtable_variable( table, variable, schema = "public", channel, error = TRUE )
table |
The name of the table |
variable |
A vector with the names of the columns |
schema |
The schema of the table. Defaults to public |
channel |
the open dplyr connection to the database. |
error |
Indicates the behaviour when a variable is missing.
Gives an error when error = |
TRUE when all variables are present in the table.
Test if an id exists in a given field of the table
check_id(value, variable, table, channel)
check_id(value, variable, table, channel)
value |
the id value |
variable |
A vector with the names of the columns |
table |
The name of the table |
channel |
the open dplyr connection to the database. |
check if a path is an exisiting file or directory
check_path(path, type = c("file", "directory"), error = TRUE)
check_path(path, type = c("file", "directory"), error = TRUE)
path |
the path of the directory or file name |
type |
either "file" or "directory" |
error |
When TRUE (default), the function returns an error when a variable is missing. Otherwise it returns a warning. |
Check if the object is a single POSIX
check_single_posix(x, name = "x", past = FALSE)
check_single_posix(x, name = "x", past = FALSE)
x |
the object to check |
name |
the name of the object to use in the error message |
past |
Should the function throw an error when x is in the future?
Default is |
The function gives the single POSIX back. It throws an error when the input is not a single character.
check_single_posix(Sys.time())
check_single_posix(Sys.time())
Check if the object is a single probability
check_single_probability(x, name = "x")
check_single_probability(x, name = "x")
x |
the object to check |
name |
the name of the object to use in the error message |
The function gives the single probability back. It throws an error when the input is not a single probability.
check_single_probability(0.5)
check_single_probability(0.5)
Open a trusted connection to the NBN database
connect_nbn()
connect_nbn()
Opens an ODBC connection to the 'results' database
connect_result(username, password, develop = TRUE)
connect_result(username, password, develop = TRUE)
username |
the username to connect to the database. |
password |
the password for the username. |
develop |
Logical value. Indicates the location of the results database |
connect to the unit test database
connect_ut_db( host = "localhost", dbname = "n2kunittest", user = "unittest_analysis", password = "unittest", port = 5432, ... )
connect_ut_db( host = "localhost", dbname = "n2kunittest", user = "unittest_analysis", password = "unittest", port = 5432, ... )
host |
Host name and port number of PostgreSQL database. |
dbname |
Database name. |
user |
User name and password. |
password |
User name and password. |
port |
Port number of database. Defaults to 5432 |
... |
arguments past to |
The periods are defined by a day and month. The same day from different years with be in the same period.
cut_date(x, dm, include_last = TRUE)
cut_date(x, dm, include_last = TRUE)
x |
the dates in POSIXt or Date format. |
dm |
the breakpoints of the periods in 'day-month' format. |
include_last |
Should the last period include the last day?
Defaults to |
x <- as.POSIXct( c( "2015-01-01", "2014-01-02", "2013-01-03", "2012-01-31", "2011-02-01", "2012-12-31" ) ) cut_date(x, dm = c("1-1", "1-2", "1-3"))
x <- as.POSIXct( c( "2015-01-01", "2014-01-02", "2013-01-03", "2012-01-31", "2011-02-01", "2012-12-31" ) ) cut_date(x, dm = c("1-1", "1-2", "1-3"))
Get the NBN key of a species
get_nbn_key(name, language = "la", channel, authority = FALSE)
get_nbn_key(name, language = "la", channel, authority = FALSE)
name |
a vector of species names to check |
language |
The language to use. Defaults to "la" 'scientific name" |
channel |
An open RODBC channel to the NBN database |
authority |
Do the species names include authority? |
Try multiple languages to get a matching NBN key
get_nbn_key_multi(species, orders = c("la", "nl", "en"), channel)
get_nbn_key_multi(species, orders = c("la", "nl", "en"), channel)
species |
A data.frame with the name of species in one or more languages |
orders |
the order in which the languages are tried to get a matching NBN key. |
channel |
An open RODBC channel to the NBN database |
Get the name associated with an NBN key
get_nbn_name(nbn_key, channel)
get_nbn_name(nbn_key, channel)
nbn_key |
A vector with NBN keys |
channel |
An open RODBC channel to the NBN database |
The details are stored in the results database.
git_connect( data_source_name, channel, type = c("ssh", "https"), username = character(0), password = character(0), commit_user, commit_email )
git_connect( data_source_name, channel, type = c("ssh", "https"), username = character(0), password = character(0), commit_user, commit_email )
data_source_name |
The name of the data source |
channel |
the ODBC channel to the database with the connection strings |
type |
Use 'ssh' or 'https' for authentication |
username |
the username in case the ConnectMethod is |
password |
the password to be used in combination with the username. |
commit_user |
the name of the user how will commit |
commit_email |
the email of the user how will commit |
The gitConnection class
Open a git connection
git_connection(repo_path, key, username, password, commit_user, commit_email)
git_connection(repo_path, key, username, password, commit_user, commit_email)
repo_path |
The path of the root of the repository |
key |
Optional: the path to a private ssh key. The public key is assumed to have the same path with a '.pub' extension. Using in case of ssh authentication. |
username |
The optional username used in case of https authentication.
Ignored when |
password |
The password required for the ssh key or the username. Should be missing when the ssh-key doesn't require a password. |
commit_user |
the name of the user how will commit |
commit_email |
the email of the user how will commit |
Repository
a git repository
Credentials
the credentials for the repository
CommitUser
the name of the user how will commit
CommitEmail
the email of the user how will commit
Test if the argument is either character or factor
is_chartor(x)
is_chartor(x)
x |
the object to check |
Merge NBN keys into a species dataframe
match_nbn_key(species, nbn_key, variable)
match_nbn_key(species, nbn_key, variable)
species |
a data.frame with the species names |
nbn_key |
a data.frame with the NBN keys |
variable |
the name of the variable of species to match with InputName from nbn_key |
The connection string is stored in the results database.
odbc_connect(data_source_name, username, password, channel)
odbc_connect(data_source_name, username, password, channel)
data_source_name |
The name of the data source |
username |
the username in case the ConnectMethod is |
password |
the password to be used in combination with the username. |
channel |
the ODBC channel to the database with the connection strings |
Get the id of the matching records
odbc_get_id(table, ..., schema = "public", channel, id_variable = "id")
odbc_get_id(table, ..., schema = "public", channel, id_variable = "id")
table |
The name of the table |
... |
arguments passed to |
schema |
The schema of the table. Defaults to public |
channel |
the open dplyr connection to the database. |
id_variable |
name of the id variable |
Get the corresponding id's
odbc_get_multi_id( data, id_field, merge_field, table, channel, create = FALSE, select = TRUE, rows_at_time = 1000 )
odbc_get_multi_id( data, id_field, merge_field, table, channel, create = FALSE, select = TRUE, rows_at_time = 1000 )
data |
the data.frame |
id_field |
the id fields |
merge_field |
the merge fields |
table |
The name of the table |
channel |
the open dplyr connection to the database. |
create |
When |
select |
Return the matching ID's when |
rows_at_time |
Number of rows to insert in one SQL statement |
a data.frame with data and the id's
Append a data.frame to a table through an ODBC connection
odbc_insert( data, table, channel, schema = "dbo", append = TRUE, rows_at_time = 1000 )
odbc_insert( data, table, channel, schema = "dbo", append = TRUE, rows_at_time = 1000 )
data |
the data.frame |
table |
The name of the table |
channel |
the open dplyr connection to the database. |
schema |
The schema of the table. Defaults to public |
append |
Append the data or overwrite existing rows? |
rows_at_time |
Number of rows to insert in one SQL statement |
The status of the SQL INSERT for each row in returned but invisible.
Read an object from an environment
read_object_environment(object, env, warn = TRUE)
read_object_environment(object, env, warn = TRUE)
object |
the name of the object |
env |
the environment |
warn |
Issue a warning if the object is not found in the environment. Defaults to TRUE |
the object or NULL
is the object doesn't exists in the
environment
object <- "test" value <- TRUE env <- new.env() assign(x = object, value = value, envir = env) read_object_environment(object, env)
object <- "test" value <- TRUE env <- new.env() assign(x = object, value = value, envir = env) read_object_environment(object, env)