mirror of
https://github.com/agdamsbo/FreesearchR.git
synced 2025-09-12 01:49:39 +02:00
plots new accept pri, sec and ter arguments instead of x,y,z to avoid confusion. tests, tests, tests
This commit is contained in:
parent
e463fa0670
commit
652a8ca1b7
28 changed files with 3274 additions and 178 deletions
|
@ -80,7 +80,8 @@ Suggests:
|
||||||
rsconnect,
|
rsconnect,
|
||||||
knitr,
|
knitr,
|
||||||
rmarkdown,
|
rmarkdown,
|
||||||
testthat (>= 3.0.0)
|
testthat (>= 3.0.0),
|
||||||
|
shinytest
|
||||||
URL: https://github.com/agdamsbo/FreesearchR, https://agdamsbo.github.io/FreesearchR/
|
URL: https://github.com/agdamsbo/FreesearchR, https://agdamsbo.github.io/FreesearchR/
|
||||||
BugReports: https://github.com/agdamsbo/FreesearchR/issues
|
BugReports: https://github.com/agdamsbo/FreesearchR/issues
|
||||||
VignetteBuilder: knitr
|
VignetteBuilder: knitr
|
||||||
|
|
|
@ -5,8 +5,8 @@ S3method(cut_var,hms)
|
||||||
S3method(plot,tbl_regression)
|
S3method(plot,tbl_regression)
|
||||||
export(add_class_icon)
|
export(add_class_icon)
|
||||||
export(add_sparkline)
|
export(add_sparkline)
|
||||||
|
export(align_axes)
|
||||||
export(all_but)
|
export(all_but)
|
||||||
export(allign_axes)
|
|
||||||
export(append_column)
|
export(append_column)
|
||||||
export(append_list)
|
export(append_list)
|
||||||
export(argsstring2list)
|
export(argsstring2list)
|
||||||
|
@ -95,7 +95,6 @@ export(regression_model_uv_list)
|
||||||
export(regression_table)
|
export(regression_table)
|
||||||
export(remove_empty_attr)
|
export(remove_empty_attr)
|
||||||
export(remove_empty_cols)
|
export(remove_empty_cols)
|
||||||
export(remove_na_attr)
|
|
||||||
export(remove_nested_list)
|
export(remove_nested_list)
|
||||||
export(repeated_instruments)
|
export(repeated_instruments)
|
||||||
export(sankey_ready)
|
export(sankey_ready)
|
||||||
|
|
|
@ -46,7 +46,8 @@ data_correlations_server <- function(id,
|
||||||
} else {
|
} else {
|
||||||
out <- data()
|
out <- data()
|
||||||
}
|
}
|
||||||
out |> dplyr::mutate(dplyr::across(tidyselect::everything(),as.numeric))
|
# out |> dplyr::mutate(dplyr::across(tidyselect::everything(),as.numeric))
|
||||||
|
sapply(data,as.numeric)
|
||||||
# as.numeric()
|
# as.numeric()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -100,8 +101,9 @@ data_correlations_server <- function(id,
|
||||||
}
|
}
|
||||||
|
|
||||||
correlation_pairs <- function(data, threshold = .8) {
|
correlation_pairs <- function(data, threshold = .8) {
|
||||||
data <- data[!sapply(data, is.character)]
|
data <- as.data.frame(data)[!sapply(as.data.frame(data), is.character)]
|
||||||
data <- data |> dplyr::mutate(dplyr::across(dplyr::where(is.factor), as.numeric))
|
data <- sapply(data,\(.x)if (is.factor(.x)) as.numeric(.x) else .x) |> as.data.frame()
|
||||||
|
# data <- data |> dplyr::mutate(dplyr::across(dplyr::where(is.factor), as.numeric))
|
||||||
cor <- Hmisc::rcorr(as.matrix(data))
|
cor <- Hmisc::rcorr(as.matrix(data))
|
||||||
r <- cor$r %>% as.table()
|
r <- cor$r %>% as.table()
|
||||||
d <- r |>
|
d <- r |>
|
||||||
|
|
145
R/data_plots.R
145
R/data_plots.R
|
@ -88,7 +88,7 @@ data_visuals_ui <- function(id, tab_title = "Plots", ...) {
|
||||||
),
|
),
|
||||||
bslib::nav_panel(
|
bslib::nav_panel(
|
||||||
title = tab_title,
|
title = tab_title,
|
||||||
shiny::plotOutput(ns("plot"),height = "70vh"),
|
shiny::plotOutput(ns("plot"), height = "70vh"),
|
||||||
shiny::tags$br(),
|
shiny::tags$br(),
|
||||||
shiny::tags$br(),
|
shiny::tags$br(),
|
||||||
shiny::htmlOutput(outputId = ns("code_plot"))
|
shiny::htmlOutput(outputId = ns("code_plot"))
|
||||||
|
@ -115,7 +115,7 @@ data_visuals_server <- function(id,
|
||||||
rv <- shiny::reactiveValues(
|
rv <- shiny::reactiveValues(
|
||||||
plot.params = NULL,
|
plot.params = NULL,
|
||||||
plot = NULL,
|
plot = NULL,
|
||||||
code=NULL
|
code = NULL
|
||||||
)
|
)
|
||||||
|
|
||||||
# ## --- New attempt
|
# ## --- New attempt
|
||||||
|
@ -216,7 +216,7 @@ data_visuals_server <- function(id,
|
||||||
shiny::req(data())
|
shiny::req(data())
|
||||||
columnSelectInput(
|
columnSelectInput(
|
||||||
inputId = ns("primary"),
|
inputId = ns("primary"),
|
||||||
col_subset=names(data())[sapply(data(),data_type)!="text"],
|
col_subset = names(data())[sapply(data(), data_type) != "text"],
|
||||||
data = data,
|
data = data,
|
||||||
placeholder = "Select variable",
|
placeholder = "Select variable",
|
||||||
label = "Response variable",
|
label = "Response variable",
|
||||||
|
@ -318,37 +318,30 @@ data_visuals_server <- function(id,
|
||||||
|
|
||||||
shiny::observeEvent(input$act_plot,
|
shiny::observeEvent(input$act_plot,
|
||||||
{
|
{
|
||||||
if (NROW(data())>0){
|
if (NROW(data()) > 0) {
|
||||||
tryCatch(
|
tryCatch(
|
||||||
{
|
{
|
||||||
parameters <- list(
|
parameters <- list(
|
||||||
type = rv$plot.params()[["fun"]],
|
type = rv$plot.params()[["fun"]],
|
||||||
x = input$primary,
|
pri = input$primary,
|
||||||
y = input$secondary,
|
sec = input$secondary,
|
||||||
z = input$tertiary
|
ter = input$tertiary
|
||||||
)
|
)
|
||||||
|
|
||||||
shiny::withProgress(message = "Drawing the plot. Hold tight for a moment..", {
|
shiny::withProgress(message = "Drawing the plot. Hold tight for a moment..", {
|
||||||
rv$plot <- rlang::exec(create_plot, !!!append_list(data(),parameters,"data"))
|
rv$plot <- rlang::exec(create_plot, !!!append_list(data(), parameters, "data"))
|
||||||
# rv$plot <- create_plot(
|
})
|
||||||
# data = data(),
|
|
||||||
# type = rv$plot.params()[["fun"]],
|
|
||||||
# x = input$primary,
|
|
||||||
# y = input$secondary,
|
|
||||||
# z = input$tertiary
|
|
||||||
# )
|
|
||||||
})
|
|
||||||
|
|
||||||
rv$code <- glue::glue("FreesearchR::create_plot(data,{list2str(parameters)})")
|
rv$code <- glue::glue("FreesearchR::create_plot(data,{list2str(parameters)})")
|
||||||
|
},
|
||||||
},
|
# warning = function(warn) {
|
||||||
# warning = function(warn) {
|
# showNotification(paste0(warn), type = "warning")
|
||||||
# showNotification(paste0(warn), type = "warning")
|
# },
|
||||||
# },
|
error = function(err) {
|
||||||
error = function(err) {
|
showNotification(paste0(err), type = "err")
|
||||||
showNotification(paste0(err), type = "err")
|
}
|
||||||
}
|
)
|
||||||
)}
|
}
|
||||||
},
|
},
|
||||||
ignoreInit = TRUE
|
ignoreInit = TRUE
|
||||||
)
|
)
|
||||||
|
@ -415,7 +408,7 @@ all_but <- function(data, ...) {
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' default_parsing(mtcars) |> subset_types("ordinal")
|
#' default_parsing(mtcars) |> subset_types("ordinal")
|
||||||
#' default_parsing(mtcars) |> subset_types(c("dichotomous", "ordinal" ,"categorical"))
|
#' default_parsing(mtcars) |> subset_types(c("dichotomous", "ordinal", "categorical"))
|
||||||
#' #' default_parsing(mtcars) |> subset_types("factor",class)
|
#' #' default_parsing(mtcars) |> subset_types("factor",class)
|
||||||
subset_types <- function(data, types, type.fun = data_type) {
|
subset_types <- function(data, types, type.fun = data_type) {
|
||||||
data[sapply(data, type.fun) %in% types]
|
data[sapply(data, type.fun) %in% types]
|
||||||
|
@ -450,21 +443,21 @@ supported_plots <- function() {
|
||||||
fun = "plot_hbars",
|
fun = "plot_hbars",
|
||||||
descr = "Stacked horizontal bars",
|
descr = "Stacked horizontal bars",
|
||||||
note = "A classical way of visualising the distribution of an ordinal scale like the modified Ranking Scale and known as Grotta bars",
|
note = "A classical way of visualising the distribution of an ordinal scale like the modified Ranking Scale and known as Grotta bars",
|
||||||
primary.type = c("dichotomous", "ordinal" ,"categorical"),
|
primary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.type = c("dichotomous", "ordinal" ,"categorical"),
|
secondary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.multi = FALSE,
|
secondary.multi = FALSE,
|
||||||
tertiary.type = c("dichotomous", "ordinal" ,"categorical"),
|
tertiary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.extra = "none"
|
secondary.extra = "none"
|
||||||
),
|
),
|
||||||
plot_violin = list(
|
plot_violin = list(
|
||||||
fun = "plot_violin",
|
fun = "plot_violin",
|
||||||
descr = "Violin plot",
|
descr = "Violin plot",
|
||||||
note = "A modern alternative to the classic boxplot to visualise data distribution",
|
note = "A modern alternative to the classic boxplot to visualise data distribution",
|
||||||
primary.type = c("datatime","continuous", "dichotomous", "ordinal" ,"categorical"),
|
primary.type = c("datatime", "continuous", "dichotomous", "ordinal", "categorical"),
|
||||||
secondary.type = c("dichotomous", "ordinal" ,"categorical"),
|
secondary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.multi = FALSE,
|
secondary.multi = FALSE,
|
||||||
secondary.extra = "none",
|
secondary.extra = "none",
|
||||||
tertiary.type = c("dichotomous", "ordinal" ,"categorical")
|
tertiary.type = c("dichotomous", "ordinal", "categorical")
|
||||||
),
|
),
|
||||||
# plot_ridge = list(
|
# plot_ridge = list(
|
||||||
# descr = "Ridge plot",
|
# descr = "Ridge plot",
|
||||||
|
@ -478,30 +471,30 @@ supported_plots <- function() {
|
||||||
fun = "plot_sankey",
|
fun = "plot_sankey",
|
||||||
descr = "Sankey plot",
|
descr = "Sankey plot",
|
||||||
note = "A way of visualising change between groups",
|
note = "A way of visualising change between groups",
|
||||||
primary.type = c("dichotomous", "ordinal" ,"categorical"),
|
primary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.type = c("dichotomous", "ordinal" ,"categorical"),
|
secondary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.multi = FALSE,
|
secondary.multi = FALSE,
|
||||||
secondary.extra = NULL,
|
secondary.extra = NULL,
|
||||||
tertiary.type = c("dichotomous", "ordinal" ,"categorical")
|
tertiary.type = c("dichotomous", "ordinal", "categorical")
|
||||||
),
|
),
|
||||||
plot_scatter = list(
|
plot_scatter = list(
|
||||||
fun = "plot_scatter",
|
fun = "plot_scatter",
|
||||||
descr = "Scatter plot",
|
descr = "Scatter plot",
|
||||||
note = "A classic way of showing the association between to variables",
|
note = "A classic way of showing the association between to variables",
|
||||||
primary.type = c("datatime","continuous"),
|
primary.type = c("datatime", "continuous"),
|
||||||
secondary.type = c("datatime","continuous", "ordinal" ,"categorical"),
|
secondary.type = c("datatime", "continuous", "ordinal", "categorical"),
|
||||||
secondary.multi = FALSE,
|
secondary.multi = FALSE,
|
||||||
tertiary.type = c("dichotomous", "ordinal" ,"categorical"),
|
tertiary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.extra = NULL
|
secondary.extra = NULL
|
||||||
),
|
),
|
||||||
plot_box = list(
|
plot_box = list(
|
||||||
fun = "plot_box",
|
fun = "plot_box",
|
||||||
descr = "Box plot",
|
descr = "Box plot",
|
||||||
note = "A classic way to plot data distribution by groups",
|
note = "A classic way to plot data distribution by groups",
|
||||||
primary.type = c("datatime","continuous", "dichotomous", "ordinal" ,"categorical"),
|
primary.type = c("datatime", "continuous", "dichotomous", "ordinal", "categorical"),
|
||||||
secondary.type = c("dichotomous", "ordinal" ,"categorical"),
|
secondary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.multi = FALSE,
|
secondary.multi = FALSE,
|
||||||
tertiary.type = c("dichotomous", "ordinal" ,"categorical"),
|
tertiary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.extra = "none"
|
secondary.extra = "none"
|
||||||
),
|
),
|
||||||
plot_euler = list(
|
plot_euler = list(
|
||||||
|
@ -512,7 +505,7 @@ supported_plots <- function() {
|
||||||
secondary.type = "dichotomous",
|
secondary.type = "dichotomous",
|
||||||
secondary.multi = TRUE,
|
secondary.multi = TRUE,
|
||||||
secondary.max = 4,
|
secondary.max = 4,
|
||||||
tertiary.type = c("dichotomous", "ordinal" ,"categorical"),
|
tertiary.type = c("dichotomous", "ordinal", "categorical"),
|
||||||
secondary.extra = NULL
|
secondary.extra = NULL
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
@ -591,9 +584,9 @@ get_plot_options <- function(data) {
|
||||||
#' Wrapper to create plot based on provided type
|
#' Wrapper to create plot based on provided type
|
||||||
#'
|
#'
|
||||||
#' @param data data.frame
|
#' @param data data.frame
|
||||||
#' @param x primary variable
|
#' @param pri primary variable
|
||||||
#' @param y secondary variable
|
#' @param sec secondary variable
|
||||||
#' @param z tertiary variable
|
#' @param ter tertiary variable
|
||||||
#' @param type plot type (derived from possible_plots() and matches custom function)
|
#' @param type plot type (derived from possible_plots() and matches custom function)
|
||||||
#' @param ... ignored for now
|
#' @param ... ignored for now
|
||||||
#'
|
#'
|
||||||
|
@ -603,20 +596,36 @@ get_plot_options <- function(data) {
|
||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' create_plot(mtcars, "plot_violin", "mpg", "cyl")
|
#' create_plot(mtcars, "plot_violin", "mpg", "cyl") |> attributes()
|
||||||
create_plot <- function(data, type, x, y, z = NULL, ...) {
|
create_plot <- function(data, type, pri, sec, ter = NULL, ...) {
|
||||||
if (!any(y %in% names(data))) {
|
if (!is.null(sec)) {
|
||||||
y <- NULL
|
if (!any(sec %in% names(data))) {
|
||||||
|
sec <- NULL
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!z %in% names(data)) {
|
if (!is.null(ter)) {
|
||||||
z <- NULL
|
if (!ter %in% names(data)) {
|
||||||
|
ter <- NULL
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
do.call(
|
parameters <- list(
|
||||||
type,
|
pri = pri,
|
||||||
list(data, x, y, z, ...)
|
sec = sec,
|
||||||
|
ter = ter,
|
||||||
|
...
|
||||||
)
|
)
|
||||||
|
|
||||||
|
out <- do.call(
|
||||||
|
type,
|
||||||
|
modifyList(parameters,list(data=data))
|
||||||
|
)
|
||||||
|
|
||||||
|
code <- rlang::call2(type,!!!parameters,.ns = "FreesearchR")
|
||||||
|
|
||||||
|
attr(out,"code") <- code
|
||||||
|
out
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Print label, and if missing print variable name
|
#' Print label, and if missing print variable name
|
||||||
|
@ -666,8 +675,8 @@ get_label <- function(data, var = NULL) {
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' "Lorem ipsum... you know the routine" |> line_break()
|
#' "Lorem ipsum... you know the routine" |> line_break()
|
||||||
#' paste(sample(letters[1:10], 100, TRUE), collapse = "") |> line_break(fixed = TRUE)
|
#' paste(sample(letters[1:10], 100, TRUE), collapse = "") |> line_break(force = TRUE)
|
||||||
line_break <- function(data, lineLength = 20, fixed = FALSE) {
|
line_break <- function(data, lineLength = 20, force = FALSE) {
|
||||||
if (isTRUE(force)) {
|
if (isTRUE(force)) {
|
||||||
gsub(paste0("(.{1,", lineLength, "})(\\s|[[:alnum:]])"), "\\1\n", data)
|
gsub(paste0("(.{1,", lineLength, "})(\\s|[[:alnum:]])"), "\\1\n", data)
|
||||||
} else {
|
} else {
|
||||||
|
@ -698,7 +707,7 @@ wrap_plot_list <- function(data, tag_levels = NULL) {
|
||||||
.x
|
.x
|
||||||
}
|
}
|
||||||
})() |>
|
})() |>
|
||||||
allign_axes() |>
|
align_axes() |>
|
||||||
patchwork::wrap_plots(guides = "collect", axes = "collect", axis_titles = "collect")
|
patchwork::wrap_plots(guides = "collect", axes = "collect", axis_titles = "collect")
|
||||||
if (!is.null(tag_levels)) {
|
if (!is.null(tag_levels)) {
|
||||||
out <- out + patchwork::plot_annotation(tag_levels = tag_levels)
|
out <- out + patchwork::plot_annotation(tag_levels = tag_levels)
|
||||||
|
@ -713,19 +722,21 @@ wrap_plot_list <- function(data, tag_levels = NULL) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#' Alligns axes between plots
|
#' Aligns axes between plots
|
||||||
#'
|
#'
|
||||||
#' @param ... ggplot2 objects or list of ggplot2 objects
|
#' @param ... ggplot2 objects or list of ggplot2 objects
|
||||||
#'
|
#'
|
||||||
#' @returns list of ggplot2 objects
|
#' @returns list of ggplot2 objects
|
||||||
#' @export
|
#' @export
|
||||||
#'
|
#'
|
||||||
allign_axes <- function(...) {
|
align_axes <- function(...) {
|
||||||
# https://stackoverflow.com/questions/62818776/get-axis-limits-from-ggplot-object
|
# https://stackoverflow.com/questions/62818776/get-axis-limits-from-ggplot-object
|
||||||
# https://github.com/thomasp85/patchwork/blob/main/R/plot_multipage.R#L150
|
# https://github.com/thomasp85/patchwork/blob/main/R/plot_multipage.R#L150
|
||||||
if (ggplot2::is.ggplot(..1)) {
|
if (ggplot2::is.ggplot(..1)) {
|
||||||
|
## Assumes list of ggplots
|
||||||
p <- list(...)
|
p <- list(...)
|
||||||
} else if (is.list(..1)) {
|
} else if (is.list(..1)) {
|
||||||
|
## Assumes list with list of ggplots
|
||||||
p <- ..1
|
p <- ..1
|
||||||
} else {
|
} else {
|
||||||
cli::cli_abort("Can only align {.cls ggplot} objects or a list of them")
|
cli::cli_abort("Can only align {.cls ggplot} objects or a list of them")
|
||||||
|
@ -737,7 +748,7 @@ allign_axes <- function(...) {
|
||||||
|
|
||||||
suppressWarnings({
|
suppressWarnings({
|
||||||
p |> purrr::map(~ .x + ggplot2::xlim(xr) + ggplot2::ylim(yr))
|
p |> purrr::map(~ .x + ggplot2::xlim(xr) + ggplot2::ylim(yr))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#' Extract and clean axis ranges
|
#' Extract and clean axis ranges
|
||||||
|
|
27
R/plot_box.R
27
R/plot_box.R
|
@ -6,13 +6,13 @@
|
||||||
#' @name data-plots
|
#' @name data-plots
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' mtcars |> plot_box(x = "mpg", y = "cyl", z = "gear")
|
#' mtcars |> plot_box(pri = "mpg", sec = "cyl", ter = "gear")
|
||||||
#' mtcars |>
|
#' mtcars |>
|
||||||
#' default_parsing() |>
|
#' default_parsing() |>
|
||||||
#' plot_box(x = "mpg", y = "cyl", z = "gear")
|
#' plot_box(pri = "mpg", sec = "cyl", ter = "gear")
|
||||||
plot_box <- function(data, x, y, z = NULL) {
|
plot_box <- function(data, pri, sec, ter = NULL) {
|
||||||
if (!is.null(z)) {
|
if (!is.null(ter)) {
|
||||||
ds <- split(data, data[z])
|
ds <- split(data, data[ter])
|
||||||
} else {
|
} else {
|
||||||
ds <- list(data)
|
ds <- list(data)
|
||||||
}
|
}
|
||||||
|
@ -20,13 +20,12 @@ plot_box <- function(data, x, y, z = NULL) {
|
||||||
out <- lapply(ds, \(.ds){
|
out <- lapply(ds, \(.ds){
|
||||||
plot_box_single(
|
plot_box_single(
|
||||||
data = .ds,
|
data = .ds,
|
||||||
x = x,
|
pri = pri,
|
||||||
y = y
|
sec = sec
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
wrap_plot_list(out)
|
wrap_plot_list(out)
|
||||||
# patchwork::wrap_plots(out,guides = "collect")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -41,18 +40,18 @@ plot_box <- function(data, x, y, z = NULL) {
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' mtcars |> plot_box_single("mpg","cyl")
|
#' mtcars |> plot_box_single("mpg","cyl")
|
||||||
plot_box_single <- function(data, x, y=NULL, seed = 2103) {
|
plot_box_single <- function(data, pri, sec=NULL, seed = 2103) {
|
||||||
set.seed(seed)
|
set.seed(seed)
|
||||||
|
|
||||||
if (is.null(y)) {
|
if (is.null(sec)) {
|
||||||
y <- "All"
|
sec <- "All"
|
||||||
data[[y]] <- y
|
data[[y]] <- sec
|
||||||
}
|
}
|
||||||
|
|
||||||
discrete <- !data_type(data[[y]]) %in% "continuous"
|
discrete <- !data_type(data[[sec]]) %in% "continuous"
|
||||||
|
|
||||||
data |>
|
data |>
|
||||||
ggplot2::ggplot(ggplot2::aes(x = !!dplyr::sym(x), y = !!dplyr::sym(y), fill = !!dplyr::sym(y), group = !!dplyr::sym(y))) +
|
ggplot2::ggplot(ggplot2::aes(x = !!dplyr::sym(pri), y = !!dplyr::sym(sec), fill = !!dplyr::sym(sec), group = !!dplyr::sym(sec))) +
|
||||||
ggplot2::geom_boxplot(linewidth = 1.8, outliers = FALSE) +
|
ggplot2::geom_boxplot(linewidth = 1.8, outliers = FALSE) +
|
||||||
## THis could be optional in future
|
## THis could be optional in future
|
||||||
ggplot2::geom_jitter(color = "black", size = 2, alpha = 0.9, width = 0.1, height = .5) +
|
ggplot2::geom_jitter(color = "black", size = 2, alpha = 0.9, width = 0.1, height = .5) +
|
||||||
|
|
|
@ -76,16 +76,16 @@ ggeulerr <- function(
|
||||||
#' D = sample(c(TRUE, FALSE, FALSE, FALSE), 50, TRUE)
|
#' D = sample(c(TRUE, FALSE, FALSE, FALSE), 50, TRUE)
|
||||||
#' ) |> plot_euler("A", c("B", "C"), "D", seed = 4)
|
#' ) |> plot_euler("A", c("B", "C"), "D", seed = 4)
|
||||||
#' mtcars |> plot_euler("vs", "am", seed = 1)
|
#' mtcars |> plot_euler("vs", "am", seed = 1)
|
||||||
plot_euler <- function(data, x, y, z = NULL, seed = 2103) {
|
plot_euler <- function(data, pri, sec, ter = NULL, seed = 2103) {
|
||||||
set.seed(seed = seed)
|
set.seed(seed = seed)
|
||||||
if (!is.null(z)) {
|
if (!is.null(ter)) {
|
||||||
ds <- split(data, data[z])
|
ds <- split(data, data[ter])
|
||||||
} else {
|
} else {
|
||||||
ds <- list(data)
|
ds <- list(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
out <- lapply(ds, \(.x){
|
out <- lapply(ds, \(.x){
|
||||||
.x[c(x, y)] |>
|
.x[c(pri, sec)] |>
|
||||||
as.data.frame() |>
|
as.data.frame() |>
|
||||||
plot_euler_single()
|
plot_euler_single()
|
||||||
})
|
})
|
||||||
|
@ -95,7 +95,6 @@ plot_euler <- function(data, x, y, z = NULL, seed = 2103) {
|
||||||
# patchwork::wrap_plots(out, guides = "collect")
|
# patchwork::wrap_plots(out, guides = "collect")
|
||||||
}
|
}
|
||||||
|
|
||||||
?withCallingHandlers()
|
|
||||||
#' Easily plot single euler diagrams
|
#' Easily plot single euler diagrams
|
||||||
#'
|
#'
|
||||||
#' @returns ggplot2 object
|
#' @returns ggplot2 object
|
||||||
|
|
|
@ -6,10 +6,10 @@
|
||||||
#' @name data-plots
|
#' @name data-plots
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' mtcars |> plot_hbars(x = "carb", y = "cyl")
|
#' mtcars |> plot_hbars(pri = "carb", sec = "cyl")
|
||||||
#' mtcars |> plot_hbars(x = "carb", y = NULL)
|
#' mtcars |> plot_hbars(pri = "carb", sec = NULL)
|
||||||
plot_hbars <- function(data, x, y, z = NULL) {
|
plot_hbars <- function(data, pri, sec, ter = NULL) {
|
||||||
out <- vertical_stacked_bars(data = data, score = x, group = y, strata = z)
|
out <- vertical_stacked_bars(data = data, score = pri, group = sec, strata = ter)
|
||||||
|
|
||||||
out
|
out
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,42 +15,42 @@
|
||||||
#' last = sample(c(TRUE, FALSE, FALSE), 100, TRUE)
|
#' last = sample(c(TRUE, FALSE, FALSE), 100, TRUE)
|
||||||
#' ) |>
|
#' ) |>
|
||||||
#' sankey_ready("first", "last")
|
#' sankey_ready("first", "last")
|
||||||
sankey_ready <- function(data, x, y, numbers = "count", ...) {
|
sankey_ready <- function(data, pri, sec, numbers = "count", ...) {
|
||||||
## TODO: Ensure ordering x and y
|
## TODO: Ensure ordering x and y
|
||||||
|
|
||||||
## Ensure all are factors
|
## Ensure all are factors
|
||||||
data[c(x, y)] <- data[c(x, y)] |>
|
data[c(pri, sec)] <- data[c(pri, sec)] |>
|
||||||
dplyr::mutate(dplyr::across(!dplyr::where(is.factor), forcats::as_factor))
|
dplyr::mutate(dplyr::across(!dplyr::where(is.factor), forcats::as_factor))
|
||||||
|
|
||||||
out <- dplyr::count(data, !!dplyr::sym(x), !!dplyr::sym(y))
|
out <- dplyr::count(data, !!dplyr::sym(pri), !!dplyr::sym(sec))
|
||||||
|
|
||||||
out <- out |>
|
out <- out |>
|
||||||
dplyr::group_by(!!dplyr::sym(x)) |>
|
dplyr::group_by(!!dplyr::sym(pri)) |>
|
||||||
dplyr::mutate(gx.sum = sum(n)) |>
|
dplyr::mutate(gx.sum = sum(n)) |>
|
||||||
dplyr::ungroup() |>
|
dplyr::ungroup() |>
|
||||||
dplyr::group_by(!!dplyr::sym(y)) |>
|
dplyr::group_by(!!dplyr::sym(sec)) |>
|
||||||
dplyr::mutate(gy.sum = sum(n)) |>
|
dplyr::mutate(gy.sum = sum(n)) |>
|
||||||
dplyr::ungroup()
|
dplyr::ungroup()
|
||||||
|
|
||||||
if (numbers == "count") {
|
if (numbers == "count") {
|
||||||
out <- out |> dplyr::mutate(
|
out <- out |> dplyr::mutate(
|
||||||
lx = factor(paste0(!!dplyr::sym(x), "\n(n=", gx.sum, ")")),
|
lx = factor(paste0(!!dplyr::sym(pri), "\n(n=", gx.sum, ")")),
|
||||||
ly = factor(paste0(!!dplyr::sym(y), "\n(n=", gy.sum, ")"))
|
ly = factor(paste0(!!dplyr::sym(sec), "\n(n=", gy.sum, ")"))
|
||||||
)
|
)
|
||||||
} else if (numbers == "percentage") {
|
} else if (numbers == "percentage") {
|
||||||
out <- out |> dplyr::mutate(
|
out <- out |> dplyr::mutate(
|
||||||
lx = factor(paste0(!!dplyr::sym(x), "\n(", round((gx.sum / sum(n)) * 100, 1), "%)")),
|
lx = factor(paste0(!!dplyr::sym(pri), "\n(", round((gx.sum / sum(n)) * 100, 1), "%)")),
|
||||||
ly = factor(paste0(!!dplyr::sym(y), "\n(", round((gy.sum / sum(n)) * 100, 1), "%)"))
|
ly = factor(paste0(!!dplyr::sym(sec), "\n(", round((gy.sum / sum(n)) * 100, 1), "%)"))
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is.factor(data[[x]])) {
|
if (is.factor(data[[pri]])) {
|
||||||
index <- match(levels(data[[x]]), str_remove_last(levels(out$lx), "\n"))
|
index <- match(levels(data[[pri]]), str_remove_last(levels(out$lx), "\n"))
|
||||||
out$lx <- factor(out$lx, levels = levels(out$lx)[index])
|
out$lx <- factor(out$lx, levels = levels(out$lx)[index])
|
||||||
}
|
}
|
||||||
|
|
||||||
if (is.factor(data[[y]])) {
|
if (is.factor(data[[sec]])) {
|
||||||
index <- match(levels(data[[y]]), str_remove_last(levels(out$ly), "\n"))
|
index <- match(levels(data[[sec]]), str_remove_last(levels(out$ly), "\n"))
|
||||||
out$ly <- factor(out$ly, levels = levels(out$ly)[index])
|
out$ly <- factor(out$ly, levels = levels(out$ly)[index])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -75,15 +75,15 @@ str_remove_last <- function(data, pattern = "\n") {
|
||||||
#' ds |> plot_sankey("first", "last")
|
#' ds |> plot_sankey("first", "last")
|
||||||
#' ds |> plot_sankey("first", "last", color.group = "y")
|
#' ds |> plot_sankey("first", "last", color.group = "y")
|
||||||
#' ds |> plot_sankey("first", "last", z = "g", color.group = "y")
|
#' ds |> plot_sankey("first", "last", z = "g", color.group = "y")
|
||||||
plot_sankey <- function(data, x, y, z = NULL, color.group = "x", colors = NULL) {
|
plot_sankey <- function(data, pri, sec, ter = NULL, color.group = "x", colors = NULL) {
|
||||||
if (!is.null(z)) {
|
if (!is.null(ter)) {
|
||||||
ds <- split(data, data[z])
|
ds <- split(data, data[ter])
|
||||||
} else {
|
} else {
|
||||||
ds <- list(data)
|
ds <- list(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
out <- lapply(ds, \(.ds){
|
out <- lapply(ds, \(.ds){
|
||||||
plot_sankey_single(.ds, x = x, y = y, color.group = color.group, colors = colors)
|
plot_sankey_single(.ds, x = pri, y = sec, color.group = color.group, colors = colors)
|
||||||
})
|
})
|
||||||
|
|
||||||
patchwork::wrap_plots(out)
|
patchwork::wrap_plots(out)
|
||||||
|
@ -112,10 +112,10 @@ default_theme <- function() {
|
||||||
#' first = REDCapCAST::as_factor(sample(letters[1:4], 100, TRUE)),
|
#' first = REDCapCAST::as_factor(sample(letters[1:4], 100, TRUE)),
|
||||||
#' last = sample(c(TRUE, FALSE, FALSE), 100, TRUE)
|
#' last = sample(c(TRUE, FALSE, FALSE), 100, TRUE)
|
||||||
#' ) |>
|
#' ) |>
|
||||||
#' plot_sankey_single("first", "last", color.group = "x")
|
#' plot_sankey_single("first", "last", color.group = "pri")
|
||||||
plot_sankey_single <- function(data, x, y, color.group = c("x", "y"), colors = NULL, ...) {
|
plot_sankey_single <- function(data, pri, sec, color.group = c("pri", "sec"), colors = NULL, ...) {
|
||||||
color.group <- match.arg(color.group)
|
color.group <- match.arg(color.group)
|
||||||
data <- data |> sankey_ready(x = x, y = y, ...)
|
data <- data |> sankey_ready(pri = pri, sec = sec, ...)
|
||||||
|
|
||||||
library(ggalluvial)
|
library(ggalluvial)
|
||||||
|
|
||||||
|
@ -123,13 +123,13 @@ plot_sankey_single <- function(data, x, y, color.group = c("x", "y"), colors = N
|
||||||
box.color <- "#1E4B66"
|
box.color <- "#1E4B66"
|
||||||
|
|
||||||
if (is.null(colors)) {
|
if (is.null(colors)) {
|
||||||
if (color.group == "y") {
|
if (color.group == "sec") {
|
||||||
main.colors <- viridisLite::viridis(n = length(levels(data[[y]])))
|
main.colors <- viridisLite::viridis(n = length(levels(data[[sec]])))
|
||||||
secondary.colors <- rep(na.color, length(levels(data[[x]])))
|
secondary.colors <- rep(na.color, length(levels(data[[pri]])))
|
||||||
label.colors <- Reduce(c, lapply(list(secondary.colors, rev(main.colors)), contrast_text))
|
label.colors <- Reduce(c, lapply(list(secondary.colors, rev(main.colors)), contrast_text))
|
||||||
} else {
|
} else {
|
||||||
main.colors <- viridisLite::viridis(n = length(levels(data[[x]])))
|
main.colors <- viridisLite::viridis(n = length(levels(data[[pri]])))
|
||||||
secondary.colors <- rep(na.color, length(levels(data[[y]])))
|
secondary.colors <- rep(na.color, length(levels(data[[sec]])))
|
||||||
label.colors <- Reduce(c, lapply(list(rev(main.colors), secondary.colors), contrast_text))
|
label.colors <- Reduce(c, lapply(list(rev(main.colors), secondary.colors), contrast_text))
|
||||||
}
|
}
|
||||||
colors <- c(na.color, main.colors, secondary.colors)
|
colors <- c(na.color, main.colors, secondary.colors)
|
||||||
|
@ -137,33 +137,33 @@ plot_sankey_single <- function(data, x, y, color.group = c("x", "y"), colors = N
|
||||||
label.colors <- contrast_text(colors)
|
label.colors <- contrast_text(colors)
|
||||||
}
|
}
|
||||||
|
|
||||||
group_labels <- c(get_label(data, x), get_label(data, y)) |>
|
group_labels <- c(get_label(data, pri), get_label(data, sec)) |>
|
||||||
sapply(line_break) |>
|
sapply(line_break) |>
|
||||||
unname()
|
unname()
|
||||||
|
|
||||||
p <- ggplot2::ggplot(data, ggplot2::aes(y = n, axis1 = lx, axis2 = ly))
|
p <- ggplot2::ggplot(data, ggplot2::aes(y = n, axis1 = lx, axis2 = ly))
|
||||||
|
|
||||||
if (color.group == "y") {
|
if (color.group == "sec") {
|
||||||
p <- p +
|
p <- p +
|
||||||
ggalluvial::geom_alluvium(
|
ggalluvial::geom_alluvium(
|
||||||
ggplot2::aes(fill = !!dplyr::sym(y), color = !!dplyr::sym(y)),
|
ggplot2::aes(fill = !!dplyr::sym(sec), color = !!dplyr::sym(sec)),
|
||||||
width = 1 / 16,
|
width = 1 / 16,
|
||||||
alpha = .8,
|
alpha = .8,
|
||||||
knot.pos = 0.4,
|
knot.pos = 0.4,
|
||||||
curve_type = "sigmoid"
|
curve_type = "sigmoid"
|
||||||
) + ggalluvial::geom_stratum(ggplot2::aes(fill = !!dplyr::sym(y)),
|
) + ggalluvial::geom_stratum(ggplot2::aes(fill = !!dplyr::sym(sec)),
|
||||||
size = 2,
|
size = 2,
|
||||||
width = 1 / 3.4
|
width = 1 / 3.4
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
p <- p +
|
p <- p +
|
||||||
ggalluvial::geom_alluvium(
|
ggalluvial::geom_alluvium(
|
||||||
ggplot2::aes(fill = !!dplyr::sym(x), color = !!dplyr::sym(x)),
|
ggplot2::aes(fill = !!dplyr::sym(pri), color = !!dplyr::sym(pri)),
|
||||||
width = 1 / 16,
|
width = 1 / 16,
|
||||||
alpha = .8,
|
alpha = .8,
|
||||||
knot.pos = 0.4,
|
knot.pos = 0.4,
|
||||||
curve_type = "sigmoid"
|
curve_type = "sigmoid"
|
||||||
) + ggalluvial::geom_stratum(ggplot2::aes(fill = !!dplyr::sym(x)),
|
) + ggalluvial::geom_stratum(ggplot2::aes(fill = !!dplyr::sym(pri)),
|
||||||
size = 2,
|
size = 2,
|
||||||
width = 1 / 3.4
|
width = 1 / 3.4
|
||||||
)
|
)
|
||||||
|
|
|
@ -6,20 +6,24 @@
|
||||||
#' @name data-plots
|
#' @name data-plots
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' mtcars |> plot_scatter(x = "mpg", y = "wt")
|
#' mtcars |> plot_scatter(pri = "mpg", sec = "wt")
|
||||||
plot_scatter <- function(data, x, y, z = NULL) {
|
plot_scatter <- function(data, pri, sec, ter = NULL) {
|
||||||
if (is.null(z)) {
|
if (is.null(ter)) {
|
||||||
rempsyc::nice_scatter(
|
rempsyc::nice_scatter(
|
||||||
data = data,
|
data = data,
|
||||||
predictor = y,
|
predictor = sec,
|
||||||
response = x, xtitle = get_label(data, var = y), ytitle = get_label(data, var = x)
|
response = pri,
|
||||||
|
xtitle = get_label(data, var = sec),
|
||||||
|
ytitle = get_label(data, var = pri)
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
rempsyc::nice_scatter(
|
rempsyc::nice_scatter(
|
||||||
data = data,
|
data = data,
|
||||||
predictor = y,
|
predictor = sec,
|
||||||
response = x,
|
response = pri,
|
||||||
group = z, xtitle = get_label(data, var = y), ytitle = get_label(data, var = x)
|
group = ter,
|
||||||
|
xtitle = get_label(data, var = sec),
|
||||||
|
ytitle = get_label(data, var = pri)
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,10 +6,10 @@
|
||||||
#' @name data-plots
|
#' @name data-plots
|
||||||
#'
|
#'
|
||||||
#' @examples
|
#' @examples
|
||||||
#' mtcars |> plot_violin(x = "mpg", y = "cyl", z = "gear")
|
#' mtcars |> plot_violin(pri = "mpg", sec = "cyl", ter = "gear")
|
||||||
plot_violin <- function(data, x, y, z = NULL) {
|
plot_violin <- function(data, pri, sec, ter = NULL) {
|
||||||
if (!is.null(z)) {
|
if (!is.null(ter)) {
|
||||||
ds <- split(data, data[z])
|
ds <- split(data, data[ter])
|
||||||
} else {
|
} else {
|
||||||
ds <- list(data)
|
ds <- list(data)
|
||||||
}
|
}
|
||||||
|
@ -17,8 +17,10 @@ plot_violin <- function(data, x, y, z = NULL) {
|
||||||
out <- lapply(ds, \(.ds){
|
out <- lapply(ds, \(.ds){
|
||||||
rempsyc::nice_violin(
|
rempsyc::nice_violin(
|
||||||
data = .ds,
|
data = .ds,
|
||||||
group = y,
|
group = sec,
|
||||||
response = x, xtitle = get_label(data, var = y), ytitle = get_label(data, var = x)
|
response = pri,
|
||||||
|
xtitle = get_label(data, var = sec),
|
||||||
|
ytitle = get_label(data, var = pri)
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
% Generated by roxygen2: do not edit by hand
|
% Generated by roxygen2: do not edit by hand
|
||||||
% Please edit documentation in R/data_plots.R
|
% Please edit documentation in R/data_plots.R
|
||||||
\name{allign_axes}
|
\name{align_axes}
|
||||||
\alias{allign_axes}
|
\alias{align_axes}
|
||||||
\title{Alligns axes between plots}
|
\title{Aligns axes between plots}
|
||||||
\usage{
|
\usage{
|
||||||
allign_axes(...)
|
align_axes(...)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{...}{ggplot2 objects or list of ggplot2 objects}
|
\item{...}{ggplot2 objects or list of ggplot2 objects}
|
||||||
|
@ -13,5 +13,5 @@ allign_axes(...)
|
||||||
list of ggplot2 objects
|
list of ggplot2 objects
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Alligns axes between plots
|
Aligns axes between plots
|
||||||
}
|
}
|
|
@ -15,3 +15,7 @@ list
|
||||||
\description{
|
\description{
|
||||||
Idea from the answer: https://stackoverflow.com/a/62979238
|
Idea from the answer: https://stackoverflow.com/a/62979238
|
||||||
}
|
}
|
||||||
|
\examples{
|
||||||
|
argsstring2list("A=1:5,b=2:4")
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -32,4 +32,5 @@ Create a baseline table
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
mtcars |> create_baseline(by.var = "gear", add.p = "yes" == "yes")
|
mtcars |> create_baseline(by.var = "gear", add.p = "yes" == "yes")
|
||||||
|
create_baseline(default_parsing(mtcars), by.var = "am", add.p = FALSE, add.overall = FALSE, theme = "lancet")
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,8 +18,13 @@ data.frame
|
||||||
Filter function to filter data set by variable type
|
Filter function to filter data set by variable type
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
default_parsing(mtcars) |> data_type_filter(type=c("categorical","continuous")) |> attributes()
|
default_parsing(mtcars) |>
|
||||||
|
data_type_filter(type = c("categorical", "continuous")) |>
|
||||||
|
attributes()
|
||||||
|
default_parsing(mtcars) |>
|
||||||
|
data_type_filter(type = NULL) |>
|
||||||
|
attributes()
|
||||||
\dontrun{
|
\dontrun{
|
||||||
default_parsing(mtcars) |> data_type_filter(type=c("test","categorical","continuous"))
|
default_parsing(mtcars) |> data_type_filter(type = c("test", "categorical", "continuous"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -17,7 +17,7 @@ Deparses expression as string, substitutes native pipe and adds assign
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
list(
|
list(
|
||||||
as.symbol(paste0("mtcars$","mpg")),
|
as.symbol(paste0("mtcars$", "mpg")),
|
||||||
rlang::call2(.fn = "select", !!!list(c("cyl", "disp")), .ns = "dplyr"),
|
rlang::call2(.fn = "select", !!!list(c("cyl", "disp")), .ns = "dplyr"),
|
||||||
rlang::call2(.fn = "default_parsing", .ns = "FreesearchR")
|
rlang::call2(.fn = "default_parsing", .ns = "FreesearchR")
|
||||||
) |>
|
) |>
|
||||||
|
|
|
@ -17,3 +17,6 @@ data.frame
|
||||||
\description{
|
\description{
|
||||||
Factorize variables in data.frame
|
Factorize variables in data.frame
|
||||||
}
|
}
|
||||||
|
\examples{
|
||||||
|
factorize(mtcars,names(mtcars))
|
||||||
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
\alias{line_break}
|
\alias{line_break}
|
||||||
\title{Line breaking at given number of characters for nicely plotting labels}
|
\title{Line breaking at given number of characters for nicely plotting labels}
|
||||||
\usage{
|
\usage{
|
||||||
line_break(data, lineLength = 20, fixed = FALSE)
|
line_break(data, lineLength = 20, force = FALSE)
|
||||||
}
|
}
|
||||||
\arguments{
|
\arguments{
|
||||||
\item{data}{string}
|
\item{data}{string}
|
||||||
|
@ -22,5 +22,5 @@ Line breaking at given number of characters for nicely plotting labels
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
"Lorem ipsum... you know the routine" |> line_break()
|
"Lorem ipsum... you know the routine" |> line_break()
|
||||||
paste(sample(letters[1:10], 100, TRUE), collapse = "") |> line_break(fixed = TRUE)
|
paste(sample(letters[1:10], 100, TRUE), collapse = "") |> line_break(force = TRUE)
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,3 +15,12 @@ data of same class as input
|
||||||
\description{
|
\description{
|
||||||
Remove empty/NA attributes
|
Remove empty/NA attributes
|
||||||
}
|
}
|
||||||
|
\examples{
|
||||||
|
ds <- mtcars |> lapply(\(.x) REDCapCAST::set_attr(.x, label = NA, attr = "label")) |> dplyr::bind_cols()
|
||||||
|
ds |>
|
||||||
|
remove_empty_attr() |>
|
||||||
|
str()
|
||||||
|
mtcars |> lapply(\(.x) REDCapCAST::set_attr(.x, label = NA, attr = "label")) |> remove_empty_attr() |>
|
||||||
|
str()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -1,23 +0,0 @@
|
||||||
% Generated by roxygen2: do not edit by hand
|
|
||||||
% Please edit documentation in R/helpers.R
|
|
||||||
\name{remove_na_attr}
|
|
||||||
\alias{remove_na_attr}
|
|
||||||
\title{Remove NA labels}
|
|
||||||
\usage{
|
|
||||||
remove_na_attr(data, attr = "label")
|
|
||||||
}
|
|
||||||
\arguments{
|
|
||||||
\item{data}{data}
|
|
||||||
}
|
|
||||||
\value{
|
|
||||||
data.frame
|
|
||||||
}
|
|
||||||
\description{
|
|
||||||
Remove NA labels
|
|
||||||
}
|
|
||||||
\examples{
|
|
||||||
ds <- mtcars |> lapply(\(.x) REDCapCAST::set_attr(.x, label = NA, attr = "label"))
|
|
||||||
ds |>
|
|
||||||
remove_na_attr() |>
|
|
||||||
str()
|
|
||||||
}
|
|
|
@ -2,7 +2,7 @@
|
||||||
% Please edit documentation in R/helpers.R
|
% Please edit documentation in R/helpers.R
|
||||||
\name{remove_nested_list}
|
\name{remove_nested_list}
|
||||||
\alias{remove_nested_list}
|
\alias{remove_nested_list}
|
||||||
\title{Very simple function to remove nested lists, lik ewhen uploading .rds}
|
\title{Very simple function to remove nested lists, like when uploading .rds}
|
||||||
\usage{
|
\usage{
|
||||||
remove_nested_list(data)
|
remove_nested_list(data)
|
||||||
}
|
}
|
||||||
|
@ -13,7 +13,7 @@ remove_nested_list(data)
|
||||||
data.frame
|
data.frame
|
||||||
}
|
}
|
||||||
\description{
|
\description{
|
||||||
Very simple function to remove nested lists, lik ewhen uploading .rds
|
Very simple function to remove nested lists, like when uploading .rds
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
dplyr::tibble(a = 1:10, b = rep(list("a"), 10)) |> remove_nested_list()
|
dplyr::tibble(a = 1:10, b = rep(list("a"), 10)) |> remove_nested_list()
|
||||||
|
|
|
@ -21,6 +21,6 @@ Easily subset by data type function
|
||||||
}
|
}
|
||||||
\examples{
|
\examples{
|
||||||
default_parsing(mtcars) |> subset_types("ordinal")
|
default_parsing(mtcars) |> subset_types("ordinal")
|
||||||
default_parsing(mtcars) |> subset_types(c("dichotomous", "ordinal" ,"categorical"))
|
default_parsing(mtcars) |> subset_types(c("dichotomous", "ordinal", "categorical"))
|
||||||
#' default_parsing(mtcars) |> subset_types("factor",class)
|
#' default_parsing(mtcars) |> subset_types("factor",class)
|
||||||
}
|
}
|
||||||
|
|
2182
tests/testthat/_snaps/baseline_table.md
Normal file
2182
tests/testthat/_snaps/baseline_table.md
Normal file
File diff suppressed because it is too large
Load diff
23
tests/testthat/_snaps/contrast_text.md
Normal file
23
tests/testthat/_snaps/contrast_text.md
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
# Contrasting works
|
||||||
|
|
||||||
|
Code
|
||||||
|
contrast_text(colors)
|
||||||
|
Output
|
||||||
|
[1] "black" "white" "white" "white" "black" "white"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
contrast_text(colors, light_text = "blue", dark_text = "grey10", method = "relative",
|
||||||
|
threshold = 0.1)
|
||||||
|
Output
|
||||||
|
[1] "grey10" "blue" "grey10" "blue" "grey10" "grey10"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
contrast_text(colors, light_text = "blue", dark_text = "grey10", method = "perceived",
|
||||||
|
threshold = 0.7)
|
||||||
|
Output
|
||||||
|
[1] "grey10" "blue" "blue" "blue" "grey10" "blue"
|
||||||
|
|
160
tests/testthat/_snaps/data_plots.md
Normal file
160
tests/testthat/_snaps/data_plots.md
Normal file
|
@ -0,0 +1,160 @@
|
||||||
|
# all_but works
|
||||||
|
|
||||||
|
Code
|
||||||
|
all_but(1:10, c(2, 3), 11, 5)
|
||||||
|
Output
|
||||||
|
[1] 1 4 6 7 8 9 10
|
||||||
|
|
||||||
|
# subset_types works
|
||||||
|
|
||||||
|
Code
|
||||||
|
subset_types(default_parsing(mtcars), "continuous")
|
||||||
|
Output
|
||||||
|
# A tibble: 32 x 6
|
||||||
|
mpg disp hp drat wt qsec
|
||||||
|
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
|
||||||
|
1 21 160 110 3.9 2.62 16.5
|
||||||
|
2 21 160 110 3.9 2.88 17.0
|
||||||
|
3 22.8 108 93 3.85 2.32 18.6
|
||||||
|
4 21.4 258 110 3.08 3.22 19.4
|
||||||
|
5 18.7 360 175 3.15 3.44 17.0
|
||||||
|
6 18.1 225 105 2.76 3.46 20.2
|
||||||
|
7 14.3 360 245 3.21 3.57 15.8
|
||||||
|
8 24.4 147. 62 3.69 3.19 20
|
||||||
|
9 22.8 141. 95 3.92 3.15 22.9
|
||||||
|
10 19.2 168. 123 3.92 3.44 18.3
|
||||||
|
# i 22 more rows
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
subset_types(default_parsing(mtcars), c("dichotomous", "ordinal", "categorical"))
|
||||||
|
Output
|
||||||
|
# A tibble: 32 x 5
|
||||||
|
cyl vs am gear carb
|
||||||
|
<fct> <lgl> <lgl> <fct> <fct>
|
||||||
|
1 6 FALSE TRUE 4 4
|
||||||
|
2 6 FALSE TRUE 4 4
|
||||||
|
3 4 TRUE TRUE 4 1
|
||||||
|
4 6 TRUE FALSE 3 1
|
||||||
|
5 8 FALSE FALSE 3 2
|
||||||
|
6 6 TRUE FALSE 3 1
|
||||||
|
7 8 FALSE FALSE 3 4
|
||||||
|
8 4 TRUE FALSE 4 2
|
||||||
|
9 4 TRUE FALSE 4 2
|
||||||
|
10 6 TRUE FALSE 4 4
|
||||||
|
# i 22 more rows
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
subset_types(default_parsing(mtcars), "test")
|
||||||
|
Output
|
||||||
|
# A tibble: 32 x 0
|
||||||
|
|
||||||
|
# possible_plots works
|
||||||
|
|
||||||
|
Code
|
||||||
|
possible_plots(mtcars$mpg)
|
||||||
|
Output
|
||||||
|
[1] "Violin plot" "Scatter plot" "Box plot"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
possible_plots(default_parsing(mtcars)["cyl"])
|
||||||
|
Output
|
||||||
|
[1] "Stacked horizontal bars" "Violin plot"
|
||||||
|
[3] "Sankey plot" "Box plot"
|
||||||
|
|
||||||
|
# get_plot_options works
|
||||||
|
|
||||||
|
Code
|
||||||
|
get_plot_options((function(.x) {
|
||||||
|
.x[[1]]
|
||||||
|
})(possible_plots(default_parsing(mtcars)["mpg"])))
|
||||||
|
Output
|
||||||
|
$plot_violin
|
||||||
|
$plot_violin$fun
|
||||||
|
[1] "plot_violin"
|
||||||
|
|
||||||
|
$plot_violin$descr
|
||||||
|
[1] "Violin plot"
|
||||||
|
|
||||||
|
$plot_violin$note
|
||||||
|
[1] "A modern alternative to the classic boxplot to visualise data distribution"
|
||||||
|
|
||||||
|
$plot_violin$primary.type
|
||||||
|
[1] "datatime" "continuous" "dichotomous" "ordinal" "categorical"
|
||||||
|
|
||||||
|
$plot_violin$secondary.type
|
||||||
|
[1] "dichotomous" "ordinal" "categorical"
|
||||||
|
|
||||||
|
$plot_violin$secondary.multi
|
||||||
|
[1] FALSE
|
||||||
|
|
||||||
|
$plot_violin$secondary.extra
|
||||||
|
[1] "none"
|
||||||
|
|
||||||
|
$plot_violin$tertiary.type
|
||||||
|
[1] "dichotomous" "ordinal" "categorical"
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
# get_label works
|
||||||
|
|
||||||
|
Code
|
||||||
|
get_label(mtcars, var = "mpg")
|
||||||
|
Output
|
||||||
|
[1] "mpg"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
get_label(mtcars)
|
||||||
|
Output
|
||||||
|
[1] "mtcars"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
get_label(mtcars$mpg)
|
||||||
|
Output
|
||||||
|
[1] "mtcars$mpg"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
get_label(gtsummary::trial, var = "trt")
|
||||||
|
Output
|
||||||
|
[1] "Chemotherapy Treatment"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
get_label(1:10)
|
||||||
|
Output
|
||||||
|
[1] "1:10"
|
||||||
|
|
||||||
|
# line_break works
|
||||||
|
|
||||||
|
Code
|
||||||
|
line_break("Lorem ipsum... you know the routine")
|
||||||
|
Output
|
||||||
|
[1] "Lorem ipsum... you\nknow the routine"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
line_break(paste(sample(letters[1:10], 100, TRUE), collapse = ""), force = TRUE,
|
||||||
|
lineLength = 5)
|
||||||
|
Output
|
||||||
|
[1] "cjijd\ncjcfb\nihfgi\nfcffh\neaddf\ngegjb\njeegi\nfdhbe\nbgcac\nibfbe\nejibi\nggedh\ngajhf\ngadca\nijeig\ncieeh\ncah\n"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
line_break(paste(sample(letters[1:10], 100, TRUE), collapse = ""), force = FALSE)
|
||||||
|
Output
|
||||||
|
[1] "idjcgcjceeefchffjdbjafabigaiadcfdcfgfgibibhcjbbbejabddeheafggcgbdfbcbeegijggbibaghfidjgeaefhcadbfjig"
|
||||||
|
|
532
tests/testthat/_snaps/helpers.md
Normal file
532
tests/testthat/_snaps/helpers.md
Normal file
|
@ -0,0 +1,532 @@
|
||||||
|
# getfun works
|
||||||
|
|
||||||
|
Code
|
||||||
|
getfun("stats::lm")
|
||||||
|
Output
|
||||||
|
function (formula, data, subset, weights, na.action, method = "qr",
|
||||||
|
model = TRUE, x = FALSE, y = FALSE, qr = TRUE, singular.ok = TRUE,
|
||||||
|
contrasts = NULL, offset, ...)
|
||||||
|
{
|
||||||
|
ret.x <- x
|
||||||
|
ret.y <- y
|
||||||
|
cl <- match.call()
|
||||||
|
mf <- match.call(expand.dots = FALSE)
|
||||||
|
m <- match(c("formula", "data", "subset", "weights", "na.action",
|
||||||
|
"offset"), names(mf), 0L)
|
||||||
|
mf <- mf[c(1L, m)]
|
||||||
|
mf$drop.unused.levels <- TRUE
|
||||||
|
mf[[1L]] <- quote(stats::model.frame)
|
||||||
|
mf <- eval(mf, parent.frame())
|
||||||
|
if (method == "model.frame")
|
||||||
|
return(mf)
|
||||||
|
else if (method != "qr")
|
||||||
|
warning(gettextf("method = '%s' is not supported. Using 'qr'",
|
||||||
|
method), domain = NA)
|
||||||
|
mt <- attr(mf, "terms")
|
||||||
|
y <- model.response(mf, "numeric")
|
||||||
|
w <- as.vector(model.weights(mf))
|
||||||
|
if (!is.null(w) && !is.numeric(w))
|
||||||
|
stop("'weights' must be a numeric vector")
|
||||||
|
offset <- model.offset(mf)
|
||||||
|
mlm <- is.matrix(y)
|
||||||
|
ny <- if (mlm)
|
||||||
|
nrow(y)
|
||||||
|
else length(y)
|
||||||
|
if (!is.null(offset)) {
|
||||||
|
if (!mlm)
|
||||||
|
offset <- as.vector(offset)
|
||||||
|
if (NROW(offset) != ny)
|
||||||
|
stop(gettextf("number of offsets is %d, should equal %d (number of observations)",
|
||||||
|
NROW(offset), ny), domain = NA)
|
||||||
|
}
|
||||||
|
if (is.empty.model(mt)) {
|
||||||
|
x <- NULL
|
||||||
|
z <- list(coefficients = if (mlm) matrix(NA_real_, 0,
|
||||||
|
ncol(y)) else numeric(), residuals = y, fitted.values = 0 *
|
||||||
|
y, weights = w, rank = 0L, df.residual = if (!is.null(w)) sum(w !=
|
||||||
|
0) else ny)
|
||||||
|
if (!is.null(offset)) {
|
||||||
|
z$fitted.values <- offset
|
||||||
|
z$residuals <- y - offset
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
x <- model.matrix(mt, mf, contrasts)
|
||||||
|
z <- if (is.null(w))
|
||||||
|
lm.fit(x, y, offset = offset, singular.ok = singular.ok,
|
||||||
|
...)
|
||||||
|
else lm.wfit(x, y, w, offset = offset, singular.ok = singular.ok,
|
||||||
|
...)
|
||||||
|
}
|
||||||
|
class(z) <- c(if (mlm) "mlm", "lm")
|
||||||
|
z$na.action <- attr(mf, "na.action")
|
||||||
|
z$offset <- offset
|
||||||
|
z$contrasts <- attr(x, "contrasts")
|
||||||
|
z$xlevels <- .getXlevels(mt, mf)
|
||||||
|
z$call <- cl
|
||||||
|
z$terms <- mt
|
||||||
|
if (model)
|
||||||
|
z$model <- mf
|
||||||
|
if (ret.x)
|
||||||
|
z$x <- x
|
||||||
|
if (ret.y)
|
||||||
|
z$y <- y
|
||||||
|
if (!qr)
|
||||||
|
z$qr <- NULL
|
||||||
|
z
|
||||||
|
}
|
||||||
|
<bytecode: 0x12c7f2dd8>
|
||||||
|
<environment: namespace:stats>
|
||||||
|
|
||||||
|
# argsstring2list works
|
||||||
|
|
||||||
|
Code
|
||||||
|
argsstring2list("A=1:5,b=2:4")
|
||||||
|
Output
|
||||||
|
$A
|
||||||
|
[1] 1 2 3 4 5
|
||||||
|
|
||||||
|
$b
|
||||||
|
[1] 2 3 4
|
||||||
|
|
||||||
|
|
||||||
|
# factorize works
|
||||||
|
|
||||||
|
Code
|
||||||
|
factorize(mtcars, names(mtcars))
|
||||||
|
Output
|
||||||
|
mpg cyl disp hp drat wt qsec vs am gear carb
|
||||||
|
Mazda RX4 21 6 160 110 3.9 2.62 16.46 0 1 4 4
|
||||||
|
Mazda RX4 Wag 21 6 160 110 3.9 2.875 17.02 0 1 4 4
|
||||||
|
Datsun 710 22.8 4 108 93 3.85 2.32 18.61 1 1 4 1
|
||||||
|
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
|
||||||
|
Hornet Sportabout 18.7 8 360 175 3.15 3.44 17.02 0 0 3 2
|
||||||
|
Valiant 18.1 6 225 105 2.76 3.46 20.22 1 0 3 1
|
||||||
|
Duster 360 14.3 8 360 245 3.21 3.57 15.84 0 0 3 4
|
||||||
|
Merc 240D 24.4 4 146.7 62 3.69 3.19 20 1 0 4 2
|
||||||
|
Merc 230 22.8 4 140.8 95 3.92 3.15 22.9 1 0 4 2
|
||||||
|
Merc 280 19.2 6 167.6 123 3.92 3.44 18.3 1 0 4 4
|
||||||
|
Merc 280C 17.8 6 167.6 123 3.92 3.44 18.9 1 0 4 4
|
||||||
|
Merc 450SE 16.4 8 275.8 180 3.07 4.07 17.4 0 0 3 3
|
||||||
|
Merc 450SL 17.3 8 275.8 180 3.07 3.73 17.6 0 0 3 3
|
||||||
|
Merc 450SLC 15.2 8 275.8 180 3.07 3.78 18 0 0 3 3
|
||||||
|
Cadillac Fleetwood 10.4 8 472 205 2.93 5.25 17.98 0 0 3 4
|
||||||
|
Lincoln Continental 10.4 8 460 215 3 5.424 17.82 0 0 3 4
|
||||||
|
Chrysler Imperial 14.7 8 440 230 3.23 5.345 17.42 0 0 3 4
|
||||||
|
Fiat 128 32.4 4 78.7 66 4.08 2.2 19.47 1 1 4 1
|
||||||
|
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
|
||||||
|
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.9 1 1 4 1
|
||||||
|
Toyota Corona 21.5 4 120.1 97 3.7 2.465 20.01 1 0 3 1
|
||||||
|
Dodge Challenger 15.5 8 318 150 2.76 3.52 16.87 0 0 3 2
|
||||||
|
AMC Javelin 15.2 8 304 150 3.15 3.435 17.3 0 0 3 2
|
||||||
|
Camaro Z28 13.3 8 350 245 3.73 3.84 15.41 0 0 3 4
|
||||||
|
Pontiac Firebird 19.2 8 400 175 3.08 3.845 17.05 0 0 3 2
|
||||||
|
Fiat X1-9 27.3 4 79 66 4.08 1.935 18.9 1 1 4 1
|
||||||
|
Porsche 914-2 26 4 120.3 91 4.43 2.14 16.7 0 1 5 2
|
||||||
|
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.9 1 1 5 2
|
||||||
|
Ford Pantera L 15.8 8 351 264 4.22 3.17 14.5 0 1 5 4
|
||||||
|
Ferrari Dino 19.7 6 145 175 3.62 2.77 15.5 0 1 5 6
|
||||||
|
Maserati Bora 15 8 301 335 3.54 3.57 14.6 0 1 5 8
|
||||||
|
Volvo 142E 21.4 4 121 109 4.11 2.78 18.6 1 1 4 2
|
||||||
|
|
||||||
|
# default_parsing works
|
||||||
|
|
||||||
|
Code
|
||||||
|
default_parsing(mtcars)
|
||||||
|
Output
|
||||||
|
# A tibble: 32 x 11
|
||||||
|
mpg cyl disp hp drat wt qsec vs am gear carb
|
||||||
|
<dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl> <lgl> <fct> <fct>
|
||||||
|
1 21 6 160 110 3.9 2.62 16.5 FALSE TRUE 4 4
|
||||||
|
2 21 6 160 110 3.9 2.88 17.0 FALSE TRUE 4 4
|
||||||
|
3 22.8 4 108 93 3.85 2.32 18.6 TRUE TRUE 4 1
|
||||||
|
4 21.4 6 258 110 3.08 3.22 19.4 TRUE FALSE 3 1
|
||||||
|
5 18.7 8 360 175 3.15 3.44 17.0 FALSE FALSE 3 2
|
||||||
|
6 18.1 6 225 105 2.76 3.46 20.2 TRUE FALSE 3 1
|
||||||
|
7 14.3 8 360 245 3.21 3.57 15.8 FALSE FALSE 3 4
|
||||||
|
8 24.4 4 147. 62 3.69 3.19 20 TRUE FALSE 4 2
|
||||||
|
9 22.8 4 141. 95 3.92 3.15 22.9 TRUE FALSE 4 2
|
||||||
|
10 19.2 6 168. 123 3.92 3.44 18.3 TRUE FALSE 4 4
|
||||||
|
# i 22 more rows
|
||||||
|
|
||||||
|
# remove_empty_attr works
|
||||||
|
|
||||||
|
Code
|
||||||
|
remove_empty_attr(ds)
|
||||||
|
Output
|
||||||
|
$mpg
|
||||||
|
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
|
||||||
|
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
|
||||||
|
[31] 15.0 21.4
|
||||||
|
|
||||||
|
$cyl
|
||||||
|
[1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8 8 8 4 4 4 4 8 8 8 8 4 4 4 8 6 8 4
|
||||||
|
|
||||||
|
$disp
|
||||||
|
[1] 160.0 160.0 108.0 258.0 360.0 225.0 360.0 146.7 140.8 167.6 167.6 275.8
|
||||||
|
[13] 275.8 275.8 472.0 460.0 440.0 78.7 75.7 71.1 120.1 318.0 304.0 350.0
|
||||||
|
[25] 400.0 79.0 120.3 95.1 351.0 145.0 301.0 121.0
|
||||||
|
|
||||||
|
$hp
|
||||||
|
[1] 110 110 93 110 175 105 245 62 95 123 123 180 180 180 205 215 230 66 52
|
||||||
|
[20] 65 97 150 150 245 175 66 91 113 264 175 335 109
|
||||||
|
|
||||||
|
$drat
|
||||||
|
[1] 3.90 3.90 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 3.92 3.07 3.07 3.07 2.93
|
||||||
|
[16] 3.00 3.23 4.08 4.93 4.22 3.70 2.76 3.15 3.73 3.08 4.08 4.43 3.77 4.22 3.62
|
||||||
|
[31] 3.54 4.11
|
||||||
|
|
||||||
|
$wt
|
||||||
|
[1] 2.620 2.875 2.320 3.215 3.440 3.460 3.570 3.190 3.150 3.440 3.440 4.070
|
||||||
|
[13] 3.730 3.780 5.250 5.424 5.345 2.200 1.615 1.835 2.465 3.520 3.435 3.840
|
||||||
|
[25] 3.845 1.935 2.140 1.513 3.170 2.770 3.570 2.780
|
||||||
|
|
||||||
|
$qsec
|
||||||
|
[1] 16.46 17.02 18.61 19.44 17.02 20.22 15.84 20.00 22.90 18.30 18.90 17.40
|
||||||
|
[13] 17.60 18.00 17.98 17.82 17.42 19.47 18.52 19.90 20.01 16.87 17.30 15.41
|
||||||
|
[25] 17.05 18.90 16.70 16.90 14.50 15.50 14.60 18.60
|
||||||
|
|
||||||
|
$vs
|
||||||
|
[1] 0 0 1 1 0 1 0 1 1 1 1 0 0 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 0 0 0 1
|
||||||
|
|
||||||
|
$am
|
||||||
|
[1] 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 0 0 0 0 0 1 1 1 1 1 1 1
|
||||||
|
|
||||||
|
$gear
|
||||||
|
[1] 4 4 4 3 3 3 3 4 4 4 4 3 3 3 3 3 3 4 4 4 3 3 3 3 3 4 5 5 5 5 5 4
|
||||||
|
|
||||||
|
$carb
|
||||||
|
[1] 4 4 1 1 2 1 4 2 2 4 4 3 3 3 4 4 4 1 2 1 1 2 2 4 2 1 2 2 4 6 8 2
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
remove_empty_attr(dplyr::bind_cols(ds))
|
||||||
|
Output
|
||||||
|
# A tibble: 32 x 11
|
||||||
|
mpg cyl disp hp drat wt qsec vs am gear carb
|
||||||
|
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
|
||||||
|
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
|
||||||
|
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
|
||||||
|
3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
|
||||||
|
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
|
||||||
|
5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
|
||||||
|
6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
|
||||||
|
7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
|
||||||
|
8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
|
||||||
|
9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
|
||||||
|
10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
|
||||||
|
# i 22 more rows
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
remove_empty_attr(ds[[1]])
|
||||||
|
Output
|
||||||
|
[1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4
|
||||||
|
[16] 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7
|
||||||
|
[31] 15.0 21.4
|
||||||
|
|
||||||
|
# remove_empty_cols works
|
||||||
|
|
||||||
|
Code
|
||||||
|
remove_empty_cols(data.frame(a = 1:10, b = NA, c = c(2, NA)), cutoff = 0.5)
|
||||||
|
Output
|
||||||
|
a c
|
||||||
|
1 1 2
|
||||||
|
2 2 NA
|
||||||
|
3 3 2
|
||||||
|
4 4 NA
|
||||||
|
5 5 2
|
||||||
|
6 6 NA
|
||||||
|
7 7 2
|
||||||
|
8 8 NA
|
||||||
|
9 9 2
|
||||||
|
10 10 NA
|
||||||
|
|
||||||
|
# append_list works
|
||||||
|
|
||||||
|
Code
|
||||||
|
append_list(data.frame(letters[1:20], 1:20), ls_d, "letters")
|
||||||
|
Output
|
||||||
|
$letters
|
||||||
|
letters.1.20. X1.20
|
||||||
|
1 a 1
|
||||||
|
2 b 2
|
||||||
|
3 c 3
|
||||||
|
4 d 4
|
||||||
|
5 e 5
|
||||||
|
6 f 6
|
||||||
|
7 g 7
|
||||||
|
8 h 8
|
||||||
|
9 i 9
|
||||||
|
10 j 10
|
||||||
|
11 k 11
|
||||||
|
12 l 12
|
||||||
|
13 m 13
|
||||||
|
14 n 14
|
||||||
|
15 o 15
|
||||||
|
16 p 16
|
||||||
|
17 q 17
|
||||||
|
18 r 18
|
||||||
|
19 s 19
|
||||||
|
20 t 20
|
||||||
|
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
append_list(letters[1:20], ls_d, "letters")
|
||||||
|
Output
|
||||||
|
$letters
|
||||||
|
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
|
||||||
|
[20] "t"
|
||||||
|
|
||||||
|
|
||||||
|
# missing_fraction works
|
||||||
|
|
||||||
|
Code
|
||||||
|
missing_fraction(c(NA, 1:10, rep(NA, 3)))
|
||||||
|
Output
|
||||||
|
[1] 0.2857143
|
||||||
|
|
||||||
|
# data_description works
|
||||||
|
|
||||||
|
Code
|
||||||
|
data_description(data.frame(sample(1:8, 20, TRUE), sample(c(1:8, NA), 20, TRUE)),
|
||||||
|
data_text = "This data")
|
||||||
|
Output
|
||||||
|
[1] "This data has 20 observations and 2 variables, with 16 (80%) complete cases."
|
||||||
|
|
||||||
|
# Data type filter works
|
||||||
|
|
||||||
|
Code
|
||||||
|
data_type_filter(default_parsing(mtcars), type = c("categorical", "continuous"))
|
||||||
|
Output
|
||||||
|
# A tibble: 32 x 9
|
||||||
|
mpg cyl disp hp drat wt qsec gear carb
|
||||||
|
<dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <fct> <fct>
|
||||||
|
1 21 6 160 110 3.9 2.62 16.5 4 4
|
||||||
|
2 21 6 160 110 3.9 2.88 17.0 4 4
|
||||||
|
3 22.8 4 108 93 3.85 2.32 18.6 4 1
|
||||||
|
4 21.4 6 258 110 3.08 3.22 19.4 3 1
|
||||||
|
5 18.7 8 360 175 3.15 3.44 17.0 3 2
|
||||||
|
6 18.1 6 225 105 2.76 3.46 20.2 3 1
|
||||||
|
7 14.3 8 360 245 3.21 3.57 15.8 3 4
|
||||||
|
8 24.4 4 147. 62 3.69 3.19 20 4 2
|
||||||
|
9 22.8 4 141. 95 3.92 3.15 22.9 4 2
|
||||||
|
10 19.2 6 168. 123 3.92 3.44 18.3 4 4
|
||||||
|
# i 22 more rows
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
data_type_filter(default_parsing(mtcars), type = NULL)
|
||||||
|
Output
|
||||||
|
# A tibble: 32 x 11
|
||||||
|
mpg cyl disp hp drat wt qsec vs am gear carb
|
||||||
|
<dbl> <fct> <dbl> <dbl> <dbl> <dbl> <dbl> <lgl> <lgl> <fct> <fct>
|
||||||
|
1 21 6 160 110 3.9 2.62 16.5 FALSE TRUE 4 4
|
||||||
|
2 21 6 160 110 3.9 2.88 17.0 FALSE TRUE 4 4
|
||||||
|
3 22.8 4 108 93 3.85 2.32 18.6 TRUE TRUE 4 1
|
||||||
|
4 21.4 6 258 110 3.08 3.22 19.4 TRUE FALSE 3 1
|
||||||
|
5 18.7 8 360 175 3.15 3.44 17.0 FALSE FALSE 3 2
|
||||||
|
6 18.1 6 225 105 2.76 3.46 20.2 TRUE FALSE 3 1
|
||||||
|
7 14.3 8 360 245 3.21 3.57 15.8 FALSE FALSE 3 4
|
||||||
|
8 24.4 4 147. 62 3.69 3.19 20 TRUE FALSE 4 2
|
||||||
|
9 22.8 4 141. 95 3.92 3.15 22.9 TRUE FALSE 4 2
|
||||||
|
10 19.2 6 168. 123 3.92 3.44 18.3 TRUE FALSE 4 4
|
||||||
|
# i 22 more rows
|
||||||
|
|
||||||
|
# sort_by works
|
||||||
|
|
||||||
|
Code
|
||||||
|
sort_by(c("Multivariable", "Univariable"), c("Univariable", "Minimal",
|
||||||
|
"Multivariable"))
|
||||||
|
Output
|
||||||
|
[1] "Univariable" NA "Multivariable"
|
||||||
|
|
||||||
|
# if_not_missing works
|
||||||
|
|
||||||
|
Code
|
||||||
|
if_not_missing(NULL, "new")
|
||||||
|
Output
|
||||||
|
[1] "new"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
if_not_missing(c(2, "a", NA))
|
||||||
|
Output
|
||||||
|
[1] "2" "a"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
if_not_missing("See")
|
||||||
|
Output
|
||||||
|
[1] "See"
|
||||||
|
|
||||||
|
# merge_expression, expression_string and pipe_string works
|
||||||
|
|
||||||
|
Code
|
||||||
|
merge_expression(list(rlang::call2(.fn = "select", !!!list(c("cyl", "disp")),
|
||||||
|
.ns = "dplyr"), rlang::call2(.fn = "default_parsing", .ns = "FreesearchR")))
|
||||||
|
Output
|
||||||
|
dplyr::select(c("cyl", "disp")) %>% FreesearchR::default_parsing()
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
expression_string(pipe_string(lapply(list("mtcars", rlang::call2(.fn = "select",
|
||||||
|
!!!list(c("cyl", "disp")), .ns = "dplyr"), rlang::call2(.fn = "default_parsing",
|
||||||
|
.ns = "FreesearchR")), expression_string)), "data<-")
|
||||||
|
Output
|
||||||
|
[1] "data<-mtcars|>\ndplyr::select(c('cyl','disp'))|>\nFreesearchR::default_parsing()"
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
expression_string(merge_expression(list(as.symbol(paste0("mtcars$", "mpg")),
|
||||||
|
rlang::call2(.fn = "select", !!!list(c("cyl", "disp")), .ns = "dplyr"), rlang::call2(
|
||||||
|
.fn = "default_parsing", .ns = "FreesearchR"))))
|
||||||
|
Output
|
||||||
|
[1] "mtcars$mpg|>\ndplyr::select(c('cyl','disp'))|>\nFreesearchR::default_parsing()"
|
||||||
|
|
||||||
|
# remove_nested_list works
|
||||||
|
|
||||||
|
Code
|
||||||
|
remove_nested_list(dplyr::tibble(a = 1:10, b = rep(list("a"), 10)))
|
||||||
|
Output
|
||||||
|
# A tibble: 10 x 1
|
||||||
|
a
|
||||||
|
<int>
|
||||||
|
1 1
|
||||||
|
2 2
|
||||||
|
3 3
|
||||||
|
4 4
|
||||||
|
5 5
|
||||||
|
6 6
|
||||||
|
7 7
|
||||||
|
8 8
|
||||||
|
9 9
|
||||||
|
10 10
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
remove_nested_list(as.data.frame(dplyr::tibble(a = 1:10, b = rep(list(c("a",
|
||||||
|
"b")), 10))))
|
||||||
|
Output
|
||||||
|
a
|
||||||
|
1 1
|
||||||
|
2 2
|
||||||
|
3 3
|
||||||
|
4 4
|
||||||
|
5 5
|
||||||
|
6 6
|
||||||
|
7 7
|
||||||
|
8 8
|
||||||
|
9 9
|
||||||
|
10 10
|
||||||
|
|
||||||
|
# set_column_label works
|
||||||
|
|
||||||
|
Code
|
||||||
|
set_column_label(set_column_label(set_column_label(mtcars, ls), ls2), ls3)
|
||||||
|
Output
|
||||||
|
# A tibble: 32 x 11
|
||||||
|
mpg cyl disp hp drat wt qsec vs am gear carb
|
||||||
|
<dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
|
||||||
|
1 21 6 160 110 3.9 2.62 16.5 0 1 4 4
|
||||||
|
2 21 6 160 110 3.9 2.88 17.0 0 1 4 4
|
||||||
|
3 22.8 4 108 93 3.85 2.32 18.6 1 1 4 1
|
||||||
|
4 21.4 6 258 110 3.08 3.22 19.4 1 0 3 1
|
||||||
|
5 18.7 8 360 175 3.15 3.44 17.0 0 0 3 2
|
||||||
|
6 18.1 6 225 105 2.76 3.46 20.2 1 0 3 1
|
||||||
|
7 14.3 8 360 245 3.21 3.57 15.8 0 0 3 4
|
||||||
|
8 24.4 4 147. 62 3.69 3.19 20 1 0 4 2
|
||||||
|
9 22.8 4 141. 95 3.92 3.15 22.9 1 0 4 2
|
||||||
|
10 19.2 6 168. 123 3.92 3.44 18.3 1 0 4 4
|
||||||
|
# i 22 more rows
|
||||||
|
|
||||||
|
---
|
||||||
|
|
||||||
|
Code
|
||||||
|
expression_string(rlang::expr(FreesearchR::set_column_label(label = !!ls3)))
|
||||||
|
Output
|
||||||
|
[1] "FreesearchR::set_column_label(label=c(mpg='',cyl='',disp='',hp='Horses',drat='',wt='',qsec='',vs='',am='',gear='',carb=''))"
|
||||||
|
|
||||||
|
# append_column works
|
||||||
|
|
||||||
|
Code
|
||||||
|
append_column(dplyr::mutate(mtcars, mpg_cut = mpg), mtcars$mpg, "mpg_cutter")
|
||||||
|
Output
|
||||||
|
mpg cyl disp hp drat wt qsec vs am gear carb mpg_cut
|
||||||
|
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 21.0
|
||||||
|
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 21.0
|
||||||
|
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 22.8
|
||||||
|
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 21.4
|
||||||
|
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 18.7
|
||||||
|
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 18.1
|
||||||
|
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 14.3
|
||||||
|
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 24.4
|
||||||
|
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 22.8
|
||||||
|
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 19.2
|
||||||
|
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 17.8
|
||||||
|
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 16.4
|
||||||
|
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 17.3
|
||||||
|
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 15.2
|
||||||
|
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 10.4
|
||||||
|
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 10.4
|
||||||
|
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 14.7
|
||||||
|
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 32.4
|
||||||
|
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 30.4
|
||||||
|
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1 33.9
|
||||||
|
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1 21.5
|
||||||
|
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2 15.5
|
||||||
|
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2 15.2
|
||||||
|
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4 13.3
|
||||||
|
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2 19.2
|
||||||
|
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1 27.3
|
||||||
|
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2 26.0
|
||||||
|
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2 30.4
|
||||||
|
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4 15.8
|
||||||
|
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6 19.7
|
||||||
|
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8 15.0
|
||||||
|
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2 21.4
|
||||||
|
mpg_cutter
|
||||||
|
Mazda RX4 21.0
|
||||||
|
Mazda RX4 Wag 21.0
|
||||||
|
Datsun 710 22.8
|
||||||
|
Hornet 4 Drive 21.4
|
||||||
|
Hornet Sportabout 18.7
|
||||||
|
Valiant 18.1
|
||||||
|
Duster 360 14.3
|
||||||
|
Merc 240D 24.4
|
||||||
|
Merc 230 22.8
|
||||||
|
Merc 280 19.2
|
||||||
|
Merc 280C 17.8
|
||||||
|
Merc 450SE 16.4
|
||||||
|
Merc 450SL 17.3
|
||||||
|
Merc 450SLC 15.2
|
||||||
|
Cadillac Fleetwood 10.4
|
||||||
|
Lincoln Continental 10.4
|
||||||
|
Chrysler Imperial 14.7
|
||||||
|
Fiat 128 32.4
|
||||||
|
Honda Civic 30.4
|
||||||
|
Toyota Corolla 33.9
|
||||||
|
Toyota Corona 21.5
|
||||||
|
Dodge Challenger 15.5
|
||||||
|
AMC Javelin 15.2
|
||||||
|
Camaro Z28 13.3
|
||||||
|
Pontiac Firebird 19.2
|
||||||
|
Fiat X1-9 27.3
|
||||||
|
Porsche 914-2 26.0
|
||||||
|
Lotus Europa 30.4
|
||||||
|
Ford Pantera L 15.8
|
||||||
|
Ferrari Dino 19.7
|
||||||
|
Maserati Bora 15.0
|
||||||
|
Volvo 142E 21.4
|
||||||
|
|
17
tests/testthat/test-correlations-module.R
Normal file
17
tests/testthat/test-correlations-module.R
Normal file
|
@ -0,0 +1,17 @@
|
||||||
|
test_that("correlations module works", {
|
||||||
|
testServer(data_correlations_server, args=list(data = mtcars,cutoff = shiny::reactive(.8)), {
|
||||||
|
expect_equal(nchar(output$suggest), 281)
|
||||||
|
expect_equal(class(output$correlation_plot),"list")
|
||||||
|
expect_equal(length(output$correlation_plot),5)
|
||||||
|
})
|
||||||
|
|
||||||
|
expect_snapshot(
|
||||||
|
correlation_pairs(data = gtsummary::trial,threshold = .2)
|
||||||
|
)
|
||||||
|
|
||||||
|
expect_snapshot(
|
||||||
|
sentence_paste(letters[1:8])
|
||||||
|
)
|
||||||
|
|
||||||
|
})
|
||||||
|
|
|
@ -1,3 +1,83 @@
|
||||||
test_that("Create columnSelectInput", {
|
test_that("Create columnSelectInput", {
|
||||||
expect_snapshot(columnSelectInput("x",label = "X",data = mtcars))
|
library(shiny)
|
||||||
|
ui <- shiny::fluidPage(
|
||||||
|
shiny::uiOutput("x"),
|
||||||
|
shiny::uiOutput("out")
|
||||||
|
)
|
||||||
|
server <- function(input, output, session) {
|
||||||
|
library(FreesearchR)
|
||||||
|
output$x <-
|
||||||
|
shiny::renderUI({
|
||||||
|
columnSelectInput(inputId = "x",selected = "mpg",label = "X",data = mtcars)
|
||||||
|
})
|
||||||
|
|
||||||
|
output$out <- renderText({
|
||||||
|
# req(input$x)
|
||||||
|
input$x
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
# shinyApp(ui,server)
|
||||||
|
|
||||||
|
testServer(server, {
|
||||||
|
session$setInputs(x = "cyl")
|
||||||
|
expect_equal(output$out, "cyl")
|
||||||
|
|
||||||
|
session$setInputs(x = "mpg")
|
||||||
|
expect_equal(output$out, "mpg")
|
||||||
|
})
|
||||||
|
|
||||||
|
server <- function(input, output, session) {
|
||||||
|
library(FreesearchR)
|
||||||
|
output$x <-
|
||||||
|
shiny::renderUI({
|
||||||
|
columnSelectInput(inputId = "x",label = "X",data = gtsummary::trial)
|
||||||
|
})
|
||||||
|
|
||||||
|
output$out <- renderText({
|
||||||
|
# req(input$x)
|
||||||
|
input$x
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
# shinyApp(ui,server)
|
||||||
|
|
||||||
|
testServer(server, {
|
||||||
|
session$setInputs(x = "trt")
|
||||||
|
expect_equal(output$out, "trt")
|
||||||
|
|
||||||
|
session$setInputs(x = "stage")
|
||||||
|
expect_equal(output$out, "stage")
|
||||||
|
})
|
||||||
|
|
||||||
|
})
|
||||||
|
|
||||||
|
test_that("Create columnSelectInput", {
|
||||||
|
library(shiny)
|
||||||
|
ui <- shiny::fluidPage(
|
||||||
|
shiny::uiOutput("x"),
|
||||||
|
shiny::uiOutput("out")
|
||||||
|
)
|
||||||
|
server <- function(input, output, session) {
|
||||||
|
library(FreesearchR)
|
||||||
|
output$x <-
|
||||||
|
shiny::renderUI({
|
||||||
|
vectorSelectInput(inputId = "x",choices = setNames(names(mtcars),seq_len(ncol(mtcars))),label = "X")
|
||||||
|
})
|
||||||
|
|
||||||
|
output$out <- renderText({
|
||||||
|
# req(input$x)
|
||||||
|
input$x
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
# shinyApp(ui,server)
|
||||||
|
|
||||||
|
testServer(server, {
|
||||||
|
session$setInputs(x = "cyl")
|
||||||
|
expect_equal(output$out, "cyl")
|
||||||
|
|
||||||
|
session$setInputs(x = "mpg")
|
||||||
|
expect_equal(output$out, "mpg")
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
86
tests/testthat/test-data_plots.R
Normal file
86
tests/testthat/test-data_plots.R
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
## all_but
|
||||||
|
test_that("all_but works", {
|
||||||
|
expect_snapshot(all_but(1:10, c(2, 3), 11, 5))
|
||||||
|
})
|
||||||
|
|
||||||
|
## subset_types
|
||||||
|
test_that("subset_types works", {
|
||||||
|
expect_snapshot(
|
||||||
|
default_parsing(mtcars) |> subset_types("continuous")
|
||||||
|
)
|
||||||
|
expect_snapshot(
|
||||||
|
default_parsing(mtcars) |> subset_types(c("dichotomous", "ordinal", "categorical"))
|
||||||
|
)
|
||||||
|
expect_snapshot(
|
||||||
|
default_parsing(mtcars) |> subset_types("test")
|
||||||
|
)
|
||||||
|
})
|
||||||
|
|
||||||
|
## supported_plots
|
||||||
|
test_that("supported_plots works", {
|
||||||
|
expect_true(is.list(supported_plots()))
|
||||||
|
})
|
||||||
|
|
||||||
|
## possible_plots
|
||||||
|
test_that("possible_plots works", {
|
||||||
|
expect_snapshot(possible_plots(mtcars$mpg))
|
||||||
|
|
||||||
|
expect_snapshot(default_parsing(mtcars)["cyl"] |>
|
||||||
|
possible_plots())
|
||||||
|
})
|
||||||
|
|
||||||
|
## get_plot_options
|
||||||
|
test_that("get_plot_options works", {
|
||||||
|
expect_snapshot(default_parsing(mtcars)["mpg"] |>
|
||||||
|
possible_plots() |>
|
||||||
|
(\(.x){
|
||||||
|
.x[[1]]
|
||||||
|
})() |>
|
||||||
|
get_plot_options())
|
||||||
|
})
|
||||||
|
|
||||||
|
## create_plot and friends
|
||||||
|
test_that("create_plot works", {
|
||||||
|
|
||||||
|
## Violin
|
||||||
|
p_list <- create_plot(mtcars, type = "plot_violin", pri = "mpg", sec = "cyl", ter = "am")
|
||||||
|
p <- p_list[[1]] + ggplot2::labs(title = "Test plot")
|
||||||
|
|
||||||
|
expect_equal(length(p_list), 2)
|
||||||
|
expect_true(ggplot2::is.ggplot(p))
|
||||||
|
|
||||||
|
# Includes helper functions
|
||||||
|
# wrap_plot_list
|
||||||
|
# align_axes
|
||||||
|
# clean_common_axis
|
||||||
|
|
||||||
|
## Scatter
|
||||||
|
p_list <- list(
|
||||||
|
create_plot(mtcars, type = "plot_scatter", pri = "mpg", sec = "cyl"),
|
||||||
|
create_plot(mtcars, type = "plot_scatter", pri = "mpg", sec = "cyl", ter = "am")
|
||||||
|
)
|
||||||
|
|
||||||
|
lapply(p_list, \(.x){
|
||||||
|
expect_true(ggplot2::is.ggplot(.x))
|
||||||
|
})
|
||||||
|
|
||||||
|
purrr::map2(p_list, list(11, 11), \(.x, .y){
|
||||||
|
expect_equal(length(.x), .y)
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
## get_label
|
||||||
|
test_that("get_label works", {
|
||||||
|
expect_snapshot(mtcars |> get_label(var = "mpg"))
|
||||||
|
expect_snapshot(mtcars |> get_label())
|
||||||
|
expect_snapshot(mtcars$mpg |> get_label())
|
||||||
|
expect_snapshot(gtsummary::trial |> get_label(var = "trt"))
|
||||||
|
expect_snapshot(1:10 |> get_label())
|
||||||
|
})
|
||||||
|
|
||||||
|
## line_break
|
||||||
|
test_that("line_break works", {
|
||||||
|
expect_snapshot("Lorem ipsum... you know the routine" |> line_break())
|
||||||
|
expect_snapshot(paste(sample(letters[1:10], 100, TRUE), collapse = "") |> line_break(force = TRUE, lineLength = 5))
|
||||||
|
expect_snapshot(paste(sample(letters[1:10], 100, TRUE), collapse = "") |> line_break(force = FALSE))
|
||||||
|
})
|
Loading…
Add table
Reference in a new issue