R/addin.R

Defines functions addin_print addin_speak

Documented in addin_print addin_speak

#' RStudio Addin: Vocalise An R Expression
#'
#' Vocalise selected R expression aloud in English (macOS only).
#'
#' @export
addin_speak <- function() {

  # Get RStudio editor info
  active_doc <- rstudioapi::getActiveDocumentContext()

  if (!is.null(active_doc)) {

    # Capture current highlighted expression
    selected_expr <- active_doc$selection[[1]]$text

    # Capture expression as translated r2eng object
    obj <- translate_string(selected_expr, speak = FALSE)

    # Use the speak() method
    speak(obj)

  }

}

#' RStudio Addin: Print Translated R Expression
#'
#' Print the highlighted R expression and English translation
#'
#' @export
addin_print <- function() {

  # Get RStudio editor info
  active_doc <- rstudioapi::getActiveDocumentContext()

  if (!is.null(active_doc)) {

    # Capture current highlighted expression
    selected_expr <- active_doc$selection[[1]]$text

    # Capture expression as translated r2eng object
    obj <- translate_string(selected_expr, speak = FALSE)

    # Use the print() method to print R expression and English sentence
    print(obj)

  }

}
matt-dray/r2eng documentation built on April 29, 2024, 5:24 p.m.