Skip to content

Commit

Permalink
Merge branch 'release/0.13.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
adamblake committed Oct 6, 2023
2 parents 82bd90e + f40553b commit ce40b76
Show file tree
Hide file tree
Showing 39 changed files with 807 additions and 238 deletions.
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: coursekata
Title: Packages and Functions for CourseKata Courses
Version: 0.12.0
Version: 0.13.0
Date: 2023-10-05
Authors@R: c(
person("Adam", "Blake", , "[email protected]", role = c("cre", "aut"),
Expand Down Expand Up @@ -39,6 +39,7 @@ Imports:
rstudioapi (>= 0.13),
supernova (>= 2.5.1),
vctrs (>= 0.4.1),
viridisLite,
yesno (>= 0.1.2)
Suggests:
fivethirtyeight (>= 0.6.2),
Expand Down
7 changes: 5 additions & 2 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,25 @@ export(b0)
export(b1)
export(coursekata_attach)
export(coursekata_install)
export(coursekata_load_theme)
export(coursekata_packages)
export(coursekata_palette)
export(coursekata_palette_provider)
export(coursekata_repos)
export(coursekata_unload_theme)
export(coursekata_update)
export(f)
export(fVal)
export(fit_stats)
export(fitstats)
export(gf_model)
export(gf_model_old)
export(load_coursekata_themes)
export(lower)
export(middle)
export(p)
export(pre)
export(restore_default_themes)
export(scale_discrete_coursekata)
export(split_data)
export(tails)
export(theme_coursekata)
export(upper)
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# `coursekata` change log

## coursekata 0.13.0

- Add `coursekata.quickstart` option, which can reduce load times significantly.
- Reduce expensive lookups when attaching packages, further reducing load times.
- Re-introduce `gf_model` tests for density plots now that upstream is fixed.
- Add `test_fit()` simple model stats to help teachers evaluate student models.

## coursekata 0.12.0

- Remove `sse()`, `ssm()`, `ssr()`, `SSE()`, `SSM()`, `SSR()` functions: they conflict with `Metrics` package.
Expand Down
29 changes: 9 additions & 20 deletions R/coursekata_attach.R
Original file line number Diff line number Diff line change
@@ -1,31 +1,23 @@
#' Attach the CourseKata course packages
#'
#' @param startup Is this being run at start-up?
#'
#' @return An object with info about which course packages are installed and attached.
#' @return A named logical vector indicating which packages were attached.
#' @export
#'
#' @examples
#' coursekata_attach()
coursekata_attach <- function(startup = FALSE) {
coursekata_attach <- function() {
to_attach <- coursekata_detached()
if (length(to_attach)) {
suppressPackageStartupMessages(pkg_require(to_attach))
}

coursekata_attachments(startup)
invisible(suppressPackageStartupMessages(pkg_require(to_attach)))
}


#' Information about CourseKata packages.
#'
#' @param startup Is this being run at start-up?
#'
#' @param pkgs A character vector of packages being loaded.
#' @return A coursekata_attachments object, also of class data.frame with a row for each course
#' package and a column for each of the `package` name, `version`, and whether it is currently
#' `attached`.
#' @keywords internal
coursekata_attachments <- function(startup = FALSE) {
coursekata_attach_message <- function(pkgs) {
if (length(pkgs) == 0) return(NULL)

is_dark_theme <- rstudioapi::isAvailable() &&
rstudioapi::hasFun("getThemeInfo") &&
rstudioapi::getThemeInfo()$dark
Expand All @@ -39,20 +31,17 @@ coursekata_attachments <- function(startup = FALSE) {
cli::ansi_align(version, max(cli::ansi_nchar(version)))
))

msg <- paste(
paste(
cli::rule(
left = cli::style_bold("CourseKata packages"),
right = cli::style_bold("coursekata ", utils::packageVersion("coursekata"))
),
to_cols(pkgs, 2),
sep = "\n"
)

if (startup) packageStartupMessage(msg) else message(msg)

invisible(info)
}


themes <- list(
light = list(
text = cli::col_black,
Expand Down
18 changes: 8 additions & 10 deletions R/coursekata_packages.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# reversed so that most important packages are loaded last (and mask earlier ones)
coursekata_pkg_list <- rev(c(
coursekata_pkgs <- rev(c(
"supernova", "mosaic", "lsr", "Metrics",
"fivethirtyeight", "fivethirtyeightdata", "Lock5withR", "dslabs"
))


#' List all CourseKata course packages
#'
#' @param check_remote_version Should the remote version number be checked? Requires internet, and
Expand All @@ -16,12 +17,13 @@ coursekata_pkg_list <- rev(c(
#' @examples
#' coursekata_packages()
coursekata_packages <- function(check_remote_version = FALSE) {
pkgs <- coursekata_pkg_list
pkgs <- coursekata_pkgs
statuses <- pak::pkg_status(pkgs)
info <- data.frame(
package = pkgs,
installed = pkg_is_installed(pkgs),
installed = pkg_is_installed(pkgs, statuses),
attached = pkg_is_attached(pkgs),
version = pkg_version(pkgs),
version = pkg_version(pkgs, statuses),
stringsAsFactors = FALSE
)

Expand All @@ -35,20 +37,16 @@ coursekata_packages <- function(check_remote_version = FALSE) {


#' List all currently attached CourseKata course packages
#'
#' @return A character vector of the course packages that have been attached.
#' @keywords internal
coursekata_attached <- function() {
info <- coursekata_packages()
info$package[info$attached]
coursekata_pkgs[pkg_is_attached(coursekata_pkgs)]
}


#' List all currently NOT attached CourseKata course packages
#'
#' @return A character vector of the course packages that are not attached.
#' @keywords internal
coursekata_detached <- function() {
info <- coursekata_packages()
info$package[!info$attached]
coursekata_pkgs[!pkg_is_attached(coursekata_pkgs)]
}
35 changes: 9 additions & 26 deletions R/test_fit.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#' Split data into train and test sets.
#'
#' @param data A data frame.
#' @param prop The proportion of rows to assign to the training set.
#' @return A list with two data frames, `train` and `test`.
Expand All @@ -11,43 +10,27 @@ split_data <- function(data, prop = .7) {
}

#' Test the fit of a model on a train and test set.
#'
#' @param model An [`lm`] model (or anything compatible with [`formula()`]).
#' @param model An [`lm`] model.
#' @param df_train A data frame with the training data.
#' @param df_test A data frame with the test data.
#' @return A data frame with the fit statistics.
#' @export
test_fit <- function(model, df_train, df_test) {
fit_stats <- function(model, df_train, df_test) {
fit_train <- stats::update(model, data = df_train)
fit_test <- stats::update(model, data = df_test)
predictor <- m_predictor(model)
predictor <- as.character(rlang::f_lhs(stats::formula(model)))

tibble::tibble(
data = c(
'train',
'test'
),
F = c(
coursekata::f(fit_train),
coursekata::f(fit_test)
),
PRE = c(
coursekata::pre(fit_train),
coursekata::pre(fit_test)
),
data = c("train", "test"),
F = c(f(fit_train), f(fit_test)),
PRE = c(pre(fit_train), pre(fit_test)),
RMSE = c(
Metrics::rmse(df_train[[predictor]], stats::predict(fit_train)),
Metrics::rmse(df_test[[predictor]], stats::predict(fit_test))
),
)
}

#' Get the predictor variable from a model.
#'
#' @param model An [`lm`] model (or anything compatible with [`formula()`]).
#' @return A string with the name of the predictor variable.
#' @keywords internal
m_predictor <- function(model) {
as.character(rlang::f_lhs(stats::formula(model)))
}

#' @rdname fit_stats
#' @export
fitstats <- fit_stats
31 changes: 15 additions & 16 deletions R/theme_coursekata.R → R/theme.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#' The `coursekata` package automatically loads this theme when the package is loaded. This is in
#' addition to a number of other plot tweaks and option settings. To just restore the theme to the
#' default, you can run `set_theme(theme_grey)`. If you want to restore all plot related settings
#' and/or prevent them when loading the package, see [`restore_default_themes`].
#' and/or prevent them when loading the package, see [`coursekata_unload_theme`].
#'
#' @return A gg theme object
#' @export
Expand Down Expand Up @@ -93,10 +93,12 @@ coursekata_palette <- function(indices = integer(0)) {
}

#' Create a function that provides a colorblind palette.
#'
#' @return A function that accepts one argument `n`, which is the number of colors you want to use
#' in the plot. This function is used by scales like `scale_color_discrete` to provide colorblind-
#' safe palettes. See [`scale_discrete_coursekata`] for more information.
#' safe palettes. Where possible, the function will use the hand-picked colors from
#' [`coursekata_palette()`], and when more colors are needed than are available, it will use the
#' [`viridisLite::viridis()`] palette.
#' @seealso scale_discrete_coursekata
#' @export
coursekata_palette_provider <- function() {
unwrap <- function(x) {
Expand All @@ -111,13 +113,10 @@ coursekata_palette_provider <- function() {

provider <- function(n) {
if (n > max_values) {
rlang::warn(paste(
glue::glue("This manual palette can handle a maximum of {max_values}."),
glue::glue("You are requesting {n}.")
))
viridisLite::viridis(n)
} else {
palette[seq_len(n)]
}

palette[seq_len(n)]
}

structure(provider, max_n = max_values)
Expand Down Expand Up @@ -146,15 +145,15 @@ scale_discrete_coursekata <- function(...) {
#' Utility function for loading all themes.
#'
#' This function is called at package start-up and should rarely be needed by the user. The
#' exception is when the user has called [`restore_default_themes()`] and wants to go back to the
#' exception is when the user has called [`coursekata_unload_theme()`] and wants to go back to the
#' CourseKata look and feel. When run, this function sets the CourseKata color palettes
#' [`coursekata_palette()`], sets the default theme to [`theme_coursekata()`], and tweaks some
#' default settings for specific plots. To restore the original `ggplot2` settings, run
#' [`restore_default_themes()`].
#' [`coursekata_unload_theme()`].
#'
#' @seealso coursekata_palette theme_coursekata scale_discrete_coursekata restore_default_themes
#' @seealso coursekata_palette theme_coursekata scale_discrete_coursekata coursekata_unload_theme
#' @export
load_coursekata_themes <- function() {
coursekata_load_theme <- function() {
ggplot2::update_geom_defaults("bar", ggplot2::aes(
`colour` = "black",
`fill` = coursekata_palette(1),
Expand Down Expand Up @@ -227,11 +226,11 @@ load_coursekata_themes <- function() {
#'
#' This function will restore all of the tweaks to themes and plotting to the original `ggplot2`
#' defaults. If you want to go back to the CourseKata look and feel, run
#' [`load_coursekata_themes()`].
#' [`coursekata_load_theme()`].
#'
#' @seealso load_coursekata_themes
#' @seealso coursekata_load_theme
#' @export
restore_default_themes <- function() {
coursekata_unload_theme <- function() {
# find these values by creating a plot, storing it to a variable, and, e.g.
# p$layers[[1]]$geom$default_aes
ggplot2::update_geom_defaults("bar", ggplot2::aes(
Expand Down
Loading

0 comments on commit ce40b76

Please sign in to comment.