R/insert_emoji.R

Defines functions insert_emoji

Documented in insert_emoji

#' Insert Emoji into R Script using a function
#'
#' Inserts an emoji by name from the emoji_dict through writing the function in the cursor position of the Rstudio Document.
#'
#' @param name The name of the emoji (case-insensitive).
#' @param default A default emoji to use if the name is not found.
#' @return The emoji used (invisibly).
#' @export
#'
#' @examples
#' if (interactive()) {
#'   insert_emoji("smile")
#'   insert_emoji("notarealemoji", default = "?")
#' }
insert_emoji <- function(name, default = NULL) {
  name <- tolower(name)
  if (!name %in% names(emoji_dict)) {
    if (!is.null(default)) {
      rstudioapi::insertText(text = default)
      return(invisible(default))
    } else {
      stop("Emoji name not found.")
    }
  }
  emoji <- emoji_dict[[name]]
  rstudioapi::insertText(text = emoji)
  invisible(emoji)
}

Try the Rmoji package in your browser

Any scripts or data that you put into this service are public.

Rmoji documentation built on June 8, 2025, 12:25 p.m.