Skip to content

Commit

Permalink
Merge branch 'release/3.11.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
psychelzh committed Apr 3, 2024
2 parents f838485 + 26b257b commit 0e5d9d4
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 6 deletions.
3 changes: 2 additions & 1 deletion 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.10.2
Version: 3.11.0
Authors@R: c(
person("Liang", "Zhang", , "[email protected]", role = c("aut", "cre"),
comment = c(ORCID = "0000-0001-9041-1150")),
Expand All @@ -22,6 +22,7 @@ Imports:
cli,
data.iquizoo (>= 2024.3.31.2),
DBI,
jsonlite,
memoise,
rlang (>= 1.0.0),
stringr,
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(check_source)
export(fetch_data)
export(fetch_iquizoo)
export(fetch_iquizoo_mem)
export(parse_data)
export(setup_option_file)
export(setup_source)
export(setup_templates)
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# tarflow.iquizoo 3.11.0

## Breaking Changes

* Added `parse_data()`, previously named as `wrangle_data()`. See <https://github.com/psychelzh/preproc.iquizoo/issues/86>.

# tarflow.iquizoo 3.10.2

* Fixed compatibility issue with 'data.iquizoo' 2024.03.31-2.
Expand Down
38 changes: 38 additions & 0 deletions R/database.R
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,41 @@ fetch_data <- function(project_id, game_id, ...,
params = list(project_id, game_id)
)
}

#' Parse Raw Data
#'
#' Raw data fetched from iQuizoo database is stored in json string format. This
#' function is used to parse raw json string data as [data.frame()] and store
#' them in a list column.
#'
#' @param data The raw data.
#' @param col_raw_json The column name storing raw json string data.
#' @param name_raw_parsed The name used to store parsed data.
#' @return A [data.frame] contains the parsed data.
#' @export
parse_data <- function(data,
col_raw_json = "game_data",
name_raw_parsed = "raw_parsed") {
data[[name_raw_parsed]] <- lapply(
data[[col_raw_json]],
parse_raw_json
)
data[, names(data) != col_raw_json, drop = FALSE]
}

# helper functions
parse_raw_json <- function(jstr) {
tryCatch(
jsonlite::fromJSON(jstr),
error = function(cnd) {
warn(
c(
"Failed to parse json string:",
conditionMessage(cnd),
i = "Will parse it as `NULL` instead."
)
)
return()
}
)
}
13 changes: 8 additions & 5 deletions R/targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -240,12 +240,11 @@ tar_prep_raw <- function(contents,
contents$tar_indices <- syms(sprintf("%s_%s", name_indices, contents$game_id))
list(
raw_data_parsed = if ("parse" %in% action_raw_data) {
check_installed("preproc.iquizoo", "becasue required in wrangling.")
tarchetypes::tar_eval(
targets::tar_target(
tar_parsed,
wrangle_data(tar_data),
packages = "preproc.iquizoo"
parse_data(tar_data),
packages = "tarflow.iquizoo"
),
contents
)
Expand All @@ -255,7 +254,12 @@ tar_prep_raw <- function(contents,
tarchetypes::tar_eval(
targets::tar_target(
tar_indices,
preproc_data(tar_parsed, prep_fun, .input = input, .extra = extra),
preproc.iquizoo::preproc_data(
tar_parsed,
prep_fun,
.input = input,
.extra = extra
),
packages = "preproc.iquizoo"
),
data.iquizoo::merge_preproc(contents)
Expand All @@ -271,7 +275,6 @@ objects <- function() {
utils::globalVariables(
c(
"tar_data", "tar_parsed", "tar_indices",
"wrangle_data", "preproc_data",
"prep_fun", "input", "extra"
)
)
1 change: 1 addition & 0 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ reference:
- fetch_iquizoo
- fetch_iquizoo_mem
- fetch_data
- parse_data
- title: "Miscellaneous Option Functions"
desc: Functions to help you set up options.
contents:
Expand Down
23 changes: 23 additions & 0 deletions man/parse_data.Rd

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

19 changes: 19 additions & 0 deletions tests/testthat/test-database.R
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,22 @@ test_that("Ensure source checking works", {
check_source(source = source_invalid) |>
expect_false()
})

test_that("`parse_data()` works", {
js_str <- r"([{"a": 1, "b": 2}])"
data <- data.frame(game_data = js_str)
parse_data(data)$raw_parsed[[1]] |>
expect_identical(jsonlite::fromJSON(js_str))
parse_data(data, name_raw_parsed = "parsed") |>
expect_named("parsed")
})

test_that("Can deal with invalid or empty json", {
data_case_invalid <- data.frame(game_data = "[1")
parse_data(data_case_invalid)$raw_parsed[[1]] |>
expect_null() |>
expect_warning("Failed to parse json string")
data_case_empty <- data.frame(game_data = c("[]", "{}"))
parse_data(data_case_empty)$raw_parsed |>
lapply(expect_length, 0)
})

0 comments on commit 0e5d9d4

Please sign in to comment.