Skip to content

Commit

Permalink
Remove wrangle_data()
Browse files Browse the repository at this point in the history
Fix #86

Signed-off-by: Liang Zhang <[email protected]>
  • Loading branch information
psychelzh committed Apr 3, 2024
1 parent 3d82195 commit 7b3ff88
Show file tree
Hide file tree
Showing 5 changed files with 0 additions and 84 deletions.
1 change: 0 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ Depends:
R (>= 4.1.0)
Imports:
dplyr,
jsonlite,
pracma,
purrr,
rlang (>= 0.1.2),
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ export(sumweighted)
export(switchcost)
export(symncmp)
export(synwin)
export(wrangle_data)
import(dplyr)
import(rlang)
import(tidyr)
37 changes: 0 additions & 37 deletions R/preproc.R
Original file line number Diff line number Diff line change
@@ -1,23 +1,3 @@
#' Wrangle Raw Data
#'
#' 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 in which stores user's raw data in
#' format of json string.
#' @param name_raw_parsed The name used to store parsed data.
#' @return A [data.frame] contains the parsed data.
#' @export
wrangle_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]
}

#' Feed Raw Data to Pre-processing
#'
#' Calculate indices using data typically returned by [wrangle_data()].
Expand Down Expand Up @@ -69,23 +49,6 @@ preproc_data <- function(data, fn, ...,
results
}

# 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()
}
)
}

calc_indices <- function(data, fn, ..., col_raw_parsed = "raw_parsed") {
# used as a temporary id for each element
col_id <- ".id"
Expand Down
22 changes: 0 additions & 22 deletions man/wrangle_data.Rd

This file was deleted.

23 changes: 0 additions & 23 deletions tests/testthat/test-preproc.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,3 @@
test_that("Basic situation for `wrangle_data()`", {
js_str <- r"([{"a": 1, "b": 2}])"
data <- tibble::tibble(game_data = js_str)
wrangle_data(data) |>
expect_named("raw_parsed") |>
purrr::pluck("raw_parsed", 1) |>
expect_identical(jsonlite::fromJSON(js_str))
wrangle_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")
wrangle_data(data_case_invalid) |>
purrr::pluck("raw_parsed", 1) |>
expect_null() |>
expect_warning("Failed to parse json string")
data_case_empty <- data.frame(game_data = c("[]", "{}"))
wrangle_data(data_case_empty) |>
purrr::pluck("raw_parsed") |>
purrr::walk(expect_length, 0)
})

test_that("Basic situation in `preproc_data()`", {
data <- tibble::tibble(
user_id = 1:2,
Expand Down

0 comments on commit 7b3ff88

Please sign in to comment.