R/dput_addins.R

Defines functions dputSelectedAddin

Documented in dputSelectedAddin

#   __________________ #< 90a81bcce8455070cdfb04265d716dd6 ># __________________
#   dput() selection addin                                                  ####


#' @title Replaces selected code with its dput() output
#' @description
#'  \Sexpr[results=rd, stage=render]{lifecycle::badge("experimental")}
#'
#'  RStudio Addin:
#'  Runs \code{\link[base:dput]{dput()}} on the selected code and inserts
#'  it instead of the selection.
#'
#'  See \code{`Details`} for how to set a key command.
#' @author Ludvig Renbo Olsen, \email{r-pkgs@@ludvigolsen.dk}
#' @param selection String of code. (Character)
#'
#'  E.g. \code{"stop('This gives an expect_error test')"}.
#'
#'  \strong{N.B.} Mainly intended for testing the addin programmatically.
#' @param indentation Indentation of the selection. (Numeric)
#'
#'  \strong{N.B.} Mainly intended for testing the addin programmatically.
#' @param insert Whether to insert the expectations via
#'  \code{\link[rstudioapi:rstudio-documents]{rstudioapi::insertText()}}
#'  or return them. (Logical)
#'
#'  \strong{N.B.} Mainly intended for testing the addin programmatically.
#' @export
#' @family addins
#' @return Inserts the output of running
#'  \code{\link[base:dput]{dput()}} on the selected code.
#'
#'  Does not return anything.
#' @details
#'  \subsection{How}{
#'  Parses and evaluates the selected code string,
#'  applies \code{\link[base:dput]{dput()}} and
#'  inserts the output instead of the selection.
#'  }
#'  \subsection{How to set up a key command in RStudio}{
#'
#'  After installing the package.
#'  Go to:
#'
#'  \code{Tools >> Addins >> Browse Addins >> Keyboard Shortcuts}.
#'
#'  Find \code{"dput() Selected"} and press its field under \code{Shortcut}.
#'
#'  Press desired key command, e.g. \code{Alt+D}.
#'
#'  Press \code{Apply}.
#'
#'  Press \code{Execute}.
#'  }
dputSelectedAddin <- function(selection = NULL, insert = TRUE, indentation = 0) {

  # Check arguments ####
  assert_collection <- checkmate::makeAssertCollection()
  checkmate::assert_string(x = selection, null.ok = TRUE,
                           add = assert_collection)
  checkmate::assert_flag(x = insert, add = assert_collection)
  checkmate::assert_integerish(x = indentation, lower = 0,
                               any.missing = FALSE,
                               null.ok = TRUE,
                               add = assert_collection)
  checkmate::reportAssertions(assert_collection)
  # End of argument checks ####

  # Get the selection and indentation
  if (is.null(selection)){
    selection <- tryCatch(get_selection(),
                          error = function(e){return("")},
                          message = function(m){return("")})
    indentation <- tryCatch(get_indentation(),
                            error = function(e){return(0)})
  }

  # Get parent environment
  parent_envir <- parent.frame()

  # dput() and insert/return
  if (!is.null(selection) && selection != "") {
    dput_out <- apply_capture(selection, dput, envir = parent_envir)

    if (!isTRUE(insert)) {
      # Return the expectations instead of inserting them
      return(dput_out)
    } else {
      # Insert the expectations
      insert_code(dput_out, indentation = indentation)
    }

  }

  invisible()
}

Try the xpectr package in your browser

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

xpectr documentation built on Nov. 18, 2022, 5:10 p.m.