Skip to content

Instantly share code, notes, and snippets.

View moritzpschwarz's full-sized avatar

Moritz Schwarz moritzpschwarz

View GitHub Profile
# base plot
ggplot(mtcars, aes(x = mpg, y = hp)) +
geom_point()
# Ensure that the legend is single line (e.g. at the bottom)
# I need to use guide_legend() because it is discrete
# use guide_colorbar() for continuous
ggplot(mtcars, aes(x = mpg, y = hp, color = cyl)) +
geom_point() +
@moritzpschwarz
moritzpschwarz / add_source_to_package_functions.R
Last active February 11, 2023 21:30
Add :: to package functions
library(xfun)
gsub_dir(dir = "R", pattern = "mutate\\(", replacement = "dplyr::mutate\\(")
gsub_dir(dir = "R", pattern = "select\\(", replacement = "dplyr::select\\(")
gsub_dir(dir = "R", pattern = "filter\\(", replacement = "dplyr::filter\\(")
gsub_dir(dir = "R", pattern = "bind_rows\\(", replacement = "dplyr::bind_rows\\(")
gsub_dir(dir = "R", pattern = "bind_cols\\(", replacement = "dplyr::bind_cols\\(")
gsub_dir(dir = "R", pattern = "case_when\\(", replacement = "dplyr::case_when\\(")
gsub_dir(dir = "R", pattern = "left_join\\(", replacement = "dplyr::left_join\\(")
gsub_dir(dir = "R", pattern = "full_join\\(", replacement = "dplyr::full_join\\(")
@moritzpschwarz
moritzpschwarz / change_bib_retain_capitalizationtitle.R
Last active January 22, 2023 19:03
When using biblatex, most citation styles do not retain capitalization of titles. This little R script reads in an existing bib file, changes the titles and then saves another bib file.
# read in the initial bib file
tex <- readLines("test.bib")
# get the location of the titles (NOTE: also will include fields of the nature 'booktitle')
title_loc <- grepl("title", tex, fixed = TRUE)
# Change the opening bracket from { to {{
# the full regex looks for strings containing 'title = {,' or 'title={' and replaces it with 'title = {'
tex[title_loc] <- gsub("title = \\{|title=\\{","title = {{",tex[title_loc])
@moritzpschwarz
moritzpschwarz / release_package_to_CRAN.R
Last active March 18, 2024 09:59
Steps to release a package to CRAN
# check out all info here: https://r-pkgs.org/release.html
# update NEWS
# udpate cran-comments.Rd
tools::resaveRdaFiles("data/")
# build the tarball and check whether the size is below 5 MB
devtools::build()
@moritzpschwarz
moritzpschwarz / kable_tex_commands.R
Last active October 12, 2023 14:16
kable commands for tex tables
# Source: oxforddown explanation in the chapter on tables
library(kableExtra)
head(mtcars) %>%
kable(booktabs = TRUE) %>%
kable_styling(latex_options = "scale_down")
a_long_table <- rbind(mtcars, mtcars)
a_long_table %>%
kable(foo, format = "latex", booktabs = T, row.names = FALSE, linesep = "", escape = FALSE) %>%
kable_styling(latex_options = c("striped")) %>%
group_rows("$\\\\text{Group1}^a$", 1, 2, escape = FALSE) %>%
group_rows(paste0("Group2\\\\", footnote_marker_alphabet(1), sep = ""), 3, 4, escape = FALSE) %>%
# I don't think expression() is helpful, doesn't seem to get converted
# to latex
group_rows(expression("Group3"^a), 5, 6) %>%
group_rows("Group4\\\\textsuperscript{a}", 7, 8, escape = FALSE)
@moritzpschwarz
moritzpschwarz / debugging_tools.R
Last active September 19, 2022 10:50
A small collection of debugging tools in R
# to get to the recover list - allows you to walk through all frames
options(error = recover)
# to get back from a call to the options page again, use `c`
# just loading any library to try these functions out
library(gets)
# trace will execute any function when you execute the first function
# e.g. it will execute `browser` when I execute `getsm`
trace(getsm, browser)
@moritzpschwarz
moritzpschwarz / logLik.plm.R
Last active December 11, 2023 20:30
logLik.plm function
logLik.plm <- function(object){
out <- -plm::nobs(object) * log(2 * var(object$residuals) * pi)/2 - deviance(object)/(2 * var(object$residuals))
attr(out,"df") <- nobs(object) - object$df.residual
attr(out,"nobs") <- plm::nobs(object)
return(out)
}
library(plm)
library(tidyverse)
library(fastDummies)
data(EmplUK)
EmplUK %>%
select(-sector) %>%
dummy_cols(.data = .,select_columns = c("firm","year"),remove_selected_columns = TRUE,remove_first_dummy = TRUE) -> paneldata
head(paneldata)
@moritzpschwarz
moritzpschwarz / save_files_within_function_to_global_env.R
Last active December 21, 2020 04:17
This is a function that can be executed, when using Debugging and being in the browser. This will save all variables that are in the environment of the function into the global environment. The source for this comes from [StackOverflow](https://stackoverflow.com/questions/55809121/is-there-a-way-to-save-variables-in-debugger-mode-to-global-envir…
lapply(ls(), function(o) assign(x = o, value = get(o), envir = .GlobalEnv))