Skip to content

Commit

Permalink
Merge branch 'release/3.10.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
psychelzh committed Mar 29, 2024
2 parents 15ab20a + 33bb134 commit 20df7b7
Show file tree
Hide file tree
Showing 22 changed files with 655 additions and 239 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ jobs:
R_KEEP_PKG_SOURCE: yes

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -51,3 +51,4 @@ jobs:
MYSQL_PASSWORD: ${{ secrets.MYSQL_PASSWORD }}
with:
upload-snapshots: true
build_args: 'c("--no-manual","--compact-vignettes=gs+qpdf")'
2 changes: 1 addition & 1 deletion .github/workflows/lint.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ jobs:
permissions:
contents: write
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand All @@ -41,7 +41,7 @@ jobs:

- name: Deploy to GitHub pages 🚀
if: github.event_name != 'pull_request'
uses: JamesIves/github-pages-deploy-action@v4.4.1
uses: JamesIves/github-pages-deploy-action@v4.5.0
with:
clean: false
branch: gh-pages
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/test-coverage.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}

steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-r@v2
with:
Expand Down Expand Up @@ -43,12 +43,12 @@ jobs:
if: always()
run: |
## --------------------------------------------------------------------
find ${{ runner.temp }}/package -name 'testthat.Rout*' -exec cat '{}' \; || true
find '${{ runner.temp }}/package' -name 'testthat.Rout*' -exec cat '{}' \; || true
shell: bash

- name: Upload test results
if: failure()
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: coverage-test-failures
path: ${{ runner.temp }}/package
9 changes: 3 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tarflow.iquizoo
Title: Setup "targets" Workflows for "iquizoo" Data Processing
Version: 3.9.3
Version: 3.10.0
Authors@R: c(
person("Liang", "Zhang", , "psychelzh@outlook.com", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9041-1150")),
Expand All @@ -22,26 +22,23 @@ Imports:
cli,
data.iquizoo (>= 2023.10.22),
DBI,
dplyr,
memoise,
purrr,
rlang (>= 1.0.0),
stringr,
tarchetypes,
targets,
tidyr
targets
Suggests:
bit64,
covr,
digest,
lifecycle,
odbc,
preproc.iquizoo (>= 2.6.0),
purrr,
RMariaDB (>= 1.3.1),
roxygen2,
testthat (>= 3.0.0),
tibble,
tidytable,
withr
Remotes:
psychelzh/data.iquizoo,
Expand Down
5 changes: 3 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ export(fetch_iquizoo_mem)
export(setup_option_file)
export(setup_source)
export(setup_templates)
export(tar_fetch_data)
export(tar_prep_iquizoo)
export(tar_prep_proj)
export(tar_prep_raw)
export(use_targets_pipeline)
import(dplyr)
import(rlang)
import(tidyr)
14 changes: 14 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
# tarflow.iquizoo 3.10.0

## Breaking Changes

* Now `query` argument of `fetch_data()` is optional. If not specified, the default query stored in the package will be used.

## Enhancements

* Added `suffix_format` argument to `fetch_data()` to specify the format of suffix in the query file. This is useful when you want to use a different format of suffix in the query file.
* Enhanced the documentation of `fetch_data()`.
* Let package not depend on dplyr, tidyr and purrr packages (#84).
* Exported more targets factory functions: `tar_prep_proj()`, `tar_fetch_data()`, `tar_prep_raw()`.
* Do not add `progress_hash` objects when `check_progress` is set to `FALSE` in `tar_prep_iquizoo()`.

# tarflow.iquizoo 3.9.3

## Breaking Changes
Expand Down
129 changes: 29 additions & 100 deletions R/database.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,32 +70,54 @@ fetch_iquizoo_mem <- function(cache = NULL) {

#' Fetch data from iQuizoo database
#'
#' @param query A parameterized SQL query. Note the query should also contain
#' a `glue` expression to inject the table name, i.e., `"{ table_name }"`.
#' This function is a wrapper of [fetch_iquizoo()], which is used as a helper
#' function to fetch data from the iQuizoo database.
#'
#' The data essentially means one of the two types of data: raw data or scores.
#' The raw data is the original data collected from the game, while the scores
#' are the scores calculated by the iQuizoo system. While scores can also be
#' calculated from the raw data, the pre-calculated scores are used to for some
#' quick analysis.
#'
#' The data is separated by project date, so the table name is suffixed by the
#' project date, which is automatically fetched from the database by this
#' function. You could set the format of the date suffix by `suffix_format`,
#' although currently you should not need to change it because it probably will
#' not change in the future. Finally, this suffix should be substituted into the
#' query, which should contain an expression to inject the table name, i.e.,
#' `"{table_name}"`.
#'
#' @param project_id The project id to be bound to the query.
#' @param game_id The game id to be bound to the query.
#' @param ... Further arguments passed to [fetch_iquizoo()].
#' @param what What to fetch. Can be either "raw_data" or "scores".
#' @param query A parameterized SQL query. A default query file is stored in the
#' package, which is often enough for most cases. You can also specify your
#' own query file by this argument. See details for more information.
#' @param suffix_format The format of the date suffix. See details for more
#' information.
#' @return A [data.frame] contains the fetched data.
#' @export
fetch_data <- function(query, project_id, game_id, ...,
what = c("raw_data", "scores")) {
fetch_data <- function(project_id, game_id, ...,
what = c("raw_data", "scores"),
query = NULL,
suffix_format = "%Y0101") {
check_dots_used()
what <- match.arg(what)
# the database stores data from each year into a separate table with the
# suffix of course date with the format "<year>0101"
# data separated by project date, so we need to get the project date first
suffix <- package_file("sql", "project_date.sql") |>
read_file() |>
fetch_iquizoo(params = project_id) |>
.subset2("project_date") |>
format("%Y0101")
format(suffix_format)
table_name <- paste0(
switch(what,
raw_data = "content_orginal_data_",
scores = "content_ability_score_"
),
suffix
)
query <- query %||% read_file(package_file("sql", paste0(what, ".sql")))
fetch_iquizoo(
stringr::str_glue(
query,
Expand All @@ -105,96 +127,3 @@ fetch_data <- function(query, project_id, game_id, ...,
params = list(project_id, game_id)
)
}

#' Set data source
#'
#' @param driver The driver used. Set as an option of `"tarflow.driver"`.
#' Options are [odbc::odbc()] and [RMariaDB::MariaDB()], both of which need
#' pre-configurations. Default to first available one.
#' @param dsn The data source name of an **ODBC** database connector. See
#' [odbc::dbConnect()] for more information. Used when `driver` is set as
#' [odbc::odbc()].
#' @param groups Section identifier in the `default.file`. See
#' [RMariaDB::MariaDB()] for more information. Used when `driver` is set as
#' [RMariaDB::MariaDB()].
#' @return An S3 class of `tarflow.source` with the options.
#' @export
setup_source <- function(driver = getOption("tarflow.driver"),
dsn = getOption("tarflow.dsn"),
groups = getOption("tarflow.groups")) {
structure(
list(
driver = driver,
dsn = dsn,
groups = groups
),
class = "tarflow.source"
)
}

#' Check if the database based on the given data source is ready
#'
#' @param source The data source from which data is fetched. See
#' [setup_source()] for details.
#' @return TRUE if the database is ready, FALSE otherwise.
#' @export
check_source <- function(source = setup_source()) {
if (!inherits(source, "tarflow.source")) {
cli::cli_abort(
"{.arg source} must be created by {.fun setup_source}.",
class = "tarflow_bad_source"
)
}
# nocov start
if (inherits(source$driver, "OdbcDriver")) {
return(DBI::dbCanConnect(source$driver, dsn = source$dsn))
}
# nocov end
if (inherits(source$driver, "MariaDBDriver")) {
return(DBI::dbCanConnect(source$driver, groups = source$groups))
}
return(FALSE)
}

# nocov start

#' Setup MySQL database connection option file
#'
#' This function will create a MySQL option file at the given path. To ensure it
#' works, set these environment variables before calling this function:
#' - `MYSQL_HOST`: The host name of the MySQL server.
#' - `MYSQL_USER`: The user name of the MySQL server.
#' - `MYSQL_PASSWORD`: The password of the MySQL server.
#'
#' @param path The path to the option file. Default location is operating system
#' dependent. On Windows, it is `C:/my.cnf`. On other systems, it is
#' `~/.my.cnf`.
#' @param overwrite Whether to overwrite the existing option file.
#' @param quietly A logical indicates whether message should be suppressed.
#' @return NULL (invisible).
#' @export
setup_option_file <- function(path = NULL, overwrite = FALSE, quietly = FALSE) {
my_cnf_tmpl <- read_file(package_file("database", "my.cnf.tmpl"))
path <- path %||% default_file()
if (file.exists(path) && !overwrite) {
if (!quietly) {
cli::cli_alert_warning(
"Option file already exists. Use {.arg overwrite = TRUE} to overwrite.",
class = "tarflow_option_file_exists"
)
}
return(invisible())
}
writeLines(stringr::str_glue(my_cnf_tmpl), path)
}

# helper functions
default_file <- function() {
if (Sys.info()["sysname"] == "Windows") {
return("C:/my.cnf")
} else {
return("~/.my.cnf")
}
}

# nocov end
Loading

0 comments on commit 20df7b7

Please sign in to comment.