Skip to content

Instantly share code, notes, and snippets.

View jonocarroll's full-sized avatar
👨‍💻
Learning all the languages

Jonathan Carroll jonocarroll

👨‍💻
Learning all the languages
View GitHub Profile

Keybase proof

I hereby claim:

  • I am jonocarroll on github.

  • I am jonocarroll (https://keybase.io/jonocarroll) on keybase.

  • I have a public key ASBY3mLwKW3JR6kHm4Pb0RN9EJIp6G3X8nD9SJMhylURGAo

@jonocarroll
jonocarroll / as.data.frame.matrix_diff.R
Last active April 20, 2019 07:52
Differences in as.data.frame.matrix between R 3.4.3 and R 3.5.3
## create a copy of mtcars
m <- mtcars
## set every other value of cyl to NA
m$cyl <- m$cyl[c(TRUE, NA)]
## show a table of cyl vs am
table(m$cyl, m$am, useNA = "always")
# 0 1 <NA>
# 4 2 3 0
@jonocarroll
jonocarroll / threed_rotate_cube.R
Created November 12, 2018 13:10
Rotating Cube
library(threed)
library(dplyr)
library(gganimate)
library(ggplot2)
angles <- tail(seq(0, pi/2, length.out = 15), -1)
camera_to_world <- threed::look_at_matrix(eye = c(1.5, 1.75, 4), at = c(0, 0, 0))
objlist <- vector("list", length(angles))
@jonocarroll
jonocarroll / image_as_xaxis_latex.R
Created October 16, 2018 11:16
images as xaxis in R via LaTeX
library(rvest)
## GDP per capita, top 11 countries
n_countries <- 11
url <- "https://en.wikipedia.org/wiki/List_of_countries_by_GDP_(nominal)_per_capita"
html <- read_html(url)
gdppc <- html_table(html_nodes(html, "table")[3])[[1]][1:n_countries, ]
## clean up; remove non-ASCII and perform type conversions
gdppc$Country <- gsub("Â ", "", gdppc$Country)
@jonocarroll
jonocarroll / tikz_image_x_axis.R
Created October 12, 2018 05:18
tikz image As x-axis
## flags borrowed from
## http://flagpedia.net/
## use LaTeX tikzDevice to render images as x-axis labels
## based on baptiste: https://gist.github.com/baptiste/9505ff4f54b31d1d7149e435faf295f9
library(tikzDevice)
library(ggplot2)
options(tikzLatexPackages = c(getOption("tikzLatexPackages"), "\\usepackage{graphicx}\n"))
@jonocarroll
jonocarroll / PDFanimation.Rmd
Created October 8, 2018 22:42
PDF Animations in RMarkdown
---
title: "Test Animation in PDF"
author: "Jonathan Carroll"
date: "09/10/2018"
output: pdf_document
header-includes:
- \usepackage[final,autoplay]{animate}
---
```{r setup, include=FALSE}
@jonocarroll
jonocarroll / plus_character.R
Last active October 4, 2018 07:52
Python/Javascript String Concatenation in R
`+` <- function(e1, e2) {
## unary
if (missing(e2)) return(e1)
if (!is.na(suppressWarnings(as.numeric(e1))) & !is.na(suppressWarnings(as.numeric(e2)))) {
## both arguments numeric-like but characters
return(base::`+`(as.numeric(e1), as.numeric(e2)))
} else if ((is.character(e1) & is.na(suppressWarnings(as.numeric(e1)))) |
(is.character(e2) & is.na(suppressWarnings(as.numeric(e2))))) {
## at least one true character
return(paste0(e1, e2))
@jonocarroll
jonocarroll / datasauRus_gganimate.R
Created August 15, 2018 06:20
Animated datasauRus
# modified from https://gist.github.com/sa-lee/c271b47d8065412d3d42c1ad8e63048d
library(datasauRus)
library(gganimate)
library(magrittr)
p <- datasaurus_dozen %>%
ggplot() +
geom_point(aes(x = x, y = y), col = "steelblue") +
theme(legend.position = 'none',
aspect.ratio = 1,