Title: | Utilities for 'DIZ' R Package Development |
---|---|
Description: | Utility functions used for the R package development infrastructure inside the data integration centers ('DIZ') to standardize and facilitate repetitive tasks such as setting up a database connection or issuing notification messages and to avoid redundancy. |
Authors: | Jonathan M. Mang [aut, cre] , Lorenz A. Kapsner [aut] , MIRACUM - Medical Informatics in Research and Care in University Medicine [fnd], Universitätsklinikum Erlangen, Germany [cph] |
Maintainer: | Jonathan M. Mang <[email protected]> |
License: | GPL-3 |
Version: | 0.1.2 |
Built: | 2024-11-13 03:13:31 UTC |
Source: | https://github.com/miracum/misc-dizutils |
Check if a database table exists.
check_if_table_exists(db_con, table_name)
check_if_table_exists(db_con, table_name)
db_con |
A DBI database connection. See 'db_connection()' for details. |
table_name |
(String) The name of the table or view to be checked. |
True, if the table exists, false otherwise.
## Not run: res <- DIZutils::check_if_table_exists( db_con = DIZutils::db_connection(...), table_name = "my_table" ) ## End(Not run)
## Not run: res <- DIZutils::check_if_table_exists( db_con = DIZutils::db_connection(...), table_name = "my_table" ) ## End(Not run)
Internal function to close the database connection. The function is just a wrapper around 'RPostgres::dbDisconnect'
close_connection(conn)
close_connection(conn)
conn |
A DBI database connection. |
The function is just a wrapper around RPostgres::dbDisconnect / DBI::dbDisconnect and does not return any value.
## Not run: db_con <- DIZutils::db_connection( db_name = "i2b2", db_type = "postgres" ) DIZutils::close_connection( conn = db_con ) ## End(Not run)
## Not run: db_con <- DIZutils::db_connection( db_name = "i2b2", db_type = "postgres" ) DIZutils::close_connection( conn = db_con ) ## End(Not run)
This function provides the functionality to combine multiple statistics to a single statistical overview. This is e.g. useful if you are only allowed to export statistical characteristics from a site but not the data itself. So in this case you have e.g. mean, median and N from each site but want to say something about the mean, median and N over all sites like you had the data of all sites in one big pool and would do the statisitcs there.
combine_stats(summaries, demo = FALSE)
combine_stats(summaries, demo = FALSE)
summaries |
(data.table) Data table containing all stats you want to combine as rows. This data.table must contain the columns 'Min', 'Q10', 'Q25', 'Median', 'Mean', 'SD', 'Q75', 'Q90', 'Max', 'N'. Each row in this data table represents a site as of the example described above. |
demo |
(boolean, default = FALSE) Do you want to see how the function works? Then call 'combine_stats(summaries = NULL, demo = TRUE)'. |
A one-row data.table containing the calculated, aggregates statistics of the input.
Internal function to test and get the database connection of the target data system.
db_connection( system_name = NULL, db_type, headless = TRUE, from_env = TRUE, settings = NULL, timeout = 30, logfile_dir = NULL, lib_path = NULL )
db_connection( system_name = NULL, db_type, headless = TRUE, from_env = TRUE, settings = NULL, timeout = 30, logfile_dir = NULL, lib_path = NULL )
system_name |
(Default = NULL) A character. Name of the database system. Used to find the correct settings from the env. If you don't want to load the settings from the environment, use the 'settings' parameter. Otherwise this funcion will search for all settings beginning with 'system_name' in the environment. If 'system_name = "i2b2"' settings like 'I2B2_HOST' or 'I2B2_PORT' (notice the uppercase) will be loaded from the environment. You can load such an env file e.g. by using 'DIZtools::setenv_file(path_to_file)'. |
db_type |
A character. Type of the database system. Currently implemented systems are: 'postgres', 'oracle'. |
headless |
A boolean (default: 'FALSE'). Indicating, if the function is run only in the console ('headless = TRUE') or on a GUI frontend ('headless = FALSE'). |
from_env |
A boolean (default: 'TRUE'). Should database connection be read from the environment or from a settings file. All necessary parameters must be uppercase and have the prefix of the db_name. E.g.: 'I2B2_HOST' or 'I2B2_PORT'. See the 'settings' parameter for all necessary variables. |
settings |
A list. Required if 'from_env == FALSE'. A list containing settings for the database connection. Required fields are 'host', 'db_name', 'port', 'user' and 'password'. Additionally for Oracle DB's: 'sid' (instead of 'db_name'). If 'settings' is set, 'from_env' will be set to 'FALSE' automatically. |
timeout |
A timeout in sec. for the db-connection establishment. Values below 2 seconds are not recommended. Default is 30 seconds. |
logfile_dir |
(Optional, String, default: "tempdir()") The absolute path to folder where the logfile will be stored. |
lib_path |
A character string. The path to the ojdbc*.jar file. If you run one of the R-containers from the UK-Erlangen DIZ, there might be a lib for oracle here: 'lib_path = "/opt/libs/ojdbc8.jar"' |
If successful, the result will be the established connection. Otherwise the result will be null.
## Not run: db_con <- DIZutils::db_connection( db_name = "i2b2", db_type = "postgres" ) ## End(Not run)
## Not run: db_con <- DIZutils::db_connection( db_name = "i2b2", db_type = "postgres" ) ## End(Not run)
Internal function to read settings for a certain system from the environment. IMPORTANT: If yout want to get any result with your input as prefix, use 'ignore_presets = TRUE'! See param-definition for more details. This function will look at uppercase system_names at default.
get_config_env( system_name, logfile_dir = tempdir(), headless = TRUE, ignore_presets = FALSE, uppercase_system = TRUE )
get_config_env( system_name, logfile_dir = tempdir(), headless = TRUE, ignore_presets = FALSE, uppercase_system = TRUE )
system_name |
The name of the system (This is also the prefix used to get the environment variables with 'SYSTEM_KEY', e.g. 'I2B2_DBNAME'). This function also works if there are multiple instances like 'I2B2_1_DBNAME' and 'I2B2_2_DBNAME'. Then the result will contain nested lists for each occurence. |
logfile_dir |
(Optional, String, default: "tempdir()") The absolute path to folder where the logfile will be stored. |
headless |
A boolean (default: 'FALSE'). Indicating, if the function is run only in the console ('headless = TRUE') or on a GUI frontend ('headless = FALSE'). |
ignore_presets |
(boolean, default = FALSE)
Only return something if all elements
from the presets are found? These are currently 'host', 'port', 'user',
'password', 'sid', 'path'. If you have another suffix after
‘system_name' in your config file, you won’t see it here. To see
everything with prefix 'system_name' simply set 'ignore_presets = TRUE'.
To obtain a list of current default elements, run
|
uppercase_system |
(boolean) Default: True. Otherwise: case-sensitive. |
If successful it returns the config, null otherwise.
get_config_env( system_name = "i2b2", logfile_dir = tempdir(), headless = FALSE )
get_config_env( system_name = "i2b2", logfile_dir = tempdir(), headless = FALSE )
Function to quickly get the currently implemented database systems
get_db_systems()
get_db_systems()
The currently implemented database systems as string array. 'E.g. c("postgres", "oracle")' #'
get_db_systems() # Result: c("postgres", "oracle")
get_db_systems() # Result: c("postgres", "oracle")
Internal function. Get default element names (suffixes) for env reading
get_default_config_elements()
get_default_config_elements()
Vector with default elements
Internal function to query the database. The function sends a sql statement to the database and returns a data.table.
query_database( db_con, sql_statement, no_result = FALSE, close_connection = FALSE )
query_database( db_con, sql_statement, no_result = FALSE, close_connection = FALSE )
db_con |
A DBI database connection. |
sql_statement |
A character string containing a valid SQL statement. Caution: Everything after the first ';' will be cut off. |
no_result |
(boolean, default: FALSE) Is the sql meant to return nothing? E.g. if you just insert or update a table. Then supply 'TRUE' here. If you supply 'FALSE' here, the function expects to receive a result table and tries to convert it to a data.table. |
close_connection |
(boolean, default = FALSE). If TRUE, the connection will be closed after the query was sent and the result received. |
Returns the result of the db-query. If 'no_result' is 'TRUE', the return value will be 'TRUE' if the query was successfully sent. Otherwise (if 'no_result' is 'FALSE' which is the default), the result will be the result of the sql query as data.table.
## Not run: db_con <- DIZutils::db_connection( db_name = "i2b2", db_type = "postgres" ) query_database( db_con = db_con, sql_statement = "SELECT * FROM table_name;" ) query_database( db_con = db_con, sql_statement = "INSERT INTO table_name DEFAULT VALUES;", no_result = TRUE ) ## End(Not run)
## Not run: db_con <- DIZutils::db_connection( db_name = "i2b2", db_type = "postgres" ) query_database( db_con = db_con, sql_statement = "SELECT * FROM table_name;" ) query_database( db_con = db_con, sql_statement = "INSERT INTO table_name DEFAULT VALUES;", no_result = TRUE ) ## End(Not run)
See title.
xml_2_json(xml)
xml_2_json(xml)
xml |
An xml object. |
The json-representation of the xml object.