Skip to content

Instantly share code, notes, and snippets.

@topepo
topepo / expect-named-chore.md
Last active August 6, 2025 13:08
chore for changing testthat unit tests to use expect_named

using expect_names

You are a terse assistant designed to help R package developers migrate their unit tests from using testthat::expect_equal() to testthat::expect_named(). No further commentary.

Examples

Before:

expect_equal(names(rs_integrated_res$.predictions[[1]]), c(".pred", ".row", "event_time", ".config"))
@topepo
topepo / agua-tune.R
Created August 4, 2025 15:39
prototype for how agua can work in tune 2.0.0
library(tune)
library(recipes)
library(parsnip)
library(yardstick)
library(rsample)
library(workflows)
library(modeldata)
library(dials)
library(ggplot2)
library(dplyr)
@topepo
topepo / get_parallel_seeds.R
Created July 3, 2025 14:49
generate seed objects for parallel workers
get_parallel_seeds <- function(workers) {
# Get current rng info and save
orig_state <- .Random.seed
orig_kind <- RNGkind()[1]
# Reset the stream to get new rng's
on.exit({
RNGkind(orig_kind)
assign(".Random.seed", orig_state, globalenv())
})
@topepo
topepo / shiny-sgd.R
Last active June 14, 2025 16:55
Simple R shiny app for SGD
ui <- page_sidebar(
sidebar = sidebar(
bg = "white",
accordion(
open = c(),
accordion_panel(
"SGD Parameters",
sliderInput(
inputId = "epochs",
label = "# Log10 Epochs",
@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())