R/util.R

Defines functions user_input user_inputs compact print.social_media_img is.social_media_img print.social_media_specs is.social_media_specs

#sysfonts::font.add.google("Droid Sans", "Droid Sans")

is.social_media_specs <- function(x) inherits(x, "social_media_specs")

print.social_media_specs <- function(x, ...) {
  x <- compact(x)
  cat("<social_media_specs>\n")
  cat(paste0(" ", format(paste0(names(x), ": ")), unlist(x), collapse = "\n"))
  cat("\n")
}

is.social_media_img <- function(x) inherits(x, "social_media_img")

print.social_media_img <- function(x, ...) {
  cat("<social_media_img>\n")
  cat(x$name, "\n")
  cat(x$width, "x", x$height, "\n")
  replayPlot(x$plot)
  cat("\n")
}

compact <- function(x) {
  null <- vapply(x, is.null, logical(1))
  x[!null]
}

user_inputs <- function(name) {
  switch(name,
         social_media = user_input("Which one? > "),
         ads = user_input("Ads text? (use \\n as line break) > "),
         x = user_input("Offset for x? (0-1) > " ),
         y = user_input("Offset for y? (1 is center, bigger move up, smaller move down) > "),
         font_size = user_input("What's a good font size? (px) > "),
         ready = user_input("Ready to export? Y/N > ")
  )
}

user_input <- function(prompt, x = NULL) {
  # Ask for user input
  x <- readline(prompt = prompt)
  # Return
  return(x)
}
yizhexu/rsocial documentation built on May 28, 2017, 9:42 a.m.