R/steps-write-text-file.R

Defines functions step_write_text_file

Documented in step_write_text_file

WriteTextFile <- R6Class(
  "WriteTextFile",
  inherit = TicStep,

  public = list(
    initialize = function(..., path) {
      private$contents <- c(...)
      private$path <- path
    },

    run = function() {
      dir.create(dirname(private$path), recursive = TRUE)
      writeLines(private$contents, private$path)
    },

    check = function() {
      !ci_is_interactive()
    }
  ),

  private = list(
    contents = NULL,
    path = NULL
  )
)

#' Step: Write a text file
#'
#' Creates a text file with arbitrary contents
#'
#' @param ... `[character]`\cr
#'   Contents of the text file.
#' @param path `[string]`\cr
#'   Path to the new text file.
#'
#' @family steps
#' @export
#' @examples
#' dsl_init()
#'
#' get_stage("script") %>%
#'   add_step(step_write_text_file("Hi!", path = "hello.txt"))
#'
#' dsl_get()
step_write_text_file <- function(..., path) {
  WriteTextFile$new(..., path = path)
}
ropenscilabs/tic documentation built on Feb. 5, 2024, 5:06 p.m.