Skip to content

Instantly share code, notes, and snippets.

@topepo
topepo / light-dark-chore.md
Last active May 2, 2025 12:08
light-dark conversion (book)

Transitioning Traditional Quarto chunks to light/dark mode

You are a terse assistant designed to help R package developers migrate Quarto content for R code chunks for figures and tables to fenced code blocks. No further commentary.

After the transition, the traditional code chunk’s label is used in the outer layer of fencing. The chunk’s label is altered to remove the prefix fig- or tbl-.

The caption from the input is removed from the R code chunk and is added to the last div layer. All other R code chunk options should remain the same.

In the new output, add a new yaml option: #| renderings: [light, dark]

@topepo
topepo / trad-chunks-to-fenced-divs.md
Last active April 23, 2025 18:04
Transitioning Traditional Quarto chunks to fenced divs

Transitioning Traditional Quarto chunks to fenced divs

You are a terse assistant designed to help R package developers migrate Quarto content for R code chunks for figures and tables to fenced code blocks. No further commentary.

After the transition, the original label yaml option is used in a fenced div and the label is removed from the traditional code chunk’s options.

After the transition, the original caption yaml option is added to the last div layer and the caption is removed from the traditional code chunk’s options.

The label and caption options should be the only yaml options removed form the code chunk.

@topepo
topepo / prob_cal.R
Created October 29, 2024 12:48
demonstration of probably's calibration functions with non-standard names and groups
library(tidymodels)
library(probably)
set.seed(12)
example <-
two_class_example %>%
mutate(
model = sample(c("logistic", "xgboost"), size = nrow(two_class_example), replace = TRUE)
) %>%
rename(`$@#!` = Class1, cls_2.5 = Class2)
@topepo
topepo / smooth_ph_linear_pred.R
Created June 18, 2024 16:05
function to produce a smooth linear predictor trend for survival models
smooth_ph_linear_pred <- function(formula, data, deg_free = 5, grid_size = 500) {
require(rlang)
rlang::is_installed("survival")
rlang::is_installed("ggplot2")
rlang::is_installed("splines2")
rlang::is_installed("cli")
require(ggplot2)
# check 1 pred and continuous
pred_sym <- rlang::f_rhs(formula)
@topepo
topepo / nested_example.R
Created June 6, 2024 13:30
nested resampling in tidymodels
# pak::pak(c("tidymodels/finetune@nested"), ask = FALSE)
library(tidymodels)
library(finetune)
library(rlang)
library(sfd)
library(doMC)
# ------------------------------------------------------------------------------
tidymodels_prefer()
@topepo
topepo / lm_vs_nnet.R
Created October 24, 2023 17:10
A simple comparison of two models with different feature set approaches
library(tidymodels)
library(doMC)
# ------------------------------------------------------------------------------
tidymodels_prefer()
theme_set(theme_bw())
options(pillar.advice = FALSE, pillar.min_title_chars = Inf)
registerDoMC(cores = parallel::detectCores())
@topepo
topepo / fuser_wrapper.R
Created April 1, 2023 18:54
example code for a model wrapper for the fuser package
# Allow fuser functions to work in tidymodels
# See https://stackoverflow.com/questions/75871678/how-can-i-pass-an-extra-variable-to-a-tidymodels-fit-function
# Create a function that can parse a formula that uses a special (non-existant)
# `groups()` function. This returns a list of character vectors that has the
# columns and their roles based on the formula. It works with '.'.
fuser_variables <- function(f, data) {
cl <- match.call()
trms <- terms(f, data = data, specials = "groups")
@topepo
topepo / extract_code
Created April 13, 2022 21:55
extract_code for purl'ing
extract_code <- function(file) {
# TODO we could put in an argument for the pattern
# or something more sophisticated...
target <- tempfile()
knitr::purl(file, output = target, quiet = TRUE)
code <- readLines(target)
code <- gsub("^##.*", "", code)
r_file <- gsub("[Rq]md$", "R", file)
writeLines(code, r_file)
invisible(TRUE)
@topepo
topepo / check_for_remotes
Created January 31, 2022 16:27
Check to see if a repo has remote dependencies
#' Check to see if a repo has remote dependencies.
#' @param repos A character vector of repos to query (e.g. `tidyverse/dplyr`).
#' @param hard_depend Should only depends and imports be included in the query.
#' Using `FALSE` will include suggests and others.
#' @details Packages listed in `Config/Needs/*` will not be returned.
#' Requires dplyr, pak, and purrr.
#' @return A tibble with columns for the repo and any github repos that are
#' listed as dependencies.
#' @examples
#' tests <- paste0("tidymodels/", c("broom", "parsnip", "recipes"))
@topepo
topepo / two_class_diag_plots.R
Created May 6, 2021 22:00
two class diagnostic plots for shinymodels
library(tidymodels)
tidymodels_prefer()
theme_set(theme_bw())
library(doMC)
registerDoMC(cores = 20)
# ------------------------------------------------------------------------------
data(ad_data)