embed_png <- function(path, dpi = NULL) {
  meta <- attr(png::readPNG(path, native = TRUE, info = TRUE), "info")
  if (!is.null(dpi)) meta$dpi <- rep(dpi, 2)
  knitr::asis_output(paste0(
    "<img src='", path, "'",
    " width=", 400,
    " height=", 300,
    " />"
  ))
}
knitr::opts_chunk$set(comment = "#>", collapse = TRUE)
library(tibble)

Function Examples

Load the Package

library(kowr)

back_to_forward()

Primarily for Windows users. When copying file paths in Windows it will use a backslash to separate files, this will throw an error in R. The process of manually changing the backslashes to forward slashes can get highly repetitive. The back_to_forward() function will take what is in the user's clipboard and paste the path with the wanted forward slashes. There are two approaches to this.

Example file path saved in clipboard: C:\Documents\Newsletters\Summer2018.csv

Option 1) Run back_to_forward() in the Console

Run back_to_forward() in the console. This will output a string with forward slashes.

Option 2) Attatch back_to_forward() to a Hotkey

back_to_forward() is also an add-in called Back to Forward Slash. In RStudio near the top click Addins and you will see the kowr package with the add-in. If you click this add-in it will paste the file path wherever your cursor is located within RStudio. This is not the most ideal way to use this. Since backward_to_forward() is an add-in we can assign the add-in Back to Forward Slash to a hotkey.

  1. Go to Tools > Modify Keyboard Shortcuts... in RStudio
  2. Sort by Name and find Back to Forward Slash
  3. Assign a hotkey to it, on my Mac I used CMD + /
  4. Test out the hotkey with the path in your clipboard!

Arguments


clear_plotly_options()

Default plot_ly() output:
library(plotly)

p <- plot_ly(data = mtcars, x = ~mpg, y = ~hp, type = "scatter", mode = "markers")

p
embed_png("plotly_with_options.png")
Remove all buttons on top of the plot:
p %>% 
  clear_plotly_options()
embed_png("plotly_no_options.png")
Keep "Zoom in" and "Zoom out" buttons:
p %>%
  clear_plotly_options(buttons_to_keep = c("zoomIn2d", "zoomOut2d"))
embed_png("plotly_zoom_options.png")

Arguments:


round_up()

Round whole numbers up. This function will always round up to the number place second to the left:

round_up(x = 123456)

Numbers with only 2 numeric places (tens), then it will round on the tens position value:

round_up(x = 19)

If needed, we can change the value to a dollar amount. Using as_dollar = TRUE will change the numeric value to a character:

round_up(x = 1991, format = "dollar")

Arguments


snake_to()

This is useful for when the user wants to use the columns in a presentation such as reports, plots, or dashboards.

I find myself using stringr::str_to_title() for a clean way of presenting columns in documents and dashboards. This is the reason the default argument for snake_to()'s output is "title". This will only work with snake case (my preferred naming syntax). If you use snake_to() on camelCase then it will capitalize it as if it was one word.

dat <- tibble(
  this_will_get_cleaned    = c(1, 2, 3),
  thisWillNot    = c("a", "b", "c"),
  cleans_snake_only = c("q", "w", "e"),
  notCamcel = c(10, 12, 14)
)

dat %>%
  snake_to()
Sentence case:
dat %>%
  snake_to(format = "sentence")
Title case and keep column names only:
dat %>%
  snake_to(names_only = TRUE)
Title case and capitalize the acronyms
acronyms = c("GET")
dat %>% 
  snake_to(acronyms = acronyms)
ggplot titles:

snake_to() will also clean up ggplot x and y axes. Pipe the saved ggplot object into snake_to() and it will clean up snake case titles. If there is a legend the title will be capitalized using str_to_title().

library(ggplot2)

p <- dat %>% 
  ggplot(aes(x = thisWillNot, y = this_will_get_cleaned)) +
  geom_col()

p

p %>% snake_to()

Arguments



KoderKow/kowr documentation built on July 19, 2021, 4:18 p.m.