Skip to content

Commit

Permalink
Merge branch 'release/1.13.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pbchase committed Jun 23, 2023
2 parents 43180b7 + f377f43 commit eaa9cf4
Show file tree
Hide file tree
Showing 26 changed files with 517 additions and 48 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: redcapcustodian
Type: Package
Title: Data automation for R-centric workflows with a nod towards REDCap
Version: 1.12.0
Version: 1.13.0
Authors@R: c(
person("Philip", "Chase",
email = "[email protected]",
Expand Down Expand Up @@ -59,6 +59,7 @@ Imports:
Suggests:
RSQLite,
digest,
duckdb,
fs,
knitr (>= 1.18),
rmarkdown (>= 2.0),
Expand Down
1 change: 1 addition & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ RUN R -e "install.packages(c( \
'RCurl', \
'REDCapR', \
'RMariaDB', \
'argparse', \
'checkmate', \
'dbx', \
'digest', \
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(connect_to_db)
export(connect_to_log_db)
export(connect_to_redcap_db)
export(convert_schema_to_sqlite)
export(copy_entire_table_to_db)
export(create_allocation_rows)
export(create_randomization_row)
export(create_test_table)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# redcapcustodian 1.13.0 (released 2023-06-23)
- Add project_purpose_other_research_labels.rda (@pbchase, #123)
- Add project_status_labels and project_purpose_labels (@pbchase, #122)
- Add conn parameter to write_summary_metrics() (@pbchase, #122)
- Add copy_entire_table_to_db() (@pbchase, #122)
- Update render_report.R to support quarto files (@pbchase, #118)

# redcapcustodian 1.12.0 (released 2023-06-02)
- Add unnest_job_summary_data_json_object() (@pbchase, #111)
- Fix Version in DESCRIPTION (@pbchase)
Expand Down
35 changes: 34 additions & 1 deletion R/data.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,39 @@
#' @title project_life_cycle_descriptions
#' @description A character vector of the descriptions used in the redcap_log_event table
#' to describe the different stages in the life cycle of a REDCap Project
#' @format A charecter vector with 24 elements
#' @format A character vector with 24 elements
#' @details DETAILS
"project_life_cycle_descriptions"

#' @title project_status_labels
#' @description A tibble of project status IDs and project statuses that reflect their
#' meaning as used in the `status` column of the `redcap_projects` table
#' @format A data frame with 4 rows and 2 variables:
#' \describe{
#' \item{\code{id}}{double primary key}
#' \item{\code{project_status}}{character redcap project status}
#'}
#' @details DETAILS
"project_status_labels"

#' @title project_purpose_labels
#' @description A tibble of project purpose IDs and project purposes that reflect their
#' meaning as used in the `purpose` column of the `redcap_projects` table
#' @format A data frame with 5 rows and 2 variables:
#' \describe{
#' \item{\code{id}}{double primary key}
#' \item{\code{project_purpose}}{character redcap project purpose}
#'}
#' @details DETAILS
"project_purpose_labels"

#' @title project_purpose_other_research_labels
#' @description A tibble project purpose other IDs and labels that reflect their
#' meaning as used in the `purpose_other` column of the `redcap_projects` table
#' @format A data frame with 5 rows and 2 variables:
#' \describe{
#' \item{\code{id}}{double primary key}
#' \item{\code{project_purpose_other_research}}{character redcap purpose other research}
#'}
#' @details DETAILS
"project_purpose_other_research_labels"
95 changes: 95 additions & 0 deletions R/devtools.R
Original file line number Diff line number Diff line change
Expand Up @@ -146,3 +146,98 @@ mutate_columns_to_posixct <- function(data, column_names) {

return(result)
}

#' @title copy_entire_table_to_db
#' @description Copy and entire DBI table from one DBI connection to another.
#' This is a developer tool designed as an aid to testing and development.
#' It designed to be called via \code{purrr::walk2()} to clone sets of tables in
#' a data-driven way to an ephemeral database created, generally with Duck
#' DB.
#'
#' \strong{Limitations}
#'
#' \itemize{
#' \item The table referenced in \code{table_name} must not exist on \code{target_conn}.
#' \item This function is suitable for cloning small tables.
#' \item When called via \code{purrr::walk2()}, all tables in the vector of
#' table names will be copied to the single \code{target_conn} DBI object
#' even if the source table is on different \code{source_conn} DBI objects.
#' }
#' @param source_conn - the DBI connection object that holds the source table
#' @param table_name - the name of the table to be copied
#' @param target_conn - the DBI connection object to which the table will
#' be copied
#'
#' @return No result
#' @export
#'
#' @examples
#' # Build the objects need for testing
#' test_data <- dplyr::tribble(
#' ~a, ~b, ~c, ~d,
#' "asdf", 1, TRUE, lubridate::ymd_hms("2023-01-14 12:34:56"),
#' "qwer", 2, FALSE, lubridate::ymd_hms("2016-01-14 12:34:56")
#' )
#' table_name <- "test_data"
#' source_conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:")
#' DBI::dbWriteTable(conn = source_conn, name = table_name, value = test_data)
#'
#' # copy the table
#' target_conn <- DBI::dbConnect(duckdb::duckdb(), dbdir = ":memory:")
#' copy_entire_table_to_db(
#' source_conn = source_conn,
#' table_name = table_name,
#' target_conn = target_conn
#' )
#'
#' dplyr::collect(dplyr::tbl(target_conn, table_name))
#'
#' \dontrun{
#' library(tidyverse)
#' library(lubridate)
#' library(dotenv)
#' library(DBI)
#' library(RMariaDB)
#' library(redcapcustodian)
#'
#' init_etl("my_script_name")
#'
#' rc_conn <- connect_to_redcap_db()
#' log_conn <- get_package_scope_var("log_con")
#'
#' # describe the tables you want to clone
#' test_tables <- tribble(
#' ~conn, ~table,
#' rc_conn, "redcap_user_information",
#' rc_conn, "redcap_projects",
#' log_conn, "redcap_summary_metrics"
#' )
#'
#' # make the target DB and clone the tables
#' target_conn <- DBI::dbConnect(
#' duckdb::duckdb(),
#' dbdir = ":memory:"
#' )
#' purrr::walk2(
#' test_tables$conn,
#' test_tables$table,
#' copy_table_to_db,
#' target_conn
#' )
#'
#' # Enumerate the tables you copied if you like
#' DBI::dbListTables(target_conn)
#'
#' # replace original connection objects
#' rc_conn <- target_conn
#' log_conn <- target_conn
#'
#' # At this point you can do destructive things on the original
#' # connection objects because they point at the ephemeral
#' # copies of the tables.
#' }
copy_entire_table_to_db <- function(source_conn, table_name, target_conn) {
dplyr::tbl(source_conn, table_name) %>%
dplyr::collect() %>%
DBI::dbWriteTable(conn = target_conn, name = table_name, value = .)
}
17 changes: 12 additions & 5 deletions R/summary_metrics.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@
#' @param reporting_period_end a datetime object, e.g. ymd_hms("2022-12-01 00:00:00")
#' @param metric_type a character string representing the metric type, e.g. "flux", "state"
#' @param metric_dataframe A wide data frame of key-value pairs with a single row of data
#' @param conn A DBI connection object to the database that holds the
#' `redcap_summary_metrics` table. Can be left as NULL if the connection is available
#' on the package scope var "log_con".
#'
#' @return nothing
#'
Expand All @@ -14,13 +17,19 @@
#' reporting_period_start = ymd_hms("2022-01-01 00:00:00", tz=Sys.getenv("TIME_ZONE")),
#' reporting_period_end = ceiling_date(reporting_period_start, "month", change_on_boundary = T)
#' metric_type = "state",
#' metric_dataframe = my_cool_df
#' metric_dataframe = my_cool_df,
#' conn = my_conn
#' )
#' }
write_summary_metrics <- function(reporting_period_start,
reporting_period_end,
metric_type,
metric_dataframe) {
metric_dataframe,
conn = NULL) {

if (is.null(conn)) {
conn = get_package_scope_var("log_con")
}

tall_df <- metric_dataframe %>%
tidyr::pivot_longer(
Expand All @@ -45,8 +54,6 @@ write_summary_metrics <- function(reporting_period_start,
"script_run_time"
)

log_conn <- get_package_scope_var("log_con")

# log data in redcap_summary_metrics
DBI::dbAppendTable(log_conn, "redcap_summary_metrics", tall_df)
DBI::dbAppendTable(conn, "redcap_summary_metrics", tall_df)
}
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.12.0
1.13.0
33 changes: 33 additions & 0 deletions data-raw/redcap_lookup_tables.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
project_purpose_labels <- dplyr::tribble(
~id, ~project_purpose,
4, "Operational Support",
2, "Research",
3, "Quality Improvement",
1, "Other",
0, "Practice / Just for fun"
)
# write the data
usethis::use_data(project_purpose_labels, overwrite = T)

project_purpose_other_research_labels <- dplyr::tribble(
~id, ~project_purpose_other_research,
0, "Basic or bench research",
1, "Clinical research study or trial",
2, "Translational research 1 (applying discoveries to the development of trials and studies in humans)",
3, "Translational research 2 (enhancing adoption of research findings and best practices into the community)",
4, "Behavioral or psychosocial research study",
5, "Epidemiology",
6, "Repository (developing a data or specimen repository for future use by investigators)",
7, "Other"
)
usethis::use_data(project_purpose_other_research_labels, overwrite = T)

project_status_labels <- dplyr::tribble(
~id, ~project_status,
0, "Development",
1, "Production",
2, "Inactive",
3, "Archived"
)
# write the data
usethis::use_data(project_status_labels, overwrite = T)
Binary file added data/project_purpose_labels.rda
Binary file not shown.
Binary file added data/project_purpose_other_research_labels.rda
Binary file not shown.
Binary file added data/project_status_labels.rda
Binary file not shown.
102 changes: 102 additions & 0 deletions man/copy_entire_table_to_db.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/project_life_cycle_descriptions.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions man/project_purpose_labels.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit eaa9cf4

Please sign in to comment.