2024-11-08 15:13:33 +01:00
|
|
|
#' Wrapper function to get function from character vector referring to function from namespace. Passed to 'do.call()'
|
|
|
|
#'
|
|
|
|
#' @description
|
|
|
|
#' This function follows the idea from this comment: https://stackoverflow.com/questions/38983179/do-call-a-function-in-r-without-loading-the-package
|
|
|
|
#' @param x function or function name
|
|
|
|
#'
|
|
|
|
#' @return function or character vector
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @examples
|
|
|
|
#' getfun("stats::lm")
|
|
|
|
getfun <- function(x) {
|
2024-12-09 14:00:44 +01:00
|
|
|
if ("character" %in% class(x)) {
|
2024-11-08 15:13:33 +01:00
|
|
|
if (length(grep("::", x)) > 0) {
|
|
|
|
parts <- strsplit(x, "::")[[1]]
|
|
|
|
requireNamespace(parts[1])
|
|
|
|
getExportedValue(parts[1], parts[2])
|
|
|
|
}
|
2024-12-09 14:00:44 +01:00
|
|
|
} else {
|
2024-11-08 15:13:33 +01:00
|
|
|
x
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#' Wrapper to save data in RDS, load into specified qmd and render
|
|
|
|
#'
|
|
|
|
#' @param data list to pass to qmd
|
2024-11-22 10:53:52 +01:00
|
|
|
#' @param ... Passed to `quarto::quarto_render()`
|
2024-11-08 15:13:33 +01:00
|
|
|
#'
|
2024-11-22 10:53:52 +01:00
|
|
|
#' @return output file name
|
2024-11-08 15:13:33 +01:00
|
|
|
#' @export
|
|
|
|
#'
|
2025-03-24 14:40:30 +01:00
|
|
|
write_quarto <- function(data, ...) {
|
2024-11-22 10:53:52 +01:00
|
|
|
# Exports data to temporary location
|
|
|
|
#
|
|
|
|
# I assume this is more secure than putting it in the www folder and deleting
|
|
|
|
# on session end
|
2025-01-23 08:44:38 +01:00
|
|
|
|
|
|
|
# temp <- base::tempfile(fileext = ".rds")
|
|
|
|
# readr::write_rds(data, file = here)
|
|
|
|
|
|
|
|
readr::write_rds(data, file = "www/web_data.rds")
|
2024-11-08 15:13:33 +01:00
|
|
|
|
2024-11-22 10:53:52 +01:00
|
|
|
## Specifying a output path will make the rendering fail
|
|
|
|
## Ref: https://github.com/quarto-dev/quarto-cli/discussions/4041
|
|
|
|
## Outputs to the same as the .qmd file
|
2024-12-09 14:00:44 +01:00
|
|
|
quarto::quarto_render(
|
2025-01-23 08:44:38 +01:00
|
|
|
execute_params = list(data.file = "web_data.rds"),
|
|
|
|
# execute_params = list(data.file = temp),
|
2024-12-09 14:00:44 +01:00
|
|
|
...
|
2024-11-08 15:13:33 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2025-03-24 14:40:30 +01:00
|
|
|
write_rmd <- function(data, ...) {
|
2025-01-23 13:21:41 +01:00
|
|
|
# Exports data to temporary location
|
|
|
|
#
|
|
|
|
# I assume this is more secure than putting it in the www folder and deleting
|
|
|
|
# on session end
|
|
|
|
|
|
|
|
# temp <- base::tempfile(fileext = ".rds")
|
|
|
|
# readr::write_rds(data, file = here)
|
|
|
|
|
|
|
|
readr::write_rds(data, file = "www/web_data.rds")
|
|
|
|
|
|
|
|
## Specifying a output path will make the rendering fail
|
|
|
|
## Ref: https://github.com/quarto-dev/quarto-cli/discussions/4041
|
|
|
|
## Outputs to the same as the .qmd file
|
|
|
|
rmarkdown::render(
|
|
|
|
params = list(data.file = "web_data.rds"),
|
|
|
|
# execute_params = list(data.file = temp),
|
|
|
|
...
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2024-11-08 15:13:33 +01:00
|
|
|
#' Flexible file import based on extension
|
|
|
|
#'
|
|
|
|
#' @param file file name
|
|
|
|
#' @param consider.na character vector of strings to consider as NAs
|
|
|
|
#'
|
|
|
|
#' @return tibble
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @examples
|
|
|
|
#' read_input("https://raw.githubusercontent.com/agdamsbo/cognitive.index.lookup/main/data/sample.csv")
|
|
|
|
read_input <- function(file, consider.na = c("NA", '""', "")) {
|
2024-11-21 12:34:27 +01:00
|
|
|
ext <- tools::file_ext(file)
|
2024-11-08 15:13:33 +01:00
|
|
|
|
|
|
|
if (ext == "csv") {
|
|
|
|
df <- readr::read_csv(file = file, na = consider.na)
|
|
|
|
} else if (ext %in% c("xls", "xlsx")) {
|
|
|
|
df <- openxlsx2::read_xlsx(file = file, na.strings = consider.na)
|
|
|
|
} else if (ext == "dta") {
|
|
|
|
df <- haven::read_dta(file = file)
|
|
|
|
} else if (ext == "ods") {
|
2024-11-21 12:34:27 +01:00
|
|
|
df <- readODS::read_ods(path = file)
|
|
|
|
} else if (ext == "rds") {
|
|
|
|
df <- readr::read_rds(file = file)
|
2024-11-08 15:13:33 +01:00
|
|
|
} else {
|
|
|
|
stop("Input file format has to be on of:
|
2024-11-21 12:34:27 +01:00
|
|
|
'.csv', '.xls', '.xlsx', '.dta', '.ods' or '.rds'")
|
2024-11-08 15:13:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
df
|
|
|
|
}
|
|
|
|
|
|
|
|
#' Convert string of arguments to list of arguments
|
|
|
|
#'
|
|
|
|
#' @description
|
|
|
|
#' Idea from the answer: https://stackoverflow.com/a/62979238
|
|
|
|
#'
|
|
|
|
#' @param string string to convert to list to use with do.call
|
|
|
|
#'
|
|
|
|
#' @return list
|
|
|
|
#' @export
|
|
|
|
#'
|
2024-12-09 14:00:44 +01:00
|
|
|
argsstring2list <- function(string) {
|
2024-11-08 15:13:33 +01:00
|
|
|
eval(parse(text = paste0("list(", string, ")")))
|
|
|
|
}
|
2024-11-22 11:48:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
#' Factorize variables in data.frame
|
|
|
|
#'
|
|
|
|
#' @param data data.frame
|
|
|
|
#' @param vars variables to force factorize
|
|
|
|
#'
|
|
|
|
#' @return data.frame
|
|
|
|
#' @export
|
2024-12-09 14:00:44 +01:00
|
|
|
factorize <- function(data, vars) {
|
2024-11-22 11:48:08 +01:00
|
|
|
if (!is.null(vars)) {
|
|
|
|
data |>
|
|
|
|
dplyr::mutate(
|
|
|
|
dplyr::across(
|
|
|
|
dplyr::all_of(vars),
|
2024-12-19 21:21:29 +01:00
|
|
|
REDCapCAST::as_factor
|
2024-11-22 11:48:08 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
data
|
|
|
|
}
|
|
|
|
}
|
2024-11-27 11:55:01 +01:00
|
|
|
|
|
|
|
dummy_Imports <- function() {
|
|
|
|
list(
|
|
|
|
MASS::as.fractions(),
|
|
|
|
broom::augment(),
|
|
|
|
broom.helpers::all_categorical(),
|
2024-11-28 21:02:23 +01:00
|
|
|
here::here(),
|
|
|
|
cardx::all_of(),
|
|
|
|
parameters::ci(),
|
|
|
|
DT::addRow(),
|
|
|
|
bslib::accordion()
|
2024-12-09 14:00:44 +01:00
|
|
|
)
|
|
|
|
# https://github.com/hadley/r-pkgs/issues/828
|
|
|
|
}
|
2024-12-04 12:58:55 +01:00
|
|
|
|
|
|
|
|
2024-12-19 15:26:23 +01:00
|
|
|
#' Title
|
|
|
|
#'
|
|
|
|
#' @param data data
|
|
|
|
#' @param output.format output
|
|
|
|
#' @param filename filename
|
|
|
|
#' @param ... passed on
|
|
|
|
#'
|
|
|
|
#' @returns data
|
|
|
|
#' @export
|
|
|
|
#'
|
2024-12-09 14:00:44 +01:00
|
|
|
file_export <- function(data, output.format = c("df", "teal", "list"), filename, ...) {
|
2024-12-04 12:58:55 +01:00
|
|
|
output.format <- match.arg(output.format)
|
|
|
|
|
2024-12-09 14:00:44 +01:00
|
|
|
filename <- gsub("-", "_", filename)
|
2024-12-04 12:58:55 +01:00
|
|
|
|
2024-12-09 14:00:44 +01:00
|
|
|
if (output.format == "teal") {
|
2024-12-04 12:58:55 +01:00
|
|
|
out <- within(
|
|
|
|
teal_data(),
|
|
|
|
{
|
2024-12-09 14:00:44 +01:00
|
|
|
assign(name, value |>
|
2025-01-15 16:21:38 +01:00
|
|
|
dplyr::bind_cols(.name_repair = "unique_quiet") |>
|
2024-12-19 21:21:29 +01:00
|
|
|
default_parsing())
|
2024-12-04 12:58:55 +01:00
|
|
|
},
|
|
|
|
value = data,
|
|
|
|
name = filename
|
|
|
|
)
|
|
|
|
|
|
|
|
datanames(out) <- filename
|
2024-12-09 14:00:44 +01:00
|
|
|
} else if (output.format == "df") {
|
2024-12-19 21:21:29 +01:00
|
|
|
out <- data |>
|
|
|
|
default_parsing()
|
2024-12-09 14:00:44 +01:00
|
|
|
} else if (output.format == "list") {
|
|
|
|
out <- list(
|
|
|
|
data = data,
|
|
|
|
name = filename
|
|
|
|
)
|
|
|
|
|
2024-12-19 21:21:29 +01:00
|
|
|
out <- c(out, ...)
|
2024-12-04 12:58:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
out
|
|
|
|
}
|
2024-12-19 11:33:19 +01:00
|
|
|
|
|
|
|
|
|
|
|
#' Default data parsing
|
|
|
|
#'
|
2024-12-19 21:21:29 +01:00
|
|
|
#' @param data data
|
2024-12-19 11:33:19 +01:00
|
|
|
#'
|
|
|
|
#' @returns data.frame or tibble
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @examples
|
|
|
|
#' mtcars |> str()
|
2024-12-19 21:21:29 +01:00
|
|
|
#' mtcars |>
|
|
|
|
#' default_parsing() |>
|
|
|
|
#' str()
|
|
|
|
default_parsing <- function(data) {
|
2025-03-24 14:40:30 +01:00
|
|
|
name_labels <- lapply(data, \(.x) REDCapCAST::get_attr(.x, attr = "label"))
|
2025-01-15 16:21:38 +01:00
|
|
|
|
|
|
|
out <- data |>
|
2024-12-19 21:21:29 +01:00
|
|
|
REDCapCAST::parse_data() |>
|
|
|
|
REDCapCAST::as_factor() |>
|
2025-03-24 14:40:30 +01:00
|
|
|
REDCapCAST::numchar2fct(numeric.threshold = 8, character.throshold = 10) |>
|
2025-03-19 13:10:56 +01:00
|
|
|
REDCapCAST::as_logical() |>
|
|
|
|
REDCapCAST::fct_drop()
|
2025-01-15 16:21:38 +01:00
|
|
|
|
2025-03-24 14:40:30 +01:00
|
|
|
purrr::map2(out, name_labels, \(.x, .l){
|
|
|
|
if (!(is.na(.l) | .l == "")) {
|
2025-01-15 16:21:38 +01:00
|
|
|
REDCapCAST::set_attr(.x, .l, attr = "label")
|
|
|
|
} else {
|
|
|
|
attr(x = .x, which = "label") <- NULL
|
|
|
|
.x
|
|
|
|
}
|
|
|
|
# REDCapCAST::set_attr(data = .x, label = .l,attr = "label", overwrite = FALSE)
|
|
|
|
}) |> dplyr::bind_cols()
|
|
|
|
}
|
|
|
|
|
|
|
|
#' Remove NA labels
|
|
|
|
#'
|
|
|
|
#' @param data data
|
|
|
|
#'
|
|
|
|
#' @returns data.frame
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @examples
|
2025-03-24 14:40:30 +01:00
|
|
|
#' ds <- mtcars |> lapply(\(.x) REDCapCAST::set_attr(.x, label = NA, attr = "label"))
|
|
|
|
#' ds |>
|
|
|
|
#' remove_na_attr() |>
|
|
|
|
#' str()
|
|
|
|
remove_na_attr <- function(data, attr = "label") {
|
2025-01-15 16:21:38 +01:00
|
|
|
out <- data |> lapply(\(.x){
|
2025-03-24 14:40:30 +01:00
|
|
|
ls <- REDCapCAST::get_attr(data = .x, attr = attr)
|
|
|
|
if (is.na(ls) | ls == "") {
|
2025-01-15 16:21:38 +01:00
|
|
|
attr(x = .x, which = attr) <- NULL
|
|
|
|
}
|
|
|
|
.x
|
|
|
|
})
|
|
|
|
|
|
|
|
dplyr::bind_cols(out)
|
2024-12-19 11:33:19 +01:00
|
|
|
}
|
2025-02-07 16:24:09 +01:00
|
|
|
|
|
|
|
#' Removes columns with completenes below cutoff
|
|
|
|
#'
|
|
|
|
#' @param data data frame
|
|
|
|
#' @param cutoff numeric
|
|
|
|
#'
|
|
|
|
#' @returns data frame
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @examples
|
2025-03-24 14:40:30 +01:00
|
|
|
#' data.frame(a = 1:10, b = NA, c = c(2, NA)) |> remove_empty_cols(cutoff = .5)
|
|
|
|
remove_empty_cols <- function(data, cutoff = .7) {
|
|
|
|
filter <- apply(X = data, MARGIN = 2, FUN = \(.x){
|
|
|
|
sum(as.numeric(!is.na(.x))) / length(.x)
|
2025-02-07 16:24:09 +01:00
|
|
|
}) >= cutoff
|
|
|
|
data[filter]
|
|
|
|
}
|
2025-02-25 09:51:42 +01:00
|
|
|
|
|
|
|
|
|
|
|
#' Append list with named index
|
|
|
|
#'
|
|
|
|
#' @param data data to add to list
|
|
|
|
#' @param list list
|
|
|
|
#' @param index index name
|
|
|
|
#'
|
|
|
|
#' @returns list
|
2025-03-19 13:10:56 +01:00
|
|
|
#' @export
|
2025-02-25 09:51:42 +01:00
|
|
|
#'
|
|
|
|
#' @examples
|
2025-03-24 14:40:30 +01:00
|
|
|
#' ls_d <- list(test = c(1:20))
|
2025-02-25 09:51:42 +01:00
|
|
|
#' ls_d <- list()
|
2025-03-24 14:40:30 +01:00
|
|
|
#' data.frame(letters[1:20], 1:20) |> append_list(ls_d, "letters")
|
|
|
|
#' letters[1:20] |> append_list(ls_d, "letters")
|
|
|
|
append_list <- function(data, list, index) {
|
2025-02-25 09:51:42 +01:00
|
|
|
## This will overwrite and not warn
|
|
|
|
## Not very safe, but convenient to append code to list
|
2025-03-24 14:40:30 +01:00
|
|
|
if (index %in% names(list)) {
|
2025-02-25 09:51:42 +01:00
|
|
|
list[[index]] <- data
|
|
|
|
out <- list
|
|
|
|
} else {
|
2025-03-24 14:40:30 +01:00
|
|
|
out <- setNames(c(list, list(data)), c(names(list), index))
|
2025-02-25 09:51:42 +01:00
|
|
|
}
|
|
|
|
out
|
|
|
|
}
|
2025-03-12 18:27:46 +01:00
|
|
|
|
|
|
|
|
|
|
|
#' Get missingsness fraction
|
|
|
|
#'
|
|
|
|
#' @param data data
|
|
|
|
#'
|
|
|
|
#' @returns numeric vector
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @examples
|
2025-03-24 14:40:30 +01:00
|
|
|
#' c(NA, 1:10, rep(NA, 3)) |> missing_fraction()
|
|
|
|
missing_fraction <- function(data) {
|
|
|
|
NROW(data[is.na(data)]) / NROW(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#' Ultra short data dascription
|
|
|
|
#'
|
|
|
|
#' @param data
|
|
|
|
#'
|
|
|
|
#' @returns character vector
|
|
|
|
#' @export
|
|
|
|
#'
|
|
|
|
#' @examples
|
|
|
|
#' data.frame(
|
|
|
|
#' sample(1:8, 20, TRUE),
|
|
|
|
#' sample(c(1:8, NA), 20, TRUE)
|
|
|
|
#' ) |> data_description()
|
|
|
|
data_description <- function(data) {
|
|
|
|
data <- if (shiny::is.reactive(data)) data() else data
|
|
|
|
|
2025-03-26 12:07:28 +01:00
|
|
|
n <- nrow(data)
|
|
|
|
n_var <- ncol(data)
|
|
|
|
n_complete <- sum(complete.cases(data))
|
|
|
|
p_complete <- n_complete/n
|
|
|
|
|
2025-03-24 14:40:30 +01:00
|
|
|
sprintf(
|
|
|
|
i18n("Data has %s observations and %s variables, with %s (%s%%) complete cases"),
|
2025-03-26 12:07:28 +01:00
|
|
|
n,
|
|
|
|
n_var,
|
|
|
|
n_complete,
|
|
|
|
signif(100 * p_complete, 3)
|
2025-03-24 14:40:30 +01:00
|
|
|
)
|
2025-03-12 18:27:46 +01:00
|
|
|
}
|