R/repack.R

Defines functions repack scratch_file

Documented in repack

#' @title FUNCTION_TITLE
#' @description FUNCTION_DESCRIPTION
#' @param x PARAM_DESCRIPTION
#' @param dir_out PARAM_DESCRIPTION, Default: NULL
#' @param single_file PARAM_DESCRIPTION, Default: TRUE
#' @return OUTPUT_DESCRIPTION
#' @details DETAILS
#' @examples
#' \dontrun{
#' if(interactive()){
#'  #EXAMPLE1
#'  }
#' }
#' @rdname repack
#' @export

repack <- function(x, dir_out = NULL, single_file = TRUE){

  if(is.null(dir_out)){

    dir_out <- file.path(tempdir(),'foreman')

  }

  dir_out <- normalizePath(dir_out,mustWork = FALSE)

  if(!dir.exists(dir_out))
    dir.create(dir_out,recursive = TRUE,showWarnings = FALSE)

  if(single_file){

    tf <- file.path(dir_out,'unpacked.R')

    scratch_file(tf)

    invisible({
      lapply(x, function(y){
        cat(y$text,file = tf,append = TRUE,sep = '\n')
      })
    })

    message(sprintf('Functions packed to %s',tf))

  }else{

    invisible({
      lapply(names(x),function(nm){

        scratch_file(file.path(dir_out,nm))

      })

    })

    invisible({
      lapply(names(x),function(nm){

        tf <- file.path(dir_out,nm)

        cat(x[[nm]]$text,file = tf, sep = '\n',append = TRUE)

      })
    })

    message(sprintf('Functions packed to %s',dir_out))

  }

  return(invisible(dir_out))

}

scratch_file <- function(file){

  cat('#Generated by foreman:\n',
      file = file,
      sep='\n',
      append = FALSE)

}
yonicd/foreman documentation built on Nov. 5, 2019, 1:21 p.m.