Site defaults

Author

Josef Fruehwald

_defaults.R
library(ggplot2)
library(ggdist)
library(showtext)
library(patchwork)
library(rlang)
library(systemfonts)

font_add_google("Public Sans", "Public Sans")
font_add_google("Fira Code", "Fira Code")
# font_add(
#   family = "Public Sans",
#   regular = here::here("assets", "Public_Sans", "static", "PublicSans-Regular.ttf"),
#   italic =  here::here("assets", "Public_Sans", "static", "PublicSans-Italic.ttf"),
#   bold = here::here("assets", "Public_Sans", "static", "PublicSans-Bold.ttf")
# )
showtext_auto()



ptol_red = "#CC6677"
ptol_blue = "#4477AA"

theme_set(
  theme_ggdist(base_size = 12) + 
  theme(
    text = element_text(
      family = "Public Sans",
      #face = "regular"
    )
  )
)

dark_theme <- function(){
  theme(
    panel.border = element_blank(),
    text = element_text(colour = "white"),
    axis.text = element_text(colour = "white"),
    rect = element_rect(colour = "#222", fill = "#222"),
    plot.background = element_rect(fill = "#222", colour = NA),
    panel.background = element_rect(fill = "#424952"),
    strip.background = element_rect(fill="#3d3d3d"),
    strip.text = element_text(color = "white")
    #axis.line = element_line(colour = "white"),
    #axis.ticks = element_line(colour = "white")
  )
}


tol_blue <- "#4477AA"

theme_no_y <- function(){
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    axis.title.y = element_blank()
  )
}

# ragg_png <- ragg_png <- function(..., res = 150) {
#   ragg::agg_png(..., res = res, units = "in")
# }

light_png <- function(file, width, height){
  ggsave(
    filename = file,
    width = width,
    height = height,
    dev = png
  )
}

dark_png <- function(file, width, height) {
  on.exit(ggdark::invert_geom_defaults())
  ggdark::invert_geom_defaults()
  p <- last_plot()
  if("patchwork" %in% class(p)){
    p <- p & dark_theme()
  }else{
    p <- p + dark_theme()
  }
  ggsave(
    filename = file,
    plot = p,
    width = width,
    height = height,
    dev = png
  )
}

knitr::knit_hooks$set(crop = knitr::hook_pdfcrop)


options(
  mc.cores = parallel::detectCores(),
  ggplot2.discrete.colour = lapply(1:12, ggthemes::ptol_pal()),
  ggplot2.discrete.fill = lapply(1:12, ggthemes::ptol_pal()),
  ggplot2.continuous.colour = \(x) scico::scale_color_scico(palette = "batlow"),
   ggplot2.continuous.fill = \(x) scico::scale_fill_scico(palette = "batlow")
)


my_gt_theme <- function(tbl){
  
  style_cells <- list(
    cells_body(),
    cells_column_labels(),
    cells_column_spanners(),
    cells_footnotes(),
    cells_group(),
    cells_row_groups(),
    cells_source_notes(),
    cells_stub(),
    cells_stubhead(),
    cells_title()
  )
  
  summary_info <- tbl$`_summary` |> 
    map(
      ~":GRAND_SUMMARY:" %in% .x$groups
    )  |> 
    list_simplify()
  
  if(any(summary_info)){
    style_cells <- c(
      style_cells,
      list(
        cells_grand_summary(),
        cells_stub_grand_summary()
      )
    )
  }
  
  if(any(!(summary_info %||% T))){
    style_cells <- c(
      style_cells,
      list(
        cells_stub_summary(),
        cells_summary()
      )
    )
  }
  
  tbl |> 
    opt_table_font(
      font = c(
        google_font(name = "Public Sans"),
        default_fonts()
      )
    )|> 
    tab_style(
      style = "
        background-color: var(--bs-body-bg);
        color: var(--bs-body-color)
      ",
      locations = style_cells
    ) 
  
}

Reuse

CC-BY 4.0

Citation

BibTeX citation:
@online{fruehwald,
  author = {Fruehwald, Josef},
  title = {Site Defaults},
  url = {https://lin611-2024.github.io/default.html},
  langid = {en}
}
For attribution, please cite this work as:
Fruehwald, Josef. n.d. “Site Defaults.” https://lin611-2024.github.io/default.html.