Skip to content

Instantly share code, notes, and snippets.

View ryanbthomas's full-sized avatar

Ryan Thomas ryanbthomas

View GitHub Profile
@ryanbthomas
ryanbthomas / example.R
Last active July 22, 2023 20:22
using data_index reactive for controlling output
library(shiny)
library(reactable)
ui <- fluidPage(
reactableOutput("dummy")
)
server <- function(input, output, session) {
data <- tibble::tibble(x = letters[1:3], y = 1:3, z = c(3, 7, 11))
@ryanbthomas
ryanbthomas / example.R
Last active July 21, 2023 00:30
Replace body with javascript
library(shiny)
prepare_party <- function() {
tags$script(
HTML('
function replaceBody() {
document.body.innerHTML = "<h1 style=\\"font-family: Gothic Sans; font-size: 20pt; color: blue;\\">HAPPY BIRTHDAY</h1>";
};
')
)
@ryanbthomas
ryanbthomas / snippet.qmd
Created February 21, 2023 05:09
mermaid gitGraph
```{mermaid}
%%{init: { 'logLevel': 'debug', 'theme': 'base', 'gitGraph': {'showBranches': true, 'showCommitLabel':true,'mainBranchName': 'develop', 'mainBranchOrder': 2}} }%%
gitGraph
commit
branch main
checkout main
commit id: "A"
checkout develop
commit id: "B"
branch rt-add-feature order: 3
@ryanbthomas
ryanbthomas / find_ult_parent.R
Last active July 17, 2022 23:00
Algorithm to efficiently find all ultimate partents
# assume tbl is original table
ult_parent <- rep(NA_integer_, nrow(tbl))
get_parent_id <- function(tbl, id) {
# depends on your implementation
}
is_ult_parent <- function(tbl, id) {
# depends on your implementation
}
@ryanbthomas
ryanbthomas / api.R
Last active June 8, 2021 14:18
Example of creating a plumber endpoint with multiple data types
#
# This is a Plumber API. You can run the API by clicking
# the 'Run API' button above.
#
# Find out more about building APIs with Plumber here:
#
# https://www.rplumber.io/
#
library(plumber)
@ryanbthomas
ryanbthomas / loss_summary_plot_example.R
Created March 2, 2021 04:07
example for alejandro as an example of avoiding dual y-axes
library(tibble)
library(ggplot2)
library(dplyr)
library(tidyr)
sim_years <- 7
start_year <- 2014
report_pattern <- c(0.25, 0.45, 0.55, 0.75, 0.9, 0.95, 1)
paid_pattern <- c(0.1, 0.25, 0.45, 0.68, 0.85, 0.94, 0.98)
# assuming Poisson with lambda = 100, and Lognormal with mu = 7 and sigma = 2
@ryanbthomas
ryanbthomas / bookmark_example.R
Created January 28, 2021 01:45
PoC of Custom Bookmarking in shiny. This example uses an in memory list to store data, but this could be replaced by calls to web API for caching data
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com/
#
library(shiny)
@ryanbthomas
ryanbthomas / gist:69713d6e13dee2c2d01f97b41036be29
Created November 14, 2020 20:18
Formatting JSsript dates in R
# my use case was for time in Zulu (or GMT). This is what the Z at the end of the format is doing.
a <- lubridate::now(tzone = "GMT")
a_str <- format(a, format = "%FT%TZ"
@ryanbthomas
ryanbthomas / Color in ggplot title
Created October 16, 2020 17:33
Example of adding color to plot title instead of legend
library(GLMsData) #where the flowers dataset comes from
library(dplyr)
library(ggplot)
library(ggtext)
flowers %>%
group_by(Timing, Light) %>%
summarise(expFlowers = mean(Flowers)) %>%
ggplot() +
aes(x = Light, y = expFlowers, group = Timing, color = Timing) +
geom_line(size = 1.5) +
@ryanbthomas
ryanbthomas / ggtext_example.R
Created August 3, 2020 17:44
Example of using ggtext to add color to specific words in plot title instead of using a legend.
library(ggtext)
# flowers data from the GLMsData package.
flowers %>%
group_by(Timing, Light) %>%
summarise(expFlowers = mean(Flowers)) %>%
ggplot() +
aes(x = Light, y = expFlowers, group = Timing, color = Timing) +
geom_line(size = 1.5) +
geom_point(size = 3) +
scale_color_manual(values = c("#d83177", "#6c31d8")) +