Skip to content

Commit

Permalink
Add unpack_aux_data function
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansteffan committed May 30, 2024
1 parent ff2d9ec commit f571de2
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 2 deletions.
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: peekbankr
Type: Package
Title: Accessing the Peekbank Database
Version: 0.1.1.9002
Version: 0.1.1.9003
Authors@R: c(
person("Mika", "Braginsky", email = "mika.br@gmail.com", role = c("aut", "cre")),
person("Kyle", "MacDonald", email = "kylem412@gmail.com", role = "aut"),
Expand All @@ -28,5 +28,5 @@ Imports:
Suggests:
knitr,
rmarkdown
RoxygenNote: 7.1.1
RoxygenNote: 7.2.3
VignetteBuilder: knitr
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export(get_trial_types)
export(get_trials)
export(get_xy_timepoints)
export(list_peekbank_tables)
export(unpack_aux_data)
importFrom(magrittr,"%<>%")
importFrom(magrittr,"%>%")
importFrom(rlang,.data)
52 changes: 52 additions & 0 deletions R/peekbankr.R
Original file line number Diff line number Diff line change
Expand Up @@ -514,3 +514,55 @@ get_xy_timepoints <- function(dataset_id = NULL, dataset_name = NULL,

return(xy_timepoints)
}


#' Unpack the json sting in the *_aux_data column and turns
#' it into a nested R list
#'
#'
#' @param df a dataframe in the peekbank format that has an aux data column
#'
#' @return the input dataframe, with the *_aux_data column unpacked
#'
#' @export
#'
#' @examples
#' \dontrun{
#' subjects_table <- unpack_aux_data(df = subjects_table)
#' }
unpack_aux_data <- function(df) {
all_names <- colnames(df)
aux_name <- all_names[str_which(all_names, ".*_aux_data$")]
if (length(aux_name) == 0) return(df)
aux_list <- df |>
ungroup() |>
pull(all_of(aux_name)) |>
lapply(\(aux) {
if(is.na(aux) | is.null(aux)) return(aux)
jsonlite::fromJSON(aux)
})
if(all(is.na(aux_list))) return(df)

col_names <- purrr::flatten(aux_list) |> names() |> unique()
col_names <- col_names[!is.na(col_names)]
aux_cols <- lapply(col_names, \(col_name) {
sapply(aux_list, \(aux) {aux[col_name]})
}) |>
`names<-`(value = col_names) |>
as_tibble() |>
mutate(across(everything(), \(aux) {
if (any(sapply(aux, \(aux_val) {typeof(aux_val) == "list"}))) {
aux <- lapply(aux, \(aux_val) {
if(all(is.na(aux_val))) return(NA)
bind_rows(aux_val)
})}
if (all(sapply(aux, is.atomic))) {
aux <- list_simplify(aux, strict = FALSE) # May need a better fix for NAs
}
aux
}))
df |>
cbind(aux_cols) |>
select(-all_of(aux_name)) |>
nest("{aux_name}" := all_of(colnames(aux_cols)))
}
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,28 @@ The `peekbankr` package allows you to access data in peekbank from R. This remov
remotes::install_github("langcog/peekbankr")
```

### Local Installation from Source

When developing, you can run:

```
install.packages(".", repos = NULL, type="source", dependencies=TRUE)
```

If it fails to install the `RMySQL` dependency automatically, you can manually trigger the installation using

```
install.packages("RMySQL")
```

After making changes, be sure to run

```
roxygen2::roxygenise()
```

to update exports and documentation.

### Usage

Here's a simple workflow for using `peekbankr` to get data from a single study.
Expand Down
24 changes: 24 additions & 0 deletions man/unpack_aux_data.Rd

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

0 comments on commit f571de2

Please sign in to comment.