Skip to content

Commit 3a59dcc

Browse files
new download logic
1 parent 9e66da3 commit 3a59dcc

18 files changed

+657
-713
lines changed

.github/workflows/pkgdown.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ jobs:
5959
run: pkgdown::build_site_github_pages(new_process = FALSE, install = FALSE)
6060
shell: Rscript {0}
6161

62-
- name: Deploy to GitHub pages <U+0001F680>
62+
- name: Deploy to GitHub pages 🚀
6363
if: github.event_name != 'pull_request'
6464
uses: JamesIves/[email protected]
6565
with:

DESCRIPTION

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ RoxygenNote: 7.2.3
2626
Suggests:
2727
knitr,
2828
rmarkdown,
29-
scorz,
29+
scorz (>= 0.0.0.9062),
3030
testthat
3131
VignetteBuilder: knitr
3232
Imports:
@@ -57,8 +57,9 @@ Imports:
5757
Depends:
5858
R (>= 2.10)
5959
LazyData: true
60-
Remotes:
60+
Remotes:
6161
ready4-dev/ready4,
6262
ready4-dev/ready4use,
6363
ready4-dev/specific,
64-
ready4-dev/youthvars
64+
ready4-dev/youthvars,
65+
ready4-dev/scorz

NAMESPACE

+10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ export(make_predn_metadata_ls)
3939
export(make_sngl_grp_ds)
4040
export(predict_from_mdl_coefs)
4141
export(transform_ds_for_cmprsn)
42+
export(transform_ds_to_drop_msng)
43+
export(transform_ds_to_long)
4244
export(update_col_with_diff)
4345
export(update_multpl_cols_with_diffs)
4446
import(methods)
@@ -60,6 +62,7 @@ importFrom(dplyr,everything)
6062
importFrom(dplyr,filter)
6163
importFrom(dplyr,group_by)
6264
importFrom(dplyr,lag)
65+
importFrom(dplyr,left_join)
6366
importFrom(dplyr,mutate)
6467
importFrom(dplyr,n)
6568
importFrom(dplyr,pull)
@@ -91,6 +94,8 @@ importFrom(purrr,pmap_dfr)
9194
importFrom(purrr,reduce)
9295
importFrom(purrr,walk)
9396
importFrom(ready4,get_from_lup_obj)
97+
importFrom(ready4,ingest)
98+
importFrom(ready4use,Ready4useRepos)
9499
importFrom(ready4use,add_labels_from_dictionary)
95100
importFrom(rlang,exec)
96101
importFrom(rlang,sym)
@@ -102,10 +107,15 @@ importFrom(stats,na.omit)
102107
importFrom(stats,rgamma)
103108
importFrom(stats,rnorm)
104109
importFrom(stats,setNames)
110+
importFrom(stringi,stri_replace_first_fixed)
111+
importFrom(stringi,stri_replace_last_fixed)
105112
importFrom(stringr,str_detect)
113+
importFrom(stringr,str_remove_all)
106114
importFrom(stringr,str_replace)
107115
importFrom(tibble,as_tibble)
116+
importFrom(tibble,rowid_to_column)
108117
importFrom(tibble,tibble)
118+
importFrom(tidyr,pivot_longer)
109119
importFrom(tidyr,pivot_wider)
110120
importFrom(tidyselect,all_of)
111121
importFrom(truncnorm,rtruncnorm)

R/fn_get.R

+11-10
Original file line numberDiff line numberDiff line change
@@ -153,27 +153,28 @@ get_mdl_ds_url <- function (mdls_lup, mdl_nm_1L_chr)
153153
#' @description get_mdl_from_dv() is a Get function that extracts data from an object. Specifically, this function implements an algorithm to get model from dataverse. The function returns Model (a model).
154154
#' @param mdl_nm_1L_chr Model name (a character vector of length one)
155155
#' @param dv_ds_nm_1L_chr Dataverse dataset name (a character vector of length one), Default: 'https://doi.org/10.7910/DVN/JC6PTV'
156+
#' @param dv_nm_1L_chr Dataverse name (a character vector of length one), Default: 'TTU'
156157
#' @param server_1L_chr Server (a character vector of length one), Default: 'dataverse.harvard.edu'
157158
#' @param key_1L_chr Key (a character vector of length one), Default: NULL
158159
#' @return Model (a model)
159160
#' @rdname get_mdl_from_dv
160161
#' @export
161-
#' @importFrom dataverse dataset_files
162-
#' @importFrom purrr map_chr
162+
#' @importFrom ready4use Ready4useRepos
163+
#' @importFrom ready4 ingest
164+
#' @importFrom purrr pluck
163165
get_mdl_from_dv <- function (mdl_nm_1L_chr, dv_ds_nm_1L_chr = "https://doi.org/10.7910/DVN/JC6PTV",
164-
server_1L_chr = "dataverse.harvard.edu", key_1L_chr = NULL)
166+
dv_nm_1L_chr = "TTU", server_1L_chr = "dataverse.harvard.edu",
167+
key_1L_chr = NULL)
165168
{
166-
ds_ls <- dataverse::dataset_files(dv_ds_nm_1L_chr, server = server_1L_chr,
167-
key = key_1L_chr)
168-
all_mdls_chr <- purrr::map_chr(ds_ls, ~.x$label)
169-
idx_1L_int <- which(all_mdls_chr == paste0(mdl_nm_1L_chr,
170-
".RDS"))
169+
X <- ready4use::Ready4useRepos(dv_nm_1L_chr = dv_nm_1L_chr,
170+
dv_server_1L_chr = server_1L_chr, dv_ds_nm_1L_chr = dv_ds_nm_1L_chr)
171+
contents_ls <- ready4::ingest(X, metadata_1L_lgl = F)
172+
idx_1L_int <- which(names(contents_ls) == mdl_nm_1L_chr)
171173
if (identical(idx_1L_int, integer(0))) {
172174
model_mdl <- NULL
173175
}
174176
else {
175-
model_mdl <- readRDS(url(paste0("https://dataverse.harvard.edu/api/access/datafile/",
176-
ds_ls[[idx_1L_int]]$dataFile$id)))
177+
model_mdl <- contents_ls %>% purrr::pluck(idx_1L_int)
177178
}
178179
return(model_mdl)
179180
}

R/fn_transform.R

+93
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,96 @@ transform_ds_for_cmprsn <- function (ds_tb, cmprsn_var_nm_1L_chr, id_var_nm_1L_c
3030
cmprsn_groups_chr)
3131
return(ds_tb)
3232
}
33+
#' Transform dataset to drop missing
34+
#' @description transform_ds_to_drop_msng() is a Transform function that edits an object in such a way that core object attributes - e.g. shape, dimensions, elements, type - are altered. Specifically, this function implements an algorithm to transform dataset to drop missing. The function returns Dataset (a tibble).
35+
#' @param ds_tb Dataset (a tibble)
36+
#' @param predictors_chr Predictors (a character vector)
37+
#' @param uid_var_nm_1L_chr Unique identifier variable name (a character vector of length one), Default: 'UID_chr'
38+
#' @return Dataset (a tibble)
39+
#' @rdname transform_ds_to_drop_msng
40+
#' @export
41+
#' @importFrom dplyr pull filter
42+
#' @importFrom rlang sym
43+
#' @keywords internal
44+
transform_ds_to_drop_msng <- function (ds_tb, predictors_chr, uid_var_nm_1L_chr = "UID_chr")
45+
{
46+
drop_chr <- ds_tb[rowSums(is.na(ds_tb[predictors_chr])) >
47+
0, ] %>% dplyr::pull(!!rlang::sym(uid_var_nm_1L_chr))
48+
ds_tb <- dplyr::filter(ds_tb, !(!!rlang::sym(uid_var_nm_1L_chr) %in%
49+
drop_chr))
50+
return(ds_tb)
51+
}
52+
#' Transform dataset to long
53+
#' @description transform_ds_to_long() is a Transform function that edits an object in such a way that core object attributes - e.g. shape, dimensions, elements, type - are altered. Specifically, this function implements an algorithm to transform dataset to long. The function returns Dataset (a tibble).
54+
#' @param ds_tb Dataset (a tibble)
55+
#' @param predictors_chr Predictors (a character vector)
56+
#' @param drop_underscore_1L_lgl Drop underscore (a logical vector of length one), Default: T
57+
#' @param msrmnt_date_var_nm_1L_chr Measurement date variable name (a character vector of length one), Default: 'date_dtm'
58+
#' @param round_var_nm_1L_chr Round variable name (a character vector of length one), Default: 'Timepoint_chr'
59+
#' @param row_id_nm_1L_chr Row identity name (a character vector of length one), Default: 'case_id'
60+
#' @param time_is_sfx_1L_lgl Time is suffix (a logical vector of length one), Default: T
61+
#' @return Dataset (a tibble)
62+
#' @rdname transform_ds_to_long
63+
#' @export
64+
#' @importFrom stringi stri_replace_first_fixed stri_replace_last_fixed
65+
#' @importFrom purrr map map_lgl map_chr flatten_chr reduce
66+
#' @importFrom stats setNames
67+
#' @importFrom stringr str_remove_all
68+
#' @importFrom tidyr pivot_longer
69+
#' @importFrom tibble rowid_to_column
70+
#' @importFrom dplyr mutate select left_join
71+
#' @importFrom rlang sym
72+
#' @importFrom tidyselect all_of
73+
#' @keywords internal
74+
transform_ds_to_long <- function (ds_tb, predictors_chr, drop_underscore_1L_lgl = T,
75+
msrmnt_date_var_nm_1L_chr = "date_dtm", round_var_nm_1L_chr = "Timepoint_chr",
76+
row_id_nm_1L_chr = "case_id", time_is_sfx_1L_lgl = T)
77+
{
78+
names_chr <- names(ds_tb)
79+
if (time_is_sfx_1L_lgl) {
80+
crop_fn <- stringi::stri_replace_first_fixed
81+
select_fn <- startsWith
82+
}
83+
else {
84+
crop_fn <- stringi::stri_replace_last_fixed
85+
select_fn <- endsWith
86+
}
87+
predictors_ls <- predictors_chr %>% purrr::map(~{
88+
predictor_1L_chr <- .x
89+
predictor_vars_chr <- names_chr[names_chr %>% purrr::map_lgl(~{
90+
name_1L_chr <- .x
91+
select_fn(name_1L_chr, predictor_1L_chr)
92+
})]
93+
}) %>% stats::setNames(predictors_chr)
94+
prefixes_chr <- suffixes_chr <- character(0)
95+
extensions_chr <- predictors_ls[[1]] %>% purrr::map_chr(~crop_fn(.x,
96+
pattern = names(predictors_ls)[1], replacement = ""))
97+
if (time_is_sfx_1L_lgl) {
98+
suffixes_chr <- extensions_chr
99+
}
100+
else {
101+
prefixes_chr <- extensions_chr
102+
}
103+
if (drop_underscore_1L_lgl) {
104+
tfmn_fn <- function(x) {
105+
stringr::str_remove_all(x, "_")
106+
}
107+
}
108+
else {
109+
tfmn_fn <- identity
110+
}
111+
predictor_vars_chr <- predictors_ls %>% purrr::flatten_chr()
112+
other_vars_chr <- setdiff(names_chr, c(paste0(prefixes_chr,
113+
msrmnt_date_var_nm_1L_chr, suffixes_chr), predictor_vars_chr))
114+
ds_tb <- c(msrmnt_date_var_nm_1L_chr, predictors_chr) %>%
115+
purrr::map(~ds_tb %>% tidyr::pivot_longer(cols = paste0(prefixes_chr,
116+
.x, suffixes_chr), names_to = round_var_nm_1L_chr,
117+
values_to = .x) %>% tibble::rowid_to_column(row_id_nm_1L_chr) %>%
118+
dplyr::mutate(`:=`(!!rlang::sym(round_var_nm_1L_chr),
119+
!!rlang::sym(round_var_nm_1L_chr) %>% crop_fn(pattern = .x,
120+
replacement = "") %>% tfmn_fn() %>% factor())) %>%
121+
dplyr::select(tidyselect::all_of(c(row_id_nm_1L_chr,
122+
other_vars_chr, round_var_nm_1L_chr, .x)))) %>%
123+
purrr::reduce(~dplyr::left_join(.x, .y)) %>% dplyr::select(-tidyselect::all_of(row_id_nm_1L_chr))
124+
return(ds_tb)
125+
}

data-raw/DATASET.R

+1-3
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ z <- ready4pack::make_pt_ready4pack_manifest(x,
5555
pkg_ds_ls_ls = datasets_ls) %>%
5656
ready4pack::ready4pack_manifest()
5757
z <- ready4::author(z)
58-
usethis::use_dev_package("scorz",
59-
type = "Suggests",
60-
remote = "ready4-dev/scorz")
58+
usethis::use_dev_package("scorz", type = "Suggests", remote = "ready4-dev/scorz")
6159
ready4::write_extra_pkgs_to_actions(path_to_dir_1L_chr = ".github/workflows", consent_1L_chr = "Y")
6260
write_to_edit_workflow("pkgdown.yaml", consent_1L_chr = "Y") # In other packages, run for "test-coverage.yaml" as well.
6361
readLines("_pkgdown.yml") %>%

0 commit comments

Comments
 (0)