R/utils.R

Defines functions `%?%` `%||%` md_to_html mkdir slug

`%?%` <- function(x, y) if (length(x) > 0) y
`%||%` <- function(x, y) if (length(x) > 0) x else y

md_to_html <- function(input) {
  tmp_in <- tempfile()
  tmp_out <- tempfile()
  
  on.exit(unlink(c(tmp_in, tmp_out), force = TRUE))
  writeLines(input, tmp_in)
  
  rmarkdown::pandoc_convert(tmp_in, to = "html", from = "markdown", output = tmp_out)
  
  readr::read_file(tmp_out)
}

mkdir <- function(x, recursive = FALSE) {
  walk(x, dir.create, showWarnings = FALSE, recursive = recursive)
}

slug <- function(x) {
  x %>%
    tolower() %>%
    stringr::str_replace_all("'", "") %>% 
    stringr::str_replace_all("[^a-z0-9]+", "-") %>% 
    stringr::str_replace_all("-+", "-") %>% 
    stringr::str_replace_all("^-|-$", "")
}
hadley/recipes documentation built on Jan. 1, 2020, 5:06 a.m.