knitr::opts_chunk$set(echo = TRUE, warning = FALSE, message = FALSE)
library(jbkmisc)
library(printr)
library(lubridate)

Why? What?

Idk:

Installation

source("https://install-github.me/jbkunst/jbkmisc")

Date helpers

ym_to_date

ym_to_date(ym = c(200902, 201912), day = 1)

ym_diff

ym_diff(ym = c(200902, 201912), ym2 = c(200901, 201712))

ym_add_months

ym_add_months(c(200902, 201912), months = c(1, -12))

ym_format

ym_format(c(200902, 201912), "%B del %Y")

ym_div

year <- format(ymd(20170101) + months(0:11), "%Y%m")
year

ym_div(year, ng = 3)

ym_div(year, ng = 4)

dplyr

countp

countp(mtcars, cyl)

countp(mtcars, cyl, am)


library(dplyr)

mtcars %>%
  group_by(cyl) %>% 
  countp(am)

ccount

ccount(iris, Species)

clean

head(iris, 3)

clean(head(iris, 3))

dbplyr

dbRemoveTableIfExists

dbRemoveTableIfExists(con, name)

compute2

compute2(con, name)

ggplot2

theme_jbk

Based on hrbrthemes::theme_ipsum, soft gridline color, legend at top for more horizontal space.

library(ggplot2)
library(dplyr)

data("diamonds")
d <- diamonds %>% 
  filter(color %in% c("E", "J", "H")) %>% 
  filter(cut %in% c("Ideal", "Premium", "Very Good")) %>% 
  sample_n(10000)
theme_set(theme_jbk())

ggplot(d, aes(carat, price)) + 
  geom_point(aes(color = clarity), size = 0.1) + 
  scale_y_continuous(labels = scales::dollar) + 
  scale_color_viridis_d() + 
  facet_grid(cut ~ color, scales = "free") + 
  labs(
    title = "This is a Title",
    subtutle = "This is a longer subtitle",
    caption = "A caption which nobody read but it looks important"
  )

ggsav and filename_gen

Automatic generation file names given a pattern to save without worry about names :D.

blog & presentations

spin_jekyll_post

My custom spin r file to md and move the widgets, etc.

blog_set_chunk

Set opt chunk with my preferences

giphy

Put a giphy image given the id.

ico

ico("tv", "red")
R()

tictoc

jbk_tictoc_clear()

jbk_tic(msg = "Some sleep")

Sys.sleep(2)

jbk_toc()

jbk_tic(msg = "Go to sleep again!")

Sys.sleep(1)

jbk_toc()

jbk_tictoc_log()

shiny

get_my_local_ip

get_my_local_ip()

Report Helpers

format_tbl

data <- data.frame(
  a_value.e = c(NA, runif(3)),
  `another.vAlue__here` = c(NA, runif(3)) * 10000
  )

data

format_tbl(data)

format_tbl(data, digits = 0, NA_symbol = "***", fun_title = toupper)

Stringr

str_clean

string <- c("name.with...points", "or_Under___scores")
string

str_clean(string)
str_clean(string, fun = toupper)

Workflow

wf_create_folders

create data, code and output folder.



jbkunst/jbkmisc documentation built on Nov. 8, 2019, 11:40 a.m.